R: Convert delimited string into variables

后端 未结 3 1086
盖世英雄少女心
盖世英雄少女心 2020-11-30 15:12

I have a data frame with a column containing a space-delimited list of character codes:

\"Ab B C\"
\"\"
\"X C\"
\"N Ab F S\"
:

I want to co

相关标签:
3条回答
  • 2020-11-30 15:46

    You can try

    library(qdapTools)
    lst <- strsplit(df1$Col1, ' ')
    cbind(df1, mtabulate(lst))
    #      Col1 Ab B C F N S X
    #1   Ab B C  1 1 1 0 0 0 0
    #2           0 0 0 0 0 0 0
    #3      X C  0 0 1 0 0 0 1
    #4 N Ab F S  1 0 0 1 1 1 0
    

    Or using base R

    lvls <- sort(unique(unlist(lst)))
    cbind(df1, t(vapply(lst, function(x) table(factor(x, levels=lvls)),
                    numeric(length(lvls)))))
    

    data

    df1 <- structure(list(Col1 = c("Ab B C", "", "X C", "N Ab F S")),
    .Names = "Col1", row.names = c(NA, -4L), class = "data.frame")
    
    0 讨论(0)
  • 2020-11-30 15:54

    In base R, another approach:

    lst  = strsplit(df$Col1, ' ')
    cols = unique(unlist(lst))
    
    m = do.call(rbind, lapply(lst, function(u) cols %in% u +0))
    colnames(m) = cols
    
    #> m
    #     Ab B C X N F S
    #[1,]  1 1 1 0 0 0 0
    #[2,]  0 0 0 0 0 0 0
    #[3,]  0 0 1 1 0 0 0
    #[4,]  1 0 0 0 1 1 1
    
    0 讨论(0)
  • 2020-11-30 16:01

    Assuming you are starting with:

    df <- data.frame(v1 = c("Ab B C", "", "X C", "N Ab F S"))
    

    You can try cSplit_e from my "splitstackshape" package:

    library(splitstackshape)
    cSplit_e(df, "v1", sep = " ", type = "character", fill = 0)
    #         v1 v1_Ab v1_B v1_C v1_F v1_N v1_S v1_X
    # 1   Ab B C     1    1    1    0    0    0    0
    # 2              0    0    0    0    0    0    0
    # 3      X C     0    0    1    0    0    0    1
    # 4 N Ab F S     1    0    0    1    1    1    0
    
    0 讨论(0)
提交回复
热议问题