Using for loop for statistics by data frame column in R. -
i have been trying make loop takes vector of column names want use , loop statistical tests column determines group of sample. here how looks now.
sink('df_statistics.txt') df <- `df.tsv` columns <- c("column1" , "column2" , "column3" , "column4") (x in columns) { wilcox.test(formula = x ~ group, data = df) } sink()
when run error:
error in model.frame.default(formula = data ~ group, data = df) : variable lengths differ (found 'group')
my groups determined numbers 1 , 2, tried naming them control , experimental keep getting same error above. suggestions?
thank you
we can use lapply
lapply(df[columns], function(x) wilcox.test(x~df$group))
data
columns <- c("column1" , "column2") set.seed(24) df <- data.frame(group = rep(1:2, each=5), column1 = rnorm(10), column2 = rnorm(10))