またはで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
何が起こっているか知っている人はいますか?