Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Undefined columns selected while doing a for loop

I have 13 data frames with the same parameters. For each data frame, I want to extract the column P.Value (only the rows which are smaller than 0.05) and add it to a new vector. Instead of writing the same code 13 times, I want to do a for loop:

for (i in (1:13)) {
  
  MyGenes[i] = df[i][df[i]$P.Value < 0.05,]
  
}

I get this error:

Error in `[.data.frame`(df, i) : undefined columns selected

What am I doing wrong? the names of the data frames are df1, df2, df3, and so on. At the end I want MyGenes1, MyGenes2 …. MyGenes13.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can probably achieve what you want with

for (i in 1:13) {
  
  assign(paste0('MyGenes', i), 
         subset(get(paste0('df', i)), P.Value < 0.05))
}

However, working with a list of data frames (as suggested by wernor) would be more convenient:

# sample data
df.list <- list(data.frame(x=1:5, P.Value=seq(0, .2, length.out=5)),
                data.frame(x=3:7, P.Value=seq(0, .1, length.out=5)))

MyGenes <- lapply(df.list, subset, P.Value < 0.05)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading