简体   繁体   English

rowSums在一列data.frame上中断,原因是在library(reshape)中进行强制转换生成了-为什么?

[英]rowSums breaks on one-column data.frame generated by cast in library(reshape) — why?

Here's some code -- I don't understand why rowSums breaks when it does, and why changing the class of test3 fixes it. 这是一些代码-我不明白为什么rowSums这样做时会中断,以及为什么更改test3的类可以解决该问题。 This seems bizarre. 这看起来很奇怪。 Is it a bug? 是虫子吗? If not, what am I missing? 如果没有,我想念什么?

library(reshape)

test <- data.frame(x=1:5)
rowSums(test)  # Works
class(test)  # "data.frame"

## Trying to break rowSums; see below
class(test) <- c("data.frame", "cast_df")
rowSums(test)  # Works, but see below

vars <- c("x", "y", "z")
test2 <- data.frame(label=rep(c("A", "B"), c(3, 3)),
                    variable=rep(vars, 2),
                    value=1:6)

test2.cast <- cast(test2, label ~ variable, value="value")

rowSums(test2.cast[, vars])  # Works
rowSums(test2.cast[, "y"])  # Breaks because of drop=TRUE

test3 <- test2.cast[, "y", drop=FALSE]
rowSums(test3)  # Why does this break?
class(test3)  # c("cast_df", "data.frame")
class(test3) <- "data.frame"
rowSums(test3)  # Works!  Why?

Context: I have a large data frame generated by cast. 上下文:我有一个由Cast生成的大数据框。 I then programmatically call rowSums on several different subsets of columns of that data frame -- and I noticed this behavior when the subset had only one column. 然后,我以编程方式在该数据帧的列的几个不同子集上调用rowSums,并且当子集只有一个列时,我注意到了这一行为。 This is a small reproducible example. 这是一个可重现的小例子。

rowSums calls as.matrix . rowSums调用as.matrix The first two lines of as.matrix.cast_df are: as.matrix.cast_df的前两行是:

ids <- attr(x, "idvars")
mat <- as.matrix.data.frame(x[, setdiff(names(x), ids)])

Note the lack of drop = TRUE there. 注意那里缺少drop = TRUE When you reset the class to just data.frame this method isn't called, instead the regular as.matrix.data.frame method is called directly, and there's no problem. 当您将类重置为data.frame不会调用此方法,而是直接调用常规的as.matrix.data.frame方法,这没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM