how to loop through various dataframes in R
0
0
Entering edit mode
23 months ago
rheab1230 ▴ 140

I have 49 data frames and one matrix file which has NA values initially and will be filled up based on a condition applied on the 49 data frame. The condition are's: First check if the respective gene is present in the data frame. if yes, then check the rsid if that rsid is present for that particular gene then take it weight value and add to the matrix file otherwise it should be 0

I am able to fill the matrix for individual data frame but not able to understand how to do it through loop so that I can analyse all the data frames at one go.

y <- en_Adipose_Subcutaneous$gene==genes_union[1]
z <- which(y[y])
a <- en_Adipose_Subcutaneous[z,]
weightsval <- ifelse(rownames(models_df)[1:1657598]%in% a$rsid,a$weight,0)
models_df[1:1657598,1] <- weightsval

y <- en_Adipose_Visceral_omentum$gene==genes_union[1]
z <- which(y[y])
a <- en_Adipose_Visceral_omentum[z,]
weightsval <- ifelse(colnames(models_df)[1:1657598]%in% a$rsid,a$weight,0)
models_df[1:1657598,2] <- weightsval

My data:

en_Adipose_Visceral_omentum <- data.frame(gene=c("ENSG00000107937.18","ENSG00000107937.18"),rsid=c("rs7475652","rs7918643"),varID=c("chr10_295356_T_C_b38","chr10_301812_C_T_b38"),ref_allele=c("T","C"),eff_allele=c("C","T"),weight=c(0.004,-0.00735))

dput(mod1els_df)
structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA), .Dim = c(3L,
3L), .Dimnames = list(c("rs11252127", "rs11252546", "rs11591988"
), c("en_Adipose_Subcutaneous", "en_Adipose_Visceral_Omentum",
"en_Adrenal_Gland")))
dataframe R • 1.2k views
ADD COMMENT
3
Entering edit mode

Your 49 data frames should be in a list, then you can use functions like lapply or purrr::map to iterate over all of the data frames.

ADD REPLY
2
Entering edit mode

As rpolicastro suggested put all your dataframes into a list: myDFs <- mget(ls(pattern = "^en_"))

ADD REPLY

Login before adding your answer.

Traffic: 2939 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6