大型数据集中使用零删除行不起作用

发布于 2025-02-12 14:23:13 字数 369 浏览 1 评论 0原文

我有一个大的数据集,即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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜是你 2025-02-19 14:23:13

要删除包含至少一个零的行,您可以使用以下代码:

library(gss)
data("Sachs")
Sachs[!apply(Sachs==0,1,any),]

To remove rows with contain at least one zero, you can use the following code:

library(gss)
data("Sachs")
Sachs[!apply(Sachs==0,1,any),]
瘫痪情歌 2025-02-19 14:23:13

或使用dplyr

library(tidyverse)
library(gss)
data("Sachs")

Sachs |> filter(!if_any(everything(), ~ . == 0))

Or using dplyr:

library(tidyverse)
library(gss)
data("Sachs")

Sachs |> filter(!if_any(everything(), ~ . == 0))
勿忘心安 2025-02-19 14:23:13

最快的可能是:

Sachs[!rowSums(Sachs==0),]

不需要明确地放入0,因为否定操作员只会将零值转换为true。我想知道,如果您放弃否定,并且只使用rowsums(sachs!= 0),但是除非您也使用as.logical,否则您可能会失败,因为您可能会失败,因为结果将是数字。

Fastest would probably be:

Sachs[!rowSums(Sachs==0),]

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 used as.logical you might fail because the result would be numeric.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文