2

My program is passed a list mylist of several dataframes with arbitrary names:

FOO  12     tbl_df list
BAR  12     tbl_df list
...

Each dataframe has the same structure:

variableX    variableY    SUBJECT_ID     SUBJECT_YEAR          SUBJECT_TOWN
2            1            A              1950                  Townsville
1            2            B              1951                  Villestown
...

I need to iterate over mylist, getting for each dataframe one plot of the mean of variableX per SUBJECT_YEAR and SUBJECT_TOWN to be exported as a uniquely named image file (e.g. FOO_SUBJECTOWN.png).

I know that R can handle this kind of task very well and there are good examples on SO of this task performed with a for-loop. However, because I am new to functions in R, I would like to know how this task could be performed efficiently using a function.

fhng
  • 123
  • 4
  • Please read [(1)](http://stackoverflow.com/help/how-to-ask) how do I ask a good question, [(2)](http://stackoverflow.com/help/mcve) How to create a MCVE as well as [(3)](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610) how to provide a minimal reproducible example in R. Then edit and improve your question accordingly. I.e., abstract from your real problem... – Christoph Nov 26 '17 at 00:06
  • Do you have a list of dataframes? – Homunculus Nov 26 '17 at 00:58

1 Answers1

0

This should work

#cast them to characters before proceeding, ok? 
unique.years = unique(dataframe$SUBJECT_YEAR)
unique.town = unique(dataframe$SUBJECT_TOWN)

mean.years = c()
mean.town = c()

for(i in 1:length(unique.years))
{
    mean.years [i] = mean( dataframe[ dataframe[,"SUBJECT_YEAR"] == unique.years[i]  ,"variableX" ] )
}


for(i in 1:length(unique.town))
{
    mean.town [i] = mean( dataframe[ dataframe[,"SUBJECT_TOWN"] == unique.years[i]  ,"variableX" ] )
}

jpeg('years.jpg')
plot(x = unique.years, y = mean.years)
dev.off()

jpeg('town.jpg')
plot(x = unique.town, y = mean.town)
dev.off()

You can later change the unique variable for any unique you desire. Also, adding title and legends in the plot, maybe some colors, would be something nice.

If it is a list of dataframes, just replace dataframe for myList[[j]], in a loop, as demonstrated bellow

for(j in 1:length(myList))
}
    #cast them to characters before proceeding, ok? 
    unique.years = unique(myList[[j]]$SUBJECT_YEAR)
    unique.town = unique(mylist[[j]]$SUBJECT_TOWN)

    mean.years = c()
    mean.town = c()

    for(i in 1:length(unique.years))
    {
        mean.years [i] = mean( myList[[j]] [ myList[[j]][,"SUBJECT_YEAR"] == unique.years[i]  ,"variableX" ] )
    }


    for(i in 1:length(unique.town))
    {
        mean.town [i] = mean( myList[[j]][ myList[[j]][,"SUBJECT_TOWN"] == unique.years[i]  ,"variableX" ] )
    }

    jpeg(paste0("years",j,".jpg")
    plot(x = unique.years, y = mean.years)
    dev.off()

    jpeg(paste0("town",j,".jpg")
    plot(x = unique.town, y = mean.town)
    dev.off()
}
Homunculus
  • 329
  • 2
  • 12