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

And*_*eas 6 types r dataframe rbind

在 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.
Run Code Online (Sandbox Code Playgroud)

必须有更简单/更好的方法吗?

the*_*ail 6

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

test2 <- rbind(test, c("All", unname(as.list(tmpSums)), "Mixed"))
Run Code Online (Sandbox Code Playgroud)

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

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