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

时间:2018-01-25 02:57:36

标签: r dataframe

我的数据如下所示:

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

我正在尝试将列组合成一个变量,如果它们相同,则结果会产生两列中的结果,如果两者都有数据,则结果是Farm列中的结果,如果两者都是<NA>结果为<NA>,如果其中一个有值而另一个没有,则结果是具有该值的列中存在的值。因此,在这种情况下,结果将是:

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

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

我已经尝试过来自unite的{​​{1}}功能,但它并没有完全按照我的要求提供给我。有什么想法吗?谢谢!

2 个答案:

答案 0 :(得分:3)

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

如果您要放弃原始列,请使用transmute代替mutate

答案 1 :(得分:0)

一个不太简洁的选项是使用几个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)))