I have a dataframe:
ID value
1 he following object is masked from ‘package:purrr’
2 Attaching package: ‘magrittr’
3 package ‘ggplot2’ was built under R version 3.6.2
4 Warning messages:
here is a code to transform a column value:
df <- df %>%
mutate(value = stringr::str_replace(value, '(^he following object)', '\\1'),
value = stringr::str_replace(value, '(^Attaching package:)', '\\1'),
value = stringr::str_replace(value, '(^package ‘ggplot2’)', '\\1'))
) %>%
group_by(ID, value)
the output is:
ID value
1 he following object
2 Attaching package:
3 package ‘ggplot2’
4 Warning messages:
As you see i use stringr::str_replace several times for one column. my actual data is much much larger (like millions of rows). this is just a subset example. so, how could i combine this three times using of this functioning one time? i want to use same functions and libraries(no radical change)
I tried this, but it doesn't work too:
df <- df %>%
mutate(value = str_replace_all(value, '(^he following object).*|(^Attaching package:).*|(^package ‘ggplot2’).*', '\\1')) %>%
group_by(ID, value)
It gave me this:
ID value
1 he following object’
2
3
4 Warning messages: