[SOLVED] Why my function always mistakenly returns a transposed matrix as output?

Issue

I am basically a beginner in R and my question most likely has a very basic answer but after many tries I just can’t figure out the reason why my function always returns a transposed output. Any help is much appreciated. I am using the Rstudio IDE, R 4.1.2.

My goal was to write a function to add two additional columns to any given dataframe, which correspond to mean and maximum of each row, respectively.

So I created a sample dataframe:

a <- c(1:10)
b <- c(11:20)
c <- c(21:30)
df <- data.frame(a,b,c)

Then I wrote the following function:

desc_n <- function(x){
  return(cbind(
                mean(x, na.rm=T),
                max(x,na.rm=T))
              )
}

After that I use the apply() function to apply my newly defined desc_n function to every row of the df dataframe.

n_df <- apply(df, 1, desc_n)

However, while the output is correct mathematically, it is transposed. Output:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   11   12   13   14   15   16   17   18   19    20
[2,]   21   22   23   24   25   26   27   28   29    30

While I expected it to be 10 rows and 2 columns. No matter what I do, the output remains the same. I know how to transpose the output to my preference, but this is mostly a learning example for me.
So I have 2 questions:

  1. Why does the desc_n function returns a [2,10] output while I used cbind in it? Shouldn’t it return a [10,2] output?
  2. Why does changing the cbind in the desc_n function to rbind returns the exact same results?

Again, any help is appreciated. Thank you.

Solution

Your result is assembled by apply, and it’s apply that shapes the output (using, of course, your desc_n function along the way).

From the ?apply help page:

If each call to FUN returns a vector of length n, and simplify is TRUE, then apply returns an array of dimension c(n, dim(X)[MARGIN])

Whichever way you bind it, your desc_n function returns something of length 2, so you get 2 rows in the apply-simplified output.

Answered By – Gregor Thomas

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.