将一行分成多行.R

R

options(stringsAsFactors=FALSE)
df <- data.frame(x1=c("A","B","D","E"),
                 x2=c("中国、美国","德国、日本","意大利",NA) )
country_list <- strsplit(with(df,x2),"、")
count_n <- sapply(country_list,length)
data.frame(x1=rep(with(df,x1),count_n),x2=unlist(country_list))


或者

library(tidyr)
df %>% separate_rows(x2, sep = "、")

  

 

posted @ 2018-08-14 17:39  qinqinyang  阅读(691)  评论(0编辑  收藏  举报