简体   繁体   English

根据R中的特定条件将两列中的值组合在一起

[英]Combine values in two columns together based specific conditions in R

I have data that looks like the following: 我的数据如下所示:

moo <- data.frame(Farm = c("A","B",NA,NA,"A","B"), 
                  Barn_Yard = c("A","A",NA,"A",NA,"B"), 
                  stringsAsFactors=FALSE)
print(moo)
Farm Barn_Yard
 A         A
 B         A
<NA>      <NA>
<NA>       A
 A       <NA>
 B         B

I am attempting to combine the columns into one variable where if they are the same the results yields what is found in both columns, if both have data the result is what is in the Farm column, if both are <NA> the result is <NA> , and if one has a value and the other doesn't the result is the value present in the column that has the value. 我试图将列组合成一个变量,如果它们是相同的,结果会产生两列中的结果,如果两个数据的结果都是Farm列中的结果,如果两者都是<NA>则结果为<NA> ,如果一个具有值而另一个不具有值,则结果是具有该值的列中存在的值。 Thus, in this instance the result would be: 因此,在这种情况下,结果将是:

oink <- data.frame(Animal_House = c("A","B",NA,"A","A","B"), 
                   stringsAsFactors = FALSE)

print(oink)
Animal_House
        A
        B
     <NA>
        A
        A
        B

I have tried the unite function from tidyr but it doesn't give me exactly what I want. 我试过了tidyrunite功能,但它并没有给我我想要的东西。 Any thoughts? 有什么想法吗? Thanks! 谢谢!

dplyr::coalesce does exactly that, substituting any NA values in the first vector with the value from the second: dplyr::coalesce这样做的,用第二个值替换第一个向量中的任何NA值:

library(dplyr)

moo <- data.frame(Farm = c("A","B",NA,NA,"A","B"), 
                  Barn_Yard = c("A","A",NA,"A",NA,"B"), 
                  stringsAsFactors = FALSE)

oink <- moo %>% mutate(Animal_House = coalesce(Farm, Barn_Yard))

oink
#>   Farm Barn_Yard Animal_House
#> 1    A         A            A
#> 2    B         A            B
#> 3 <NA>      <NA>         <NA>
#> 4 <NA>         A            A
#> 5    A      <NA>            A
#> 6    B         B            B

If you want to discard the original columns, use transmute instead of mutate . 如果要放弃原始列,请使用transmute而不是mutate

A less succinct option is to use a couple ifelse() statements, but this could be useful if you wish to introduce another condition or column into the mix. 一个不太简洁的选项是使用几个ifelse()语句,但如果您希望在混合中引入另一个条件或列,这可能很有用。

moo <- data.frame(Farm = c("A","B",NA,NA,"A","B"), 
                  Barn_Yard = c("A","A",NA,"A",NA,"B"), 
                  stringsAsFactors = FALSE)

moo$Animal_House = with(moo,ifelse(is.na(Farm) & is.na(Barn_Yard),NA,
                                   ifelse(!is.na(Barn_Yard) & is.na(Farm),Barn_Yard,
                                          Farm)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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