I\'m calling apply(data, 2, function(x) {...}) where data is a matrix. Is there any way for the function to know the name of the column whose valu
apply(data, 2, function(x) {...})
data
You can use sapply on colnames(data):
sapply
colnames(data)
set.seed(21) Data <- matrix(rnorm(10),5,2) colnames(Data) <- c("one","two") sapply(colnames(Data), function(x) sum(Data[,x])) # one two # 3.987540 -2.010875 colSums(Data) # one two # 3.987540 -2.010875