Reputation: 217
I need to create 24 variables with their corresponding data frames for 24 chromosomes and I feel that writing 24 similar lines is not efficient. Still can't figure out how to write the correct for-loop to solve this.
Here is what I've tried:
for (i in c(1:22,'X','Y')){
Chr[i] <- merge(split[['chr[i]']],split2[['chr[i]']],by='Gene_Name')
}
or
for (i in c(1:22,'X','Y')){
Chri <- merge(split[['chri']],split2[['chri']],by='Gene_Name')
}
Can anyone help me correct my code to generate data frames/variables Chr1, Chr2,...ChrY?
Here is the snapshot of part of the data frame I hope to get.
Upvotes: 0
Views: 274
Reputation: 546123
It’s not entirely clear what you need the data for but in general the solution to your problem in R is via lists (which you seem to be already using in split
and split2
!).
For instance, you can create a list of dataframes for your chromosomes:
chr = list()
for (i in paste0('chr', c(1 : 22, 'X', 'Y'))) {
chr[[i]] <- merge(split[[i]], split2[[i]], by = 'Gene_Name')
}
Or, in a more R-like way (avoiding the for
loop):
chrnames = paste0('chr', c(1 : 22, 'X', 'Y'))
chr = lapply(chrnames, function (chr) merge(split[[i]], split2[[i]], by = 'Gene_Name'))
Better yet, Bioconductor has extensive functionality for working with such data via the GenomicRanges package.
Upvotes: 1
Reputation: 102790
I am not sure if this is the thing you are after. The following code can generate empty variables to your global environment
varNames <- paste0("Chr",c(1:22,"X","Y"))
list2env(setNames(vector("list",length(varNames)),varNames),envir = .GlobalEnv)
Upvotes: 0