How to apply a function on a list of dataframes with a for loop in R?
0
1
Entering edit mode
5.0 years ago
ishackm ▴ 110

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 • 1.3k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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"
ADD REPLY

Login before adding your answer.

Traffic: 1579 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