Split columns in dataframe with NA

前端 未结 5 2437
心在旅途
心在旅途 2021-02-20 05:16

I have a df like this:

df <- data.frame(FOO = c(\'A|B|C\', \'A|B\', \'B|C\', \'A\', \'C\'))

> df
    FOO
1 A|B|C
2   A|B
3   B|C
4     A
5            


        
5条回答
  •  半阙折子戏
    2021-02-20 05:21

    Use unique and strsplit to find all unique values (A, B and C in this case). Use grep to search for the unique values, and return the values when there's a match or character(0) otherwise. cbind the resulting characters. Use apply and ifelse to replace character(0) with NA.

    vals <- unique(unlist(sapply(a1, function(x) strsplit(x, '|', fixed = T))))
    
    out <- NULL
    for(i in vals){
      out <- cbind(out, as.character((lapply(df$FOO, function(x) grep(x, i, value=T)))))
    }
    
    apply(out, 2, function(x) ifelse(x=="character(0)", NA, x))
    
         [,1] [,2] [,3]
    [1,] "A"  "B"  "C" 
    [2,] "A"  "B"  NA  
    [3,] NA   "B"  "C" 
    [4,] "A"  NA   NA  
    [5,] NA   NA   "C" 
    

提交回复
热议问题