How can I get unique values in dataframe column into a dataframe even though the number of values is not the same

后端 未结 2 1152
闹比i
闹比i 2021-01-23 00:19

I have a dataframe Data:

Data <- data.frame(A=sample(1:7),B=c(5,5,5,6,6,6,6),C=c(1,2,2,3,3,4,5))
  A B C
1 6 5 1
2 7 5 2
3 4 5 2
4 2 6 3
5 1 6 3
6 5 6 4
7 3 6         


        
2条回答
  •  旧巷少年郎
    2021-01-23 00:42

    Here's a possible data.table solution

    library(data.table)
    data.frame(t(setDT(Data)[, lapply(.SD, function(x) {
                                      temp <- unique(x)
                                      c(sort(temp), 
                                      rep(NA, length(x) - length(temp)))
                                      })]))
    
    #   X1 X2 X3 X4 X5 X6 X7
    # A  1  2  3  4  5  6  7
    # B  5  6 NA NA NA NA NA
    # C  1  2  3  4  5 NA NA
    

提交回复
热议问题