【发布时间】:2020-10-07 11:02:10
【问题描述】:
我有一个像这样的矩阵
structure(list(Item = structure(c(3L, 1L, 4L, 2L), .Label = c("boat",
"bus ", "Car", "jeep"), class = "factor"), Cost = structure(c(3L,
1L, 2L, 4L), .Label = c("E.02", "E0.23", "E2.03", "E5.60"), class = "factor"),
BMW = c(0, 2.5, 12, 0.089), VW = c(0.56, 4.56, 0, 0.054),
Bens = c(0.06, 5.01, 4.57, 0), Pet = c(0.02, 5.31, 0.36,
45)), class = "data.frame", row.names = c(NA, -4L))
我需要删除为零的列。 我试过申请
row_sub = apply(data, 1, function(row) all(row !=0 ))
data[row_sub,]
但它不起作用
预期输出
Item Cost BMW VW Bens Pet
boat E.02 2.5 4.56 5.01 5.31
【问题讨论】:
-
这能回答你的问题吗? How to remove rows with any zero value
-
您的意思是要删除包含零的行吗?因为您的标题表示列,但您的预期输出表明您的意思是行。
-
row_sub = apply(data!=0, 1, all)
标签: r