[Solved] R function calculating statistic of variable column name [duplicate]


You seem to be asking how to use a column name passed as an argument to a function??

myfunction <- function(df, col) mean(df[,col], na.rm=T)

# test
set.seed(1)
df <- data.frame(x=rnorm(10),y=rnorm(10))
myfunction(df,"x")
# [1] 0.1322028

This also works if you pass a column number.

myfunction(df,1)
# [1] 0.1322028

1

solved R function calculating statistic of variable column name [duplicate]