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

后端 未结 2 597
逝去的感伤
逝去的感伤 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 11:57

    May be this helps

    library(splitstackshape)
    cSplit(df, 'V1', sep=", ", type.convert=FALSE)
    #       V2 V1_1 V1_2
    #1: 11, 12   01   02
    #2:     14   04   NA
    #3: 13, 14   05   06
    #4: 11, 14   07   08
    #5:     13   09   NA
    #6:     15   10   NA
    

    If you want both columns to be split

    cSplit(df, 1:ncol(df), sep=",", stripWhite=TRUE, type.convert=FALSE)
    #    V1_1 V1_2 V2_1 V2_2
    #1:   01   02   11   12
    #2:   04   NA   14   NA
    #3:   05   06   13   14
    #4:   07   08   11   14
    #5:   09   NA   13   NA
    #6:   10   NA   15   NA
    

    The default is type.convert= TRUE, which would convert to numeric.

    data

     df <- data.frame(V1 = c("01, 02", "04", "05, 06", "07, 08", "09", "10"),
          V2 = c("11, 12", "14", "13, 14", "11, 14", "13", "15") )
    
    0 讨论(0)
  • 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 <NA>     14
    3 05   06 13, 14
    4 07   08 11, 14
    5 09 <NA>     13
    6 10 <NA>     15
    
    0 讨论(0)
提交回复
热议问题