我有一个长度为3的data.frame - sku_data
:
数据样本是:
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_current
或units_end
长度为<=1
的元素。它们都是列表清单。
由于
答案 0 :(得分: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
答案 1 :(得分: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