My data is like this, and I want to select the row with the word shift and place it in same number row but another column
lt may look like
truck 4 6 landfill 53 164 shif 1....(shif is in the same row as it was but a new colum just for shift ,
If I understand correctly, you are trying to extract part of a string from one column and display it in a new column. Below is one way to do that, which extracts "shift x" from the from column and displays it in a new column named shift.
library(tidyverse)
# sample data
df = data.frame(
truck_id = paste0('truck ', 1:5),
from = c('CHORILLO', 'CHORILLO', '',
'shift 1 with 0 time left',
'truck is running shift 3 today'))
df
#> truck_id from
#> 1 truck 1 CHORILLO
#> 2 truck 2 CHORILLO
#> 3 truck 3
#> 4 truck 4 shift 1 with 0 time left
#> 5 truck 5 truck is running shift 3 today
# addd new column for shift
out = df %>%
mutate(shift = str_extract(from,'shift [0-9]'))
out
#> truck_id from shift
#> 1 truck 1 CHORILLO <NA>
#> 2 truck 2 CHORILLO <NA>
#> 3 truck 3 <NA>
#> 4 truck 4 shift 1 with 0 time left shift 1
#> 5 truck 5 truck is running shift 3 today shift 3
Hello, thank you for your help , I have another question
Now I want to do the same (use the mutate function to create a new colum ) with the line 141 and 140 but I only want the numbers in the new column . for 140 line I just want in the new colum 194 and for the 141 I need 6877 .
Could you help me with this also ?
For those lines I want to use again the mutate function, and create the new column but I only want the number 6877 kg from 141 line and 194 from line 140 . How do I do that one ?