我有以下代码
fig4 <- data.frame(chads=NA,age=NA,treatment=NA,mean=NA,lower=NA,upper=NA)
fig4$chads <- as.factor(fig4$chads)
levels(fig4$chads) <- c(0,1,2,3,4,5,6)
fig4$age <- as.factor(fig4$age)
levels(fig4$age ) <- c("u80","o80")
fig4$treatment <- as.factor(fig4$treatment)
levels(fig4$treatment) <- c("OAC","OAP")
fig4$mean <- as.numeric(fig4$mean)
fig4$lower <- as.numeric(fig4$lower)
fig4$upper <- as.numeric(fig4$upper)
> str(fig4)
'data.frame': 1 obs. of 6 variables:
$ chads : Factor w/ 7 levels "0","1","2","3",..: NA
$ age : Factor w/ 2 levels "u80","o80": NA
$ treatment: Factor w/ 2 levels "OAC","OAP": NA
$ mean : num NA
$ lower : num NA
$ upper : num NA
到现在为止还挺好。但后来我这样做:
vc <- as.vector(c(6,"o80","OAC",0.1,0.02,0.25), mode = "any")
fig4 <- rbind(fig4,vc)
结果是:
> str(fig4)
'data.frame': 2 obs. of 6 variables:
$ chads : Factor w/ 7 levels "0","1","2","3",..: NA 7
$ age : Factor w/ 2 levels "u80","o80": NA 2
$ treatment: Factor w/ 2 levels "OAC","OAP": NA 1
$ mean : chr NA "0.1"
$ lower : chr NA "0.02"
$ upper : chr NA "0.25"
为什么数字向量变成字符向量?