el88
el88

Reputation: 59

How to combine two columns in R but keep NA?

I have a data frame with the following columns:

      Genus       Species
1 Somniosus microcephalus
2 Somniosus            NA
3        NA microcephalus
4 Somniosus microcephalus
5        NA            NA

I hope to get one that looks like this:

     Genus       Species                      GS
1 Somniosus microcephalus Somniosus microcephalus
2 Somniosus            NA                      NA
3        NA microcephalus                      NA
4 Somniosus microcephalus Somniosus microcephalus
5        NA            NA                      NA

i.e. I would like to combine the information in the Genus and the Species columns into a new column. However, if there is an NA present in either the Genus or the species column I would like the resulting value to be one NA value. While I understand the logic to solve my problem I'm afraid I do not have enough experience in R yet to come up with the correct syntax.

Upvotes: 2

Views: 45

Answers (2)

SmitM
SmitM

Reputation: 1376

You could directly use the paste function like this:

df$GS <- ifelse(is.na(df$Genus) | is.na(df$Species), NA, paste(df$Genus, df$Species, sep = " "))

Upvotes: 2

Jilber Urbina
Jilber Urbina

Reputation: 61154

Use ifelse like this:

> transform(df1, GS=ifelse(is.na(Genus)| is.na(Species), NA, paste(Genus, Species)))
      Genus       Species                      GS
1 Somniosus microcephalus Somniosus microcephalus
2 Somniosus          <NA>                    <NA>
3      <NA> microcephalus                    <NA>
4 Somniosus microcephalus Somniosus microcephalus
5      <NA>          <NA>                    <NA>

Using complete.cases as suggested by @thelatemail

transform(df1, GS=ifelse(complete.cases(df1), paste(Genus, Species), NA))

Upvotes: 3

Related Questions