我有这个数据框,有4列,我需要的是在新列中合并B,C和D列
由于
A B C D
1.40 Fria Moderada NA NA
-1.17 Fria Debil NA NA
-0.85 NA NA Neutro
-0.74 NA NA Neutro
0.58 NA Calida Debil NA
1.29 NA Calida Moderada NA
答案 0 :(得分:3)
包tidyr具有联合功能,可以解决这个问题:
#Sample Data
#dput(d)
d<-structure(list(A = c(1.4, -1.17, -0.85, -0.74, 0.58, 1.29), B = c("Fria Moderada",
"Fria Debil", NA, NA, NA, NA), C = c(NA, NA, NA, NA, "Calida Debil",
"Calida Moderada"), D = c(NA, NA, "Neutro", "Neutro", NA, NA)), .Names = c("A",
"B", "C", "D"), class = "data.frame", row.names = c(NA, -6L))
library(tidyr)
d[is.na(d)]<-"" #removes the NAs
unite(d, newcol, c(B, C, D), sep="")
答案 1 :(得分:2)
如果列的每行只有一个非NA值,那么#B; B&#34;到&#34; D&#34;,我们可以使用pmax
base R
cbind(d[1], newcol=do.call(pmax, c(d[-1], list(na.rm=TRUE))))
# A newcol
#1 1.40 Fria Moderada
#2 -1.17 Fria Debil
#3 -0.85 Neutro
#4 -0.74 Neutro
#5 0.58 Calida Debil
#6 1.29 Calida Moderada
答案 2 :(得分:0)
简单,但有效,还是我错了?
d[is.na(d)]<-"" #removes the NAs (code used by Dave2e)
d$newcol <- paste(d$B,d$C,d$D, sep = "")