What you specify here in code is:
lapply(data, varnames, name=names(data))
loop over the columns of data
, and provides an additional variable name
to the function varnames
. Note that here the code does not iterate over name
, but passes it as is.
If you want to get the output you require, you can use mapply
. This is essentially a version of lapply
which allows multiple variables to be iterated over:
mapply(varnames, data, names(data))
var1 var2
"var1" "var2"
To do some operations to each of the columns, and still keep the name in some way, I would use dplyr
(as you are not entirely clear what your end goal is, I'm guessing this is what your where after):
library(dplyr)
data = data.frame(var1 = runif(10), var2 = runif(10),
var3 = runif(10), var4 = runif(10))
data %>% summarise_each(funs(mean, sd, median), var1:var4)
var1_mean var2_mean var3_mean var4_mean var1_sd var2_sd var3_sd
1 0.6063735 0.5308427 0.2872901 0.6043027 0.2586042 0.2303065 0.245709
var4_sd var1_median var2_median var3_median var4_median
1 0.2721362 0.6814136 0.4535982 0.200493 0.644607
alternatively using gather
from tidyr
:
data %>%
gather(variable, value) %>%
group_by(variable) %>%
summarise_each(funs(mean, sd, median), value)
Source: local data frame [4 x 4]
variable mean sd median
(fctr) (dbl) (dbl) (dbl)
1 var1 0.6063735 0.2586042 0.6814136
2 var2 0.5308427 0.2303065 0.4535982
3 var3 0.2872901 0.2457090 0.2004930
4 var4 0.6043027 0.2721362 0.6446070