how to make a new colum with specific data from rows of 1 colum

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 ,

Thank you !

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

Created on 2023-01-12 with reprex v2.0.2.9000

thank you! it worked

Hello, thank you for your help , I have another question
image
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 ?

image

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 ?

Here is one way to extract the numbers, which says to extract a group of numbers ranging in length from 2-digits to 10-digits.

library(tidyverse)

# sample data
df = data.frame(
  truck_id = paste0('truck ', 1:5),
  from = c('CHORILLO', 'CHORILLO', '',
           'shift 1 with 0 time left, 194.0 to thrash',
           'truck is running shift 3, today generated 6877 kg')
  )

# add "new" column for numbers
out = df %>%
  mutate(shift = str_extract(from,'shift [0-9]'),
         new = str_extract(from, '[0-9]{2,10}'))

out
#>   truck_id                                              from   shift  new
#> 1  truck 1                                          CHORILLO    <NA> <NA>
#> 2  truck 2                                          CHORILLO    <NA> <NA>
#> 3  truck 3                                                      <NA> <NA>
#> 4  truck 4         shift 1 with 0 time left, 194.0 to thrash shift 1  194
#> 5  truck 5 truck is running shift 3, today generated 6877 kg shift 3 6877

Created on 2023-01-12 with reprex v2.0.2.9000

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.