【问题标题】:Remove all rows where 1 row is 0删除 1 行为 0 的所有行
【发布时间】:2015-03-26 08:43:06
【问题描述】:

如果列中的值为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'

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 ...

任何建议我做错了什么?

【问题讨论】:

  • 你在向量上使用apply df$Val 它应该是apply(df[,-1], 1, ..) 如果你想删除0行的Val,df[Val!=0]
  • 我会使用 %in% 而不是 ==!=,因为在数据中包含 NA 会给您带来意想不到的结果
  • 我相信你有一个. 而不是[。应该是df &lt;- dfFile[Value != 0]

标签: r statistics


【解决方案1】:

我相信这就是你想要的(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

响应 cmets 中的请求进行更新

新数据:

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

【讨论】:

  • 你可以告诉我我不是一个重度data.table 用户,你可以吗:(。谢谢你的更正,akrun 和 David。
  • @mrquad 这似乎与 R 无关,但 Bundler 我对此一无所知,抱歉。这个例子对我有用。另外,请参阅关于句号而不是括号的问题下的评论。
  • 感谢您的回复!您能否重命名示例中的列并用不同的名称显示它?因为我认为我有问题......
  • @mrquad,已按要求更新答案。这有帮助吗?
猜你喜欢
  • 1970-01-01
  • 2014-03-28
  • 2016-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 2019-09-14
相关资源
最近更新 更多