2014-11-20 54 views
1

我有長度爲3的data.frame - sku_data: 的數據的樣品是:從data.frame刪除元素與列表的列表

sku_data <- data.frame(
    sku = c(1200337,1200518,1200586,1200590), 
    units_current = I(list(c(1),1:3,1:4,2:4)), 
    units_end = I(list(1:3,1,2:4,1:3)) 
) 

#  sku units_current units_end 
#1 1200337    1 1, 2, 3 
#2 1200518  1, 2, 3   1 
#3 1200586 1, 2, 3, 4 2, 3, 4 
#4 1200590  2, 3, 4 1, 2, 3 

我想刪除那些元素,其中長度units_currentunits_end<=1。它們都是列表清單。

感謝

+0

請提供'dput (head(sku_data))' – 2014-11-20 22:46:18

+0

編輯以使示例更小更簡單。沒有真正需要200個項目列表來顯示數據。 – thelatemail 2014-11-20 23:57:06

回答

2

如何:

f <- function(x) which(sapply(x, length) <= 1L) 

sku_data[-unlist(lapply(sku_data[-1], f)), ] 
#  sku units_current units_end 
# 3 1200586 1, 2, 3, 4 2, 3, 4 
# 4 1200590  2, 3, 4 1, 2, 3 

而且不止一個/不等長度測試匹配

(sku2 <- rbind(sku_data, sku_data, sku_data[1,])) 
#  sku units_current units_end 
# 1 1200337    1 1, 2, 3 
# 2 1200518  1, 2, 3   1 
# 3 1200586 1, 2, 3, 4 2, 3, 4 
# 4 1200590  2, 3, 4 1, 2, 3 
# 5 1200337    1 1, 2, 3 
# 6 1200518  1, 2, 3   1 
# 7 1200586 1, 2, 3, 4 2, 3, 4 
# 8 1200590  2, 3, 4 1, 2, 3 
# 9 1200337    1 1, 2, 3 
sku2[-unlist(lapply(sku2[-1], f)), ] 
#  sku units_current units_end 
# 3 1200586 1, 2, 3, 4 2, 3, 4 
# 4 1200590  2, 3, 4 1, 2, 3 
# 7 1200586 1, 2, 3, 4 2, 3, 4 
# 8 1200590  2, 3, 4 1, 2, 3 
0

另一種選擇是:

sku_data[!rowSums(`dim<-`(rapply(sku_data[,-1], length) <=1, 
             dim(sku_data)-c(0,1))),] 
#  sku units_current units_end 
#3 1200586 1, 2, 3, 4 2, 3, 4 
#4 1200590  2, 3, 4 1, 2, 3