Hey Community,
How can we split top1,2,3 columns containing string elements, separator being "( ". Although I need only elements to the left. tried separate and str_split but need your help in feeding it to dplyr.
library(stringr)
library(dplyr)
library(rebus)
library(tidyr)
df <- data.frame( AA = runif(10), AB = runif(10), AC = runif(10),
BA = runif(10), BB = runif(10), BC = runif(10),
top1 = c("AA(.9)","AB(.6)","AA(.7)","AA(.2)","AA(.2)","AA(.5)","AA(.4)","AA(.8)","AA(.7)","AC(.3)"),
top2 = c("BA(.9)","BC(.6)","AC(.7)","BB(.2)","AB(.2)","AB(.5)","BA(.4)","BB(.8)","AC(.7)","AB(.3)"),#"AC(.4)","AC(.8)","AC(.7)"),
top3 = c("BB(.9)","AA(.6)","BA(.7)","BA(.2)","BC(.2)","BA(.5)","BB(.4)","BA(.8)","BA(.7)","AB(.3)")#AB(.4)","AB(.8)","AA(.7)")
)
# Convert to character
df[7:9] <- lapply(df[7:9],as.character)
# Expected output for only 1 column
Although Separate works when fed individually
top3sep = separate(df, top3, c("a", "b"))
But it kills the fed column
Needed all: top3, top3sep$a and top3sep$b columns in df
I tried to add 3 more columns with just string value as in top3sep$a
Tried to use stringr and dplyr like this
df %>% mutate_at(.vars = vars(top1,top2),
.funs = funs(vital = str_split(., pattern = "( ", n = 2, simplify = T)))
Seems I am missing some link !!!
Could you please guide on this.
Thanks in advance