1

Q. Write a function called col_median() that takes one argument df and returns the vector of column medians.

col_median <- function(df) {
    output <- vector("double", ncol(df))
    for (i in seq_along(df)) {
        output[[i]] <- median(df[[i]])
    }
    output
}

While working on this problem, I was using the following line of code that doesn’t work. Can someone kindly explain why? output[i] <- median (df[i])

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360

1 Answers1

1

The double bracket selects a single element in a list or data.frame (as data.frame is also a list, so the columns are elements). Here, we are assigning the median of each column of 'df' to an output list. As we are defining a list 'output', it should be

col_median <- function(df) {
 output <- vector("list", ncol(df))
 for (i in seq_along(df)) {
    output[[i]] <- median(df[[i]])
   }
 output
}

col_median(df) #this returns a `list` output

The OP's function returns a vector.

If we are using

 output[i] <- median(df[i])

by defining,

output <- vector("double", ncol(df))

the lhs of <- is fine, but df[i] returns a data.frame and median would not work on that. Instead it should median(df[[i]]) to extract the column as a vector.

data

set.seed(24)
df <- as.data.frame(matrix(sample(1:9, 10*5, replace=TRUE), ncol=5))
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • @thelatemail You are right about that, but I am answering the `[[` part – akrun Jul 20 '16 at 03:22
  • Thanks for the detailed reply. I'm a newbie. Please elaborate more by giving an example if possible, when you say, "...... df[i] returns a data.frame and median would not work on that. Thanks again. – user6210276 Jul 20 '16 at 03:35
  • That's is little hard to follow. Thanks again. – user6210276 Jul 21 '16 at 15:39