Home > OS >  How to select the specific rows using dplyr package in R?
How to select the specific rows using dplyr package in R?

Time:06-28

How to select the specific rows using dplyr package in R? If column1 is NA, I want to get the value of column2, and if column2 is NA, I want to get the value of column1.

(Sample code)

test_data %>% 
  select(Column1, Column2) %>% 
  **?????**

(Example)

test_data

(Column1)|(Column2)
NA|20
NA|30
10|NA
40|NA

result

(Column)
      20
      30
      10
      40

CodePudding user response:

The function you are looking for is coalesce.

library(dplyr)

test_data %>% select(col1, col2) %>% transmute(col3 = coalesce(col1, col2))

  col3
1   20
2   30
3   10
4   40

Data

structure(list(col1 = c(NA, NA, 10L, 40L), col2 = c(20L, 30L, 
NA, NA)), class = "data.frame", row.names = c(NA, -4L))

CodePudding user response:

An alternative would be to use pmin:

library(dplyr)
df %>% 
  transmute(col3  = pmin(col1, col2, na.rm = T))

#   col3
# 1   20
# 2   30
# 3   10
# 4   40

structure(list(col1 = c(NA, NA, 10L, 40L), col2 = c(20L, 30L, 
NA, NA)), class = "data.frame", row.names = c(NA, -4L)) -> df
  • Related