我的数据如下所示:
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}}功能,但它并没有完全按照我的要求提供给我。有什么想法吗?谢谢!
答案 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)))