2016-05-12 56 views
1

如何檢查我的列「子文檔」中的哪些行是NULL,然後返回行不是NULL列「Subdocuments」的類型是「list」 。檢查類型列表的data.frame列是否爲NULL

以下代碼行我嘗試過,但不起作用,類似的東西應該是解決方案。

newdata<- data[!is.null(data$Subdocuments),] 
    newdata<- data[!is.null(unlist(data$Subdocuments)),] 
    newdata<- data[!is.na(data$Subdocuments),] 
    newdata<- data[!is.na(unlist(data$Subdocuments)),] 

這是數據。

data <- structure(list(ID = c("1", "2", "3"), Country = c("Netherlands", 
     "Germany", "Belgium"), Subdocuments = list(structure(list(Value = c("5", 
     "5", "1", "3", "2", "1", "1", "1", "2", "5", "3", "2", "4", "5", 
     "5", "2"), Label = c("Test1", "Test2", "Test3", "Test4", "Test5", 
     "Test6", "Test7", "Test8", "Test9", "Test10", "Test11", "Test12", 
     "Test13", "Test14", "Test15", "Test16"), Year = c(2001, 2002, 
     2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 
     2014, 2015, 2016)), .Names = c("Value", "Label", "Year"), class = "data.frame", row.names = c(NA, 
     16L)), structure(list(Value = c("5", "4", "3", "2", "2", "2", 
     "1", "1", "5", "4", "4", "4", "5", "1", "1", "3"), Label = c("Test1", 
     "Test2", "Test3", "Test4", "Test5", "Test6", "Test7", "Test8", 
     "Test9", "Test10", "Test11", "Test12", "Test13", "Test14", "Test15", 
     "Test16"), Year = c(2001, 2002, 2003, 2004, 2005, 2006, 2007, 
     2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016)), .Names = c("Value", 
     "Label", "Year"), class = "data.frame", row.names = c(NA, 16L 
     )), NULL)), .Names = c("ID", "Country", "Subdocuments"), row.names = c(NA, 
     -3L), class = "data.frame") 

    class(data$Subdocuments) # "list" 

    is.null(data$Subdocuments[[1]]) # FALSE 
    is.null(data$Subdocuments[[2]]) # FALSE 
    is.null(data$Subdocuments[[3]]) # TRUE 

輸出將是data.frame

使用包data.table將是理想的溶液的前兩行。因爲我有另一個非常大的數據集,我想要使用這個主體。

回答

3

data.table溶液:

dt <- as.data.table(data); 
dt[!sapply(Subdocuments,is.null)]; 
## ID  Country Subdocuments 
## 1: 1 Netherlands <data.frame> 
## 2: 2  Germany <data.frame> 

基礎R溶液:

data[!sapply(data$Subdocuments,is.null),]; 
## ID  Country                                                               Subdocuments 
## 1 1 Netherlands 5, 5, 1, 3, 2, 1, 1, 1, 2, 5, 3, 2, 4, 5, 5, 2, Test1, Test2, Test3, Test4, Test5, Test6, Test7, Test8, Test9, Test10, Test11, Test12, Test13, Test14, Test15, Test16, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 
## 2 2  Germany 5, 4, 3, 2, 2, 2, 1, 1, 5, 4, 4, 4, 5, 1, 1, 3, Test1, Test2, Test3, Test4, Test5, Test6, Test7, Test8, Test9, Test10, Test11, Test12, Test13, Test14, Test15, Test16, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 
+0

一個不太可靠,但也許更快的方式:'數據[長度(數據$子)= 0L, ]' – Frank