0

当我使用ifelse函数时,is.table或者is.matrix我得到不需要的结果。而如果我使用一个if语句,我会得到我想要的结果。下面的例子。

x <- table(state.division, state.region)

colSums(x) ## What I am supposed to get in the code below
rowSums(x) ## Same

ifelse(is.table(x), colSums(x), 0) ## Get only the first element of colSums(x)
ifelse(is.matrix(x), apply(x, 2, sum), 0) ## Tried with apply instead but same results
ifelse(is.matrix(x), rowSums(x), 0) ## Same for rowSums
ifelse(is.table(x), apply(x, 1, sum), 0)

if (is.table(x)) colSums(x) ## All fine

有人知道发生了什么吗?

4

0 回答 0