Home > Blockchain >  New column with the count of columns that meet certain criteria
New column with the count of columns that meet certain criteria

Time:08-24

I searched a lot and I could not find a good solution for this simple problem. I tried rowSums, but with no success.

I have a df like the first image. I want to create a new column (V4), preferably using tidyverse, with the count of rows that meet a certain condition. In this example, the condition would be . == 5.

enter image description here

How many times number 5 appears in the other columns:

enter image description here

Example df

df <- data.frame(V1 = c(1,2,5,5,3),
                 V2 = c(1,5,5,5,5),
                 V3 = c(1,3,4,5,1))

CodePudding user response:

We could use rowSums on a logical matrix

df$V4 <- rowSums(df == 5)

If we want a dplyr solution

library(dplyr)
df <- df %>%
   mutate(V4 = rowSums(cur_data() == 5))

Or may also use reduce

library(purrr)
df %>% 
   mutate(V4 = across(everything(), `==`, 5) %>%
           reduce(` `))

CodePudding user response:

Here is another dplyr option:

library(dplyr)

df %>% 
  rowwise %>% 
  mutate(V4 = sum(c_across(V1:V3) == 5, na.rm = TRUE))

Output

     V1    V2    V3    V4
  <dbl> <dbl> <dbl> <int>
1     1     1     1     0
2     2     5     3     1
3     5     5     4     2
4     5     5     5     3
5     3     5     1     1

Or another option using purrr:

library(tidyverse)

df %>%
  mutate(V4 = pmap_int(select(., everything()), ~ sum(c(...) == 5, na.rm = T)))
  • Related