Home > Mobile >  R - Applying condition across multiple columns ignoring NA
R - Applying condition across multiple columns ignoring NA

Time:10-14

Suppose I have the following dataframe:

x <- c(1, 1, 2, 3, 4, 5)
y <- c(1, 1, 1, 3, 4, 5)
z <- c(NA, 1, 1, 3, 4, NA)

to get:

x  y  z
1  1  NA
1  1  1
2  1  1
3  3  3
4  4  4
5  4  NA

and I wanted to get a conditional statement such that if all of the non-NA x, y, and z values are equal to 1, then it would be flagged as 1, how would I go about writing this script?

For instance, what I want is the following:

x  y  z  flag1
1  1  NA 1
1  1  1  1
2  1  1  0
3  3  3  0
4  4  4  0
5  4  NA 0

Additionally, I would also want to flag if any of the variables contained a 4, ignoring NA, so that I can get:

x  y  z  flag1 flag2
1  1  NA 1     0
1  1  1  1     0
2  1  1  0     0
3  3  3  0     0
4  4  4  0     1
5  4  NA 0     1

CodePudding user response:

Easiest is with rowSums

df$flag <-   (!rowSums(df != 1, na.rm = TRUE) & !!rowSums(!is.na(df)))
df$flag2 <-  (rowSums(df == 4, na.rm = TRUE) > 0 & !!rowSums(!is.na(df)))

-output

> df
  x y  z flag flag2
1 1 1 NA    1     0
2 1 1  1    1     0
3 2 1  1    0     0
4 3 3  3    0     0
5 4 4  4    0     1
6 5 4 NA    0     1

In tidyverse, we may use if_all with if_any for creating those columns

library(dplyr)
df %>%
    mutate(flag1 =  (if_all(everything(),  ~is.na(.)| . %in% 1)), 
            flag2 =  (if_any(x:z, ~ . %in% 4)))
  x y  z flag1 flag2
1 1 1 NA     1     0
2 1 1  1     1     0
3 2 1  1     0     0
4 3 3  3     0     0
5 4 4  4     0     1
6 5 4 NA     0     1

data

df <-structure(list(x = c(1, 1, 2, 3, 4, 5), y = c(1, 1, 1, 3, 4, 
4), z = c(NA, 1, 1, 3, 4, NA)), class = "data.frame", row.names = c(NA, 
-6L))

CodePudding user response:

Here's a version that more verbose than @Akrun's answer (and slower on larger datasets), but more customizable:

flag1 <- ifelse( (x == 1 | is.na(x) ) &
                 (y == 1 | is.na(y) ) &
                 (z == 1 | is.na(z) ), 1, 0)

flag2 <- ifelse( x == 4 | y == 4 | z == 4, 1, 0)

If you had a bunch of these vectors, you could store them in a matrix or data.frame so you don't need to list each column in order to do the calculation:

mat <- cbind(x,y,z)

flag1 <- apply(mat, 1, function(r) sum(r==1 | is.na(r)) == length(r))
flag2 <- apply(mat, 1, function(r) any(r==4, na.rm=T))

CodePudding user response:

Using apply function:

apply(df, 1, function(x)  all(x == 1,na.rm = 1))
[1] 1 1 0 0 0 0
apply(df, 1, function(x)  any(x == 4,na.rm = 1))
[1] 0 0 0 0 1 0

Data used:

df
  x y  z
1 1 1 NA
2 1 1  1
3 2 1  1
4 3 3  3
5 4 4  4
6 5 5 NA

CodePudding user response:

Here is an additional alternative way with pivoting using all and any:

library(tidyr)
library(dplyr)

df %>% 
  pivot_longer(
    cols=everything()
  ) %>% 
  mutate(id = as.integer(gl(n(), 3, n()))) %>% 
  group_by(id) %>% 
  mutate(flag1 = ifelse(all(value == 1, na.rm=TRUE), 1,0),
         flag2 = ifelse(any(value == 4, na.rm=TRUE), 1,0)) %>% 
  pivot_wider(
    names_from = name, 
    values_from = value
  ) %>% 
  ungroup() %>% 
  select(x,y,z,flag1, flag2)

output:

      x     y     z flag1 flag2
  <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     1    NA     1     0
2     1     1     1     1     0
3     2     1     1     0     0
4     3     3     3     0     0
5     4     4     4     0     1
6     5     4    NA     0     1

CodePudding user response:

library(tidyverse)

df = tibble(
  x = c(1, 1, 2, 3, 4, 5),
  y = c(1, 1, 1, 3, 4, 5),
  z = c(NA, 1, 1, 3, 4, NA)
)


df %>% mutate(
  flag1 = ifelse((x==1 | is.na(x)) & (y==1 | is.na(y)) & (z==1 | is.na(z)), 1, 0),
  flaf2 = ifelse((x==4 | is.na(x)) | (y==4 | is.na(y)) | (z==4 | is.na(z)), 1, 0)
)

output

# A tibble: 6 x 5
      x     y     z flag1 flaf2
  <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     1    NA     1     1
2     1     1     1     1     0
3     2     1     1     0     0
4     3     3     3     0     0
5     4     4     4     0     1
6     5     5    NA     0     1

Update 1

Note, you can't forget to decide what to do when all variables are NA. Here is a corrected version of one of the possible solutions.

library(tidyverse)

df = tibble(
  x = c(1, 1, 2, 3, 4, 5, NA),
  y = c(1, 1, 1, 3, 4, 5, NA),
  z = c(NA, 1, 1, 3, 4, NA, NA)
)


df %>% mutate(
  flag1 = ifelse(is.na(x) & is.na(y) & is.na(z), NA, 
                 ifelse((x==1 | is.na(x)) & (y==1 | is.na(y)) & (z==1 | is.na(z)), 1, 0)),
  flag2 = ifelse(is.na(x) & is.na(y) & is.na(z), NA,
                 ifelse((x==4 | is.na(x)) | (y==4 | is.na(y)) | (z==4 | is.na(z)), 1, 0))
)

output

# A tibble: 7 x 5
      x     y     z flag1 flag2
  <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     1    NA     1     1
2     1     1     1     1     0
3     2     1     1     0     0
4     3     3     3     0     0
5     4     4     4     0     1
6     5     5    NA     0     1
7    NA    NA    NA    NA    NA

CodePudding user response:

Here's an option using rowwise and c_across:

library(dplyr)

df %>% 
  rowwise() %>% 
  mutate(flag1 = as.numeric(all(c_across() == 1, na.rm = T)),
         flag2 = as.numeric(any(c_across() == 4, na.rm = T))) %>% 
  ungroup()

c_across will combine each row into an atomic vector for comparison to your condition.

Note: by default c_across works across all columns. You can change this with any tidyselect syntax. For example, x:z.

Output

      x     y     z flag1 flag2
  <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     1    NA     1     0
2     1     1     1     1     0
3     2     1     1     0     0
4     3     3     3     0     0
5     4     4     4     0     1
6     5     4    NA     0     1
  • Related