When I use ifelse
function with is.table
or is.matrix
I get unwanted results. Whereas if I use an if
statement I get the result I want. Example below.
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
Anybody know what's going on?
Aucun commentaire:
Enregistrer un commentaire