首页 > 解决方案 > 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.

标签: r

解决方案


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))

推荐阅读