r - Conditional calculation across columns
问题描述
I am trying to create a new column that counts the amount of numbers that are grater than 5 across columns. for each number greater than 5 I would like to add 20 to a new column.
I am a beginner with R (For several years now :) and a loop approach is to intimidating for me. I would like to know if there is an easy tydivere option.
Example data set:
df <- data.frame(name = paste0("name", 1:7),
X1 = c(-1:5),
X2 = c(1:7),
X3 = c(2:8),
X4 = c(3:9),
X5 = c(4:10),
X6 = c(5:11))
DesiredOutcome <- c(0,20,40,60,80,100,100)
df <- cbind(df, DesiredOutcome)
df
I would be very grateful for any help and tips for how to approach something like this in the future.
解决方案
We can count the number of values in each row which is greater than 5 using rowSums
and multiply them by 20 to get the final output.
df$output <- rowSums(df[-1] > 5) * 20
df
# name X1 X2 X3 X4 X5 X6 output
#1 name1 -1 1 2 3 4 5 0
#2 name2 0 2 3 4 5 6 20
#3 name3 1 3 4 5 6 7 40
#4 name4 2 4 5 6 7 8 60
#5 name5 3 5 6 7 8 9 80
#6 name6 4 6 7 8 9 10 100
#7 name7 5 7 8 9 10 11 100
Adding some alternatives
Using apply
apply(df[-1] > 5, 1, sum) * 20
#[1] 0 20 40 60 80 100 100
Using sapply
rowSums(sapply(df[-1], function(x) x > 5)) * 20
#[1] 0 20 40 60 80 100 100
Or with tidyverse
library(tidyverse)
df %>% mutate(output = rowSums(.[-1] > 5) * 20)
Or
df %>% mutate(output = pmap_dbl(.[-1], ~ sum(c(...) > 5) * 20))
推荐阅读
- pandas - 从 unix 框中读取 .zip 文件后如何返回 Pandas 数据框
- reactjs - 如何将 MockedProvider 用于与 compose HOC 一起使用的依赖查询?
- python - 在删除某些列时将 CSV 转换为 JSON
- c - 从输入文件中获取整数的最佳方法
- c++ - 如何解决向量和 T* 的问题
- jquery - jQuery读取复杂的表结构
- php - 如何将最大表单提交计数修复为 3
- postgresql - ST_DWithin 耗时较长(超过 6000ms)
- bash - 从shell脚本中的行首删除字符?
- c - 为什么 gcc 为 memcpy 复制 rodata 字符串?如何避免?