i have data frame consists of composite information. split vector vectors "a" , "d", "a" corresponds numeric id 898, 3467 ,234 ,222 , vector "d" contains corresponding character values.
data:
a<-c("898_me","3467_you or ", "234_hi-hi", "222_what") b<-c(1,8,3,8) c<-c(2,4,6,2) df<-data.frame(a,b,c)
what tried far:
a<-str(df$a) a<-strsplit(df$a, split)
but doesn't work out regular expression skills.
the required output table might have form:
d b c 898 me 1 2 3467 or 8 3 234 hi-hi 3 6 222 8 2
library(tidyr) a<-c("898_me","3467_you or ", "234_hi-hi", "222_what") b<-c(1,8,3,8) c<-c(2,4,6,2) df <-data.frame(a,b,c) final_df <- separate(df , , c("a" , "d") , sep = "_") # d b c #1 898 me 1 2 #2 3467 or 8 4 #3 234 hi-hi 3 6 #4 222 8 2 final_df$d # [1] "me" "you or " "hi-hi" "what"
Comments
Post a Comment