【发布时间】:2016-06-06 03:51:17
【问题描述】:
在 R 中,向量不能包含不同的类型。一切都必须例如是整数或一切都必须是字符等。这有时让我头疼。例如。当我想为 data.frame 添加边距时,需要一些列是数字,而其他列是字符。
下面是一个可重现的例子:
# dummy data.frame
set.seed(42)
test <- data.frame("name"=sample(letters[1:4], 10, replace=TRUE),
"val1" = runif(10,2,5),
"val2"=rnorm(10,10,5),
"Status"=sample(c("In progres", "Done"), 10, replace=TRUE),
stringsAsFactors = FALSE)
# check that e.g. "val1" is indeed numeric
is.numeric(test$val1)
# TRUE
# create coloumn sums for my margin.
tmpSums <- colSums(test[,c(2:3)])
# Are the sums numeric?
is.numeric(tmpSums[1])
#TRUE
# So add the margin
test2 <- rbind(test, c("All", tmpSums, "Mixed"))
# is it numeric
is.numeric(test2$val1)
#FALSE
# DAMN. Because the vector `c("All", tmpSums, "Mixed")` contains strings
# the whole vector is forced to be a string. And when doing the rbind
# the orginal data.frame is forced to a new type also
# my current workaround is to convert back to numeric
# but this seems convoluted, back and forward.
valColoumns <- grepl("val", names(test2))
test2[,valColoumns] <- apply(test2[,valColoumns],2, function(x) as.numeric(x))
is.numeric(test2$val1)
# finally. It works.
一定有更简单/更好的方法吗?
【问题讨论】:
-
投反对票有点苛刻,不是吗?存在可重现的示例和 OP 的修复尝试。