将所有行均匀地符合r的条件

发布于 2025-02-13 14:11:00 字数 806 浏览 3 评论 0原文

我有一个数据框架,例如:

df <- data.frame(col1 = c(1, 2500,1, 1, 1), 
                 col2 = c(12, NA, 8,9, 5), 
                 col3 = c(25, 48, 7, 9, 14))
df
  col1 col2 col3
1    1   12   25
2 2500   NA   48
3    1    8    7
4    1    9    9
5    1    5   14

我希望仅当单元格值高于1时,而忽略na值,才能将行总结为这样的数据框架:

df_desired <- data.frame(col1 = c(2500), 
                         col2 = sum(12, 8,9, 5), 
                         col3 = sum(25, 48, 7, 9, 14))
df_desired
  col1 col2 col3
1 2500   34  103

我已经走了这么远:

df_test <- df %>%
summarise_at(vars(contains("col")), sum, na.rm = TRUE)
df_test
  col1 col2 col3
1 2504   34  103

我've尝试了ifelse的许多变体,但没有成功。到目前为止,我只发现了跨列的有条件求和并添加新列的示例。

I have a data frame such as:

df <- data.frame(col1 = c(1, 2500,1, 1, 1), 
                 col2 = c(12, NA, 8,9, 5), 
                 col3 = c(25, 48, 7, 9, 14))
df
  col1 col2 col3
1    1   12   25
2 2500   NA   48
3    1    8    7
4    1    9    9
5    1    5   14

I am hoping to sum the rows only when cell value is above 1, while ignoring NA values, to make a data frame like this:

df_desired <- data.frame(col1 = c(2500), 
                         col2 = sum(12, 8,9, 5), 
                         col3 = sum(25, 48, 7, 9, 14))
df_desired
  col1 col2 col3
1 2500   34  103

I have gotten this far:

df_test <- df %>%
summarise_at(vars(contains("col")), sum, na.rm = TRUE)
df_test
  col1 col2 col3
1 2504   34  103

I've tried many variations of ifelse with no success. I've so far only found examples of conditional summing across columns and adding a new column.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

零度° 2025-02-20 14:11:00

在上使用概述

library(dplyr)
df %>%
  summarise(across(everything(),  ~ sum(.x[.x > 1], na.rm = TRUE)))
  col1 col2 col3
1 2500   34  103

在中使用 base r 带colsums 替换替换 in小于2至na

colSums(replace(df, df < 2, NA),  na.rm = TRUE)
col1 col2 col3 
2500   34  103 

Using summarise with across

library(dplyr)
df %>%
  summarise(across(everything(),  ~ sum(.x[.x > 1], na.rm = TRUE)))
  col1 col2 col3
1 2500   34  103

Or in base R with colSums after replaceing the elements that are less than 2 to NA

colSums(replace(df, df < 2, NA),  na.rm = TRUE)
col1 col2 col3 
2500   34  103 
苦妄 2025-02-20 14:11:00

我们可以在设置map_dbl之后使用Ifelse语句:

library(purrr)
library(dplyr)

df %>% 
  mutate(across(, ~ifelse(. == 1, 0, .))) %>% 
  map_dbl(~ sum(.x, na.rm = TRUE))

或不Purrr

library(dplyr)

df %>% 
  mutate(across(, ~ifelse(. == 1, 0, .))) %>% 
  summarise(across(, ~sum(., na.rm = TRUE)))
col1 col2 col3 
2500   34  103 

We could use map_dbl after setting each 1 to 0 within a ifelse statement:

library(purrr)
library(dplyr)

df %>% 
  mutate(across(, ~ifelse(. == 1, 0, .))) %>% 
  map_dbl(~ sum(.x, na.rm = TRUE))

Or without purrr:

library(dplyr)

df %>% 
  mutate(across(, ~ifelse(. == 1, 0, .))) %>% 
  summarise(across(, ~sum(., na.rm = TRUE)))
col1 col2 col3 
2500   34  103 
孤蝉 2025-02-20 14:11:00

在基本中使用colsums

colSums(df * (df > 1), na.rm = TRUE)

col1 col2 col3 
2500   34  103

in Base R use colSums:

colSums(df * (df > 1), na.rm = TRUE)

col1 col2 col3 
2500   34  103
盛夏已如深秋| 2025-02-20 14:11:00

使用sapply函数,您解决问题如下:

sapply(df, function(x) sum(x[x>1], na.rm=TRUE))

col1 col2 col3 
2500   34  103

或使用汇总函数

df |> 
  summarise(across(, ~ sum(.[.>1], na.rm=TRUE)))
  col1 col2 col3
1 2500   34  103

Using sapply function, you solve your problem as follow:

sapply(df, function(x) sum(x[x>1], na.rm=TRUE))

col1 col2 col3 
2500   34  103

or using summarise function

df |> 
  summarise(across(, ~ sum(.[.>1], na.rm=TRUE)))
  col1 col2 col3
1 2500   34  103
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文