大型数据集中使用零删除行不起作用
我有一个大的数据集,即Sachs,可以在gss
软件包中免费获得。数据如此之大,具有7466个观测值和12个变量。我试图以至少一个为零删除所有行。也就是说,如果一行包含零,则在所有变量上删除此行。例如, 如果一个变量包含零值,则需要删除此行和所有其他变量的相应行。我尝试了所有可用的方法,并且失败了。这是我的尝试之一。我知道这个网站上已经存在许多类似的问题,但是我尝试了所有问题,但它们都不适合我。
library(gss)
data <- data.frame(Sachs[,-12])
dat <- data[apply(data,1, function(x) all(data!= 0.0000000)),]
View(dat)
I have a large data set, namely Sachs which is freely available at the gss
package. The data is so large with 7466 observations and 12 variables. I tried to remove all rows with at least one zero. That is, if one row contains zero, then remove this row over all the variables. For example,
if one variable contains zero value, then this row and the corresponding row of all other variables need to be removed. I tried all available methods and, I am failing. Here is one of my tries. I know that many similar questions are already there on this website, but I tried all of them but none of them work for me.
library(gss)
data <- data.frame(Sachs[,-12])
dat <- data[apply(data,1, function(x) all(data!= 0.0000000)),]
View(dat)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要删除包含至少一个零的行,您可以使用以下代码:
To remove rows with contain at least one zero, you can use the following code:
或使用
dplyr
:Or using
dplyr
:最快的可能是:
不需要明确地放入0,因为否定操作员只会将零值转换为true。我想知道,如果您放弃否定,并且只使用
rowsums(sachs!= 0)
,但是除非您也使用as.logical
,否则您可能会失败,因为您可能会失败,因为结果将是数字。Fastest would probably be:
Don’t need to put in the >0 explicitly because the negation operator will convert only the zero values to TRUE. I wondered if it also would succeed if you dropped the negation and just used
rowSums(Sachs != 0)
, but unless you also usedas.logical
you might fail because the result would be numeric.