英文:
Filter out dataframe rows where specific columns have zeroes (R)
问题
假设我有一个如下的数据框:
df <- data.frame(
Cat = c("A", "B", "C", "D", "E", "F"),
S1 = c(0, 0, 0, 0, 0, 0),
S2 = c(3, 0, 0, 0, 0, 2),
S3 = c(-3, 3, 2, 0, 0, 5),
S4 = c(0, -5, 5, 0, 0, 0)
)
我想要删除所有S1、S2、S3和S4都为零的行,这样我只会剩下Cat A、B、C和F。我不能简单地使用行求和,因为这样也会错误地删除Cat A(因为3 + -3 = 0)。请注意,在真实数据中,有36个"Sx"列,列名每个月都会变化。有什么想法吗?谢谢!
英文:
suppose I've got a dataframe like this:
df <- data.frame(
Cat = c("A", "B", "C", "D", "E", "F"),
S1 = c(0, 0, 0, 0, 0, 0),
S2 = c(3, 0, 0, 0, 0, 2),
S3 = c(-3, 3, 2, 0, 0, 5),
S4 = c(0, -5, 5, 0, 0, 0)
)
I want to remove any rows where S1, S2, S3, and S4 are all zeroes, so that I'd only be left with Cat A, B, C, and F. I can't simply use use a row sum because that would also wrongly remove Cat A (since 3 + -3 = 0). Note that in the real data, there are 36 "Sx" columns with names that will change every month. Any thoughts? Thank you!
答案1
得分: 1
你可以这样做:
library(tidyverse)
df %>%
mutate(keep = apply(across(-Cat), 1, function(x) !all(x == 0))) %>%
filter(keep == TRUE) %>%
select(-keep)
其中df
是你的数据框,它包含了Cat
、S1
、S2
、S3
和S4
这几列。这段代码的作用是对数据框进行处理,首先使用mutate
函数创建一个名为keep
的新列,该列的值为逐行判断除了Cat
列之外的其他列是否全部为0,如果全部为0,则为FALSE
,否则为TRUE
。然后使用filter
函数筛选出keep
列值为TRUE
的行,最后使用select
函数去除keep
列。最终得到的结果是一个新的数据框,其中只包含了满足条件的行。
英文:
You can do:
library(tidyverse)
df %>%
mutate(keep = apply(across(-Cat), 1, function(x) !all(x == 0))) %>%
filter(keep == TRUE) %>%
select(-keep)
Cat S1 S2 S3 S4
1 A 0 3 -3 0
2 B 0 0 3 -5
3 C 0 0 2 5
4 F 0 2 5 0
答案2
得分: 1
你可以使用rowSums(df[-1] == 0)
来计算每行中0的数量,不包括第一列。
df[rowSums(df[-1] == 0) < (nrow(df) - 1), ]
# Cat S1 S2 S3 S4
# 1 A 0 3 -3 0
# 2 B 0 0 3 -5
# 3 C 0 0 2 5
# 6 F 0 2 5 0
英文:
You can use rowSums(df[-1] == 0)
to count the number of 0s in each row, excluding the first column.
df[rowSums(df[-1] == 0) < (nrow(df) - 1), ]
# Cat S1 S2 S3 S4
# 1 A 0 3 -3 0
# 2 B 0 0 3 -5
# 3 C 0 0 2 5
# 6 F 0 2 5 0
答案3
得分: 0
你可以尝试以下代码:
zero_cols <- c("S1", "S2", "S3", "S4")
df[!apply(df[,zero_cols], 1, function(x) all(x == 0)),]
输出结果为:
# Cat S1 S2 S3 S4
# 1 A 0 3 -3 0
# 2 B 0 0 3 -5
# 3 C 0 0 2 5
# 6 F 0 2 5 0
英文:
You could try:
zero_cols <- c("S1", "S2", "S3", "S4")
df[!apply(df[,zero_cols], 1, function(x) all(x == 0)),]
Output:
# Cat S1 S2 S3 S4
# 1 A 0 3 -3 0
# 2 B 0 0 3 -5
# 3 C 0 0 2 5
# 6 F 0 2 5 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论