我有一个专栏:
id father mother sex birthdate.x birthdate.y
1 john 0 0 1 <NA> NA
2 mari 0 0 2 01/01/1996 NA
3 paul 0 0 2 <NA> 01/01/1995
4 ralf 0 0 2 <NA> NA
5 kimb 0 0 2 01/01/1997 NA
每次我使用它来&#34;合并&#34; birthdate.x
和birthdate.y
,它给了我一个简单的数字。
df1$birthdate.x <- ifelse(is.na(df1$birthdate.y), df1$birthdate.x, df1$birthdate.y)
df1$birthdate.y <- NULL
names(df1)[names(df1)=="birthdate.x"] <- "birthdate"
id father mother sex birthdate
1 john 0 0 1 NA
2 mari 0 0 2 2
3 paul 0 0 2 1
4 ralf 0 0 2 NA
5 kimb 0 0 2 3
答案 0 :(得分:1)
我们可以使用pmax
transform(df1, birthdate = pmax(as.character(birthdate.x),
as.character(birthdate.y), na.rm = TRUE))[-(5:6)]
# id father mother sex birthdate
#1 john 0 0 1 <NA>
#2 mari 0 0 2 01/01/1996
#3 paul 0 0 2 01/01/1995
#4 ralf 0 0 2 <NA>
#5 kimb 0 0 2 01/01/1997