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
.
CodePudding user response:
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)