2016-06-10 3 views
-1

해당 데이터 프레임으로 변환해야하는 목록이 있습니다.목록 목록의 NULL 값을 다른 길이/다른 길이의 목록으로 바꿉니다.

행의 위치는 나중에 다른 항목에 연결할 수 있도록 중요합니다. 내가 데이터 프레임으로 만들 때 한 가지 방법을 시도했지만 NULL이되는 일부 행 때문에 그렇게 할 수는 없습니다.

first = c(1,2,3) 
    second = c(1,2) 
    problemrows = NULL 

    mylist = list(first,second,problemrows) 
    #mylist - should turn into a dataframe with each row being same order as  the list of lists. NULL value would be a null row 

library(plyr) # doesn't work because of NULLs above 
    t(plyr::rbind.fill.matrix(lapply(mylist, t))) 
    ## help^^^ Ideally the row that doesn't work would just be a null row or the new dataframe would remove these NULL lists as rows and instead assign appropriate row#s to everything else to make up for what was skipped. 


    # other approach - change all the NULL list of lists to something like -999 which is another fix. 

답변

1
first = c(1,2,3) 
second = c(1,2) 
problemrows = NULL 

mylist = list(first,second,problemrows) 

mylist <- sapply(mylist, function(x) ifelse(x == "NULL", NA, x)) 

library(plyr) # doesn't work because of NULLs above 
t(plyr::rbind.fill.matrix(lapply(mylist, t))) 
관련 문제