I have a data frame
a = data.frame("a" = c("aaa|abbb", "bbb|aaa", "bbb|aaa|ccc"), "b" = c(1,2,3))
a b
aaa|abbb 1
bbb|aaa 2
bbb|aaa|ccc 3
I want to split the colum value by "|" and sort the output and merge them together to look like this
a b
aaa|abbb 1
aaa|bbb 2
|aaa|bbb|ccc 3
I tried to use following
paste(sort(ignore.case(unlist(strsplit(as.character(a$a), "\\|")))),collapse = ", ")
but that just combine everything together. How can I implement it on each value of column A and get the result as dataframe. I tried to use lapply but still got the same result, one combined list.
We can replace characters using str. replace() method is basically replacing an existing string or character in a string with a new one. we can replace characters in strings is for the entire dataframe as well as for a particular column.
You can replace substring of pandas DataFrame column by using DataFrame. replace() method. This method by default finds the exact sting match and replaces it with the specified value. Use regex=True to replace substring.
We could use separate_rows
to split the values in 'a', then grouped by 'b', sort
'a' and paste
the elements together
library(tidyverse)
a %>%
separate_rows(a) %>%
group_by(b) %>%
summarise(a = paste(sort(a), collapse="|")) %>%
select(names(a))
# A tibble: 3 x 2
# a b
# <chr> <dbl>
#1 aaa|abbb 1
#2 aaa|bbb 2
#3 aaa|bbb|ccc 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With