My code:
library(dplyr)
df <- tibble(
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10)
)
output <- vector("double", ncol(df)) # 1. output
for (i in seq_along(df)) { # 2. sequence
output[i] <- median(df[i])
}
output
If I put median(df[i])
in the for loop it shows:
Error in median.default(df[i]) : need numeric data
why is that? Why I have to use [[]] here? I thought inside the median function, i just have to call the entire column, which can be done by df[i].