This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to apply a function on a list of dataframes with a for loop in R?

Hi everyone,

I have this list of data frames:

$ABI3BP
       Gene.Name                    TGEClass           peptide     ID
150588    ABI3BP    TGEClass=5prime_extended        IYLSDSLTGK 150588
150589    ABI3BP    TGEClass=5prime_extended         VSEPVSAGR 150589
150590    ABI3BP    TGEClass=5prime_extended IQSTYDQDHTVPAYVPR 150590
150591    ABI3BP    TGEClass=5prime_extended        DNVEGGIWSK 150591

I have a data frame like this for 91 different genes.

I want to apply this function on every data frame and export each one as a csv

What I have done so far:

tge1<-split (data, data$Gene.Name)  # split each gene name into seperate df

library(reshape2)

for(i in tge1)
{
dcast(tge1[[i]],ID  ~ TGEClass , value.var="peptide")
print(i)
}

Error:

Error in tge1[[i]] : invalid subscript type 'list'

How can fix this please?, I have tried to solve this problem by searching but I haven't got the solution yet so any help will be greatly appreciated.

Thanks

for loop r export csv

Try changing from for(i in tge1) to for (i in names(tge1)).

Thanks for replying, that removed the error but how to apply the function for each element in tge1 list and export each one as csv please?

The output when I changed to names(tge1)

[1] "ABI3BP"
[1] "AEBP1"
[1] "AGRN"

0 answers

No answers yet.

Log in to answer this question.