r - Split a string in each row and duplicate that row -
this question has answer here:
in data frame working on, there 1 column of strings might contains ";". want find them , split string delimited ";" , copy row , put separated string different rows.
here sample of data frame:
name value 10 b;c 20 d 30 e 40 f;g;h 50
and want be:
name value 10 b 20 c 20 d 30 e 40 f 50 g 50 h 50
here trying write:
df$name <- sapply(df$name,function(x) { if (grepl(";",df$name)){ unlist(strsplit(df$name,"[;]"))}})
the error msg says:
condition has length > 1 , first element used
and don't know how put split string different rows
library(dplyr) library(tidyr) df%>%mutate(name=strsplit(as.character(name),';'))%>%unnest(name) name value 1 10 2 b 20 3 c 20 4 d 30 5 e 40 6 f 50 7 g 50 8 h 50
Comments
Post a Comment