Text-to-column equivalent in R, splitting dataframe on character

后端 未结 2 598
逝去的感伤
逝去的感伤 2021-01-12 11:36

I\'d like to know how to split columns in a similar way that excel does in the \"text-to-column\" feature. There are many tutorials on stackexchange about how to split colum

2条回答
  •  醉酒成梦
    2021-01-12 12:15

    Splitting with strsplit and then accessing with "[" seems to work. You do realize those were factors to begin with I hope?

    spl <-strsplit(as.character(df$V1), ",")
    data.frame(V1= sapply(spl, "[", 1), V2 = sapply(spl, "[", 2), df$V2)
      V1   V2  df.V2
    1 01   02 11, 12
    2 04      14
    3 05   06 13, 14
    4 07   08 11, 14
    5 09      13
    6 10      15
    

提交回复
热议问题