If function on several columns

Hello !
I have a question regarding the IF function.

Here is my table, I would like in column D to have the same thing as in column C except that I would like to replace the "Unidentified" by the closest known taxonomic resolution as the example below:

df <- tribble(
  ~A, ~B, ~C, ~D, ,
  Mycto, Unidentified, Unidentified, NA,
  Mact, Prio, Boreo, NA,
  Lui, Bra, Unidentified, NA,
)

Created on 2022-05-06 by the reprex package (v2.0.1)

image

Here for the first line Unidentified (column C) is replaced by Mycto because it is the closest known taxo resolution. On the other hand, for the 3rd line, the closest known taxo resolution is in column B, so Bra.

What would be the formula to enter in column D to have this kind of results?
Thanks very much !

Replace "Unidentified" by NA everywhere and then use coalesce from dplyr package.

e.g.

library(tidyverse)
df <- tribble(
  ~A, ~B, ~C, ~D,
  "Mycto",NA,NA,NA,
  "Mact", "Prio", "Boreo", NA,
  "Lui", "Bra",NA, NA
)
df %>% mutate(D = coalesce(C,B,A))
#> # A tibble: 3 × 4
#>   A     B     C     D    
#>   <chr> <chr> <chr> <chr>
#> 1 Mycto <NA>  <NA>  Mycto
#> 2 Mact  Prio  Boreo Boreo
#> 3 Lui   Bra   <NA>  Bra
Created on 2022-05-06 by the reprex package (v2.0.1)

Great thanks a lot !

I did "df %>% mutate(C = coalesce(C,B,A))" to not have to create the column D !

Thank you
Hersh

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.