简体   繁体   中英

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

ie 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.

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))

您可以像这样直接使用粘贴功能:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM