ILoveSAS94
ILoveSAS94

Reputation: 1

How to loop over a specific set of columns names in a dataframe with the use of a vector?

I've been struggling with this problem for days and I'm rather new to R. So I give up and I hope anyone of you can help me.

I want to compute summary statistics of a specific variable grouped by different variables which I want to loop. I don't want to copy-paste my syntax and change the grouping variable every time. I used a for loop and lapply with the use of vector (where my different grouping variables are stored).

I think the problem is that my dataframe cannot find the column names I stored in my vector.

My code looks something likes this:

snp_EPA <- c('rs3798713_C', 'rs174550_C', 'rs174574_A', 'rs174448_C') #Vector of grouping variables

for (i in snp_EPA) {
FA %>% group_by(as.name(i)) %>% summarise(FA, bce_c20_5n_3)
} #For loop I tried, didn't work

epa <- lapply(snp_EPA, function(x) {describeBy(FA$bce_c20_5n_3, as.name(x))})
lapply(epa, print) #lapply function I used, still didn't work....

Upvotes: 0

Views: 60

Answers (1)

dcarlson
dcarlson

Reputation: 11046

We really need more information about your data and a small sample using dput(data). I can show you a couple of ways to get what you want that might get you started. I'll use the iris data set that comes with R:

data(iris)
str(iris)
# 'data.frame': 150 obs. of  5 variables:
#  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
# $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

The data set consists of 4 measurements on three different species of iris. One simple way to get descriptive statistics is to use split and summary:

iris.split <- split(iris, iris$Species)
lapply(iris.split, summary)
# $setosa
#  Sepal.Length    Sepal.Width     Petal.Length    Petal.Width          Species  
#  Min.   :4.300   Min.   :2.300   Min.   :1.000   Min.   :0.100   setosa    :50  
#  1st Qu.:4.800   1st Qu.:3.200   1st Qu.:1.400   1st Qu.:0.200   versicolor: 0  
#  Median :5.000   Median :3.400   Median :1.500   Median :0.200   virginica : 0  
#  Mean   :5.006   Mean   :3.428   Mean   :1.462   Mean   :0.246                  
#  3rd Qu.:5.200   3rd Qu.:3.675   3rd Qu.:1.575   3rd Qu.:0.300                  
#  Max.   :5.800   Max.   :4.400   Max.   :1.900   Max.   :0.600           
# . . . results for other 3 measurements

Another approach is to use a summary statistics functions that will group the data for you. The numSummary function in package RcmdrMisc is one of many possiblities:

library(RcmdrMisc)   # You will have to install it the first time with `install.packages("RcmdrMisc)`.
numSummary(iris[, -5], groups=iris$Species)
# 
# Variable: Sepal.Length 
#             mean        sd   IQR  0%   25% 50% 75% 100%  n
# setosa     5.006 0.3524897 0.400 4.3 4.800 5.0 5.2  5.8 50
# versicolor 5.936 0.5161711 0.700 4.9 5.600 5.9 6.3  7.0 50
# virginica  6.588 0.6358796 0.675 4.9 6.225 6.5 6.9  7.9 50 
# . . . results for three other measurements.

These examples use all of the numeric columns, but you can select only some columns with iris[, 1:3] to get just the first three or iris[, c(1,4)] to get the the first and the fourth columns.

Upvotes: 1

Related Questions