如果colume中的值为0
,我想删除一行。我试过了:
> dput(df)
structure(list(ID = c(1L, 98L, 765L, 13L, 3L, 24L, 24L), Desc = c("hallpo",
"TEST", "asdfsd", "alla", "asdfs", "sd", "sd"), Val = c(0L, 0L,
0L, 100L, 123L, 2L, 10L), CODEX = c("Random", "Random", "Random",
"Random", "Random", "Random", "Random"), IsCustomer = c(TRUE,
FALSE, TRUE, TRUE, FALSE, FALSE, TRUE)), .Names = c("ID", "Desc",
"Val", "CODEX", "IsCustomer"), row.names = c(NA, -7L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x0000000000110788>)
>
> df$Val[apply(df$Val[,-1], 1, function(x) !all(x==0)),]
Error in df$Val[, -1] : incorrect number of dimensions
为什么我的尺寸错误?
感谢您的回复!
更新
我改变了我的data.frame,现在它变得复杂得多。
df <- dfFile.Value != 0]
Error in `[.data.frame`(df,File.Value != 0) :
对象File.Value&#39;找不到
str(df)
'data.frame': 14839 obs. of 4 variables:
$ test.Person : Factor w/ 3088 levels "","*asdfasdgw*",..: 9 adf ...
$ test.Value : Factor w/ 5484 levels "-243","-8575",..: 3 3 3909 4792 3 1662 3 3 3 3 ...
任何建议我做错了什么?
答案 0 :(得分:3)
我相信这就是你想要的(A中的数据):
> A
ID Desc Val CODEX IsCustomer
1: 1 hallpo 0 Random TRUE
2: 98 TEST 0 Random FALSE
3: 765 asdfsd 0 Random TRUE
4: 13 alla 100 Random TRUE
5: 3 asdfs 123 Random FALSE
6: 24 sd 2 Random FALSE
尝试(谢谢akrun和David)
B <- A[Val != 0]
B现在是
ID Desc Val CODEX IsCustomer
1: 13 alla 100 Random TRUE
2: 3 asdfs 123 Random FALSE
3: 24 sd 2 Random FALSE
4: 24 sd 10 Random TRUE
新数据:
library(data.table)
set.seed(8964)
Code <- seq_len(15)
Description <- replicate(15, paste(sample(x = letters, size = 5, replace = TRUE), collapse = ""))
Value <- sample(x = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), size = 15, replace = TRUE, prob = c(.55, rep(.05, 9)))
Other <- rep('Stochastic', 15)
Cust <- sample(x = c(TRUE, FALSE), size = 15, replace = TRUE)
Dt <- data.table(Code, Description, Value, Other, Cust)
> Dt
Code Description Value Other Cust
1: 1 octov 0 Stochastic TRUE
2: 2 pwcdl 0 Stochastic TRUE
3: 3 ucknw 0 Stochastic TRUE
4: 4 mletd 0 Stochastic FALSE
5: 5 esaej 0 Stochastic TRUE
6: 6 jzizz 5 Stochastic FALSE
7: 7 lcgep 0 Stochastic TRUE
8: 8 thrnq 0 Stochastic FALSE
9: 9 fmdpx 3 Stochastic TRUE
10: 10 eukwo 0 Stochastic FALSE
11: 11 tuvss 0 Stochastic TRUE
12: 12 tlqom 0 Stochastic FALSE
13: 13 uirpr 9 Stochastic TRUE
14: 14 onctx 0 Stochastic FALSE
15: 15 epdgo 0 Stochastic TRUE
> Dt[Value != 0]
Code Description Value Other Cust
1: 6 jzizz 5 Stochastic FALSE
2: 9 fmdpx 3 Stochastic TRUE
3: 13 uirpr 9 Stochastic TRUE