向量中的混合类型(没有类型转换的 rbind 数据框)

Mixed type in vector (rbind dataframe without typeconversion)

在 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.

一定有更简单/更好的方法吗?

在您的 rbind 中使用 list 对象,例如:

test2 <- rbind(test, c("All", unname(as.list(tmpSums)), "Mixed"))

rbind 的第二个参数是一个列表,删除了会导致 rbind 失败的冲突名称:

c("All", unname(as.list(tmpSums)), "Mixed")
#[[1]]
#[1] "All"
# 
#[[2]]
#[1] 37.70092
#
#[[3]]
#[1] 91.82716
#
#[[4]]
#[1] "Mixed"

这是一个使用 data.table 的选项。我们将 'data.frame' 转换为 'data.table' (setDT(test)),使用 lapply 获得数值列的 sum,将 (c) 与应代表其他列的值,将其放在 list 中并使用 rbindlist

library(data.table)
rAll <-  setDT(test)[, c(name="All", lapply(.SD, sum), 
              Status="Mixed"), .SDcols= val1:val2]
rbindlist(list(test, rAll))

如果我们需要让它更自动化一点,

i1 <- sapply(test, is.numeric)
v1 <- setNames(list("All", "Mixed"), setdiff(names(test),
                      names(test)[i1]))
rAll <-  setDT(test)[, c(v1, lapply(.SD, sum)), 
                 .SDcols=i1][, names(test), with=FALSE]
rbindlist(list(test, rAll))