-1

I used an lapply function twice to analyse the data of passed students and failed students of 30 schools across class1-5. Using a Split function two dataframes has been split based on school names. Now, I need to see passed students and failed students of a school(school-wise) list in a single file.

Here's my code:

spt1 <-split(pass, pass$school) 
# ^ result1 is a dataframe, splitting school wise in pass df

abc=lapply(names(spt1), function(x){write.table(spt1[[x]], 
  file = paste("C:/Users/Documents/Output/Pass", x, ".csv"), row.names = FALSE ,sep = "," )}) 
# ^ for loop to seggregate passed list across all schools

spt2 <-split(fail, fail$school) 
# ^ splitting failur list in the dataframe based on schools

def=lapply(names(spt2), function(x){write.table(spt2[[x]],  
  file = paste("C:/Users/Documents/Output/Fail", x, ".csv"), row.names = FALSE , sep = ",",)}) 
# ^ for loop to seggregate failure list across all schools

Now, I would like to see the data of passed and failure students of a school in a single pdf or csv. The expected output would be 30pdf for 30schools that each pdf contains list of passed and failed students

Note: Columns are different for all the files across two folders.

Frank
  • 66,179
  • 8
  • 96
  • 180
Ram
  • 185
  • 3
  • 12

1 Answers1

1

It would be nice to have some sample data. Would this give you an idea of a possible solution?

install.packages('gridExtra')

require(gridExtra)

for(i in 1:length(names(spt1))){
    pdf(file = "test.pdf", width=28, height=21, paper='a4')


   data1 <- spt1[[i]] 
   loops <- ceiling(nrow(data1)/40)    
      for(j in 1:loops){
          from <- max(1,(j-1)*40)
          to   <- min(nrow(data1),j*40)
          grid.table(data1[from:to,], show.rownames = FALSE, gpar.coretext=gpar(fontsize=8) )
          grid.newpage()    
       }


    grid.newpage()

   data2 <- spt2[[i]] 
   loops <- ceiling(nrow(data2)/40)    
      for(j in 1:loops){
          from <- max(1,(j-1)*40)
          to   <- min(nrow(data2),j*40)
          grid.table(data2[from:to,], show.rownames = FALSE, gpar.coretext=gpar(fontsize=8) )
          grid.newpage()    
       }
    dev.off()

}

my code works using the gridExtra package and writes your table into a pdf-document. you may add text and headings according to the package help...

of course there are also solutions using LaTeX... maybe you want to consult this question.

EDITS:

  • Added another loop in order to fit the talbe-chunks on one page
  • Edited the pdf-function in order to get a4 paper
Community
  • 1
  • 1
Seb
  • 5,417
  • 7
  • 31
  • 50
  • Thanks for your response. I need to add two dataframes spt1 and spt2 that contains 30 files across each dataframe, and should see in the pdf document – Ram Jan 22 '16 at 06:23
  • It seems I am a little slow to understand but can you please describe a little more in detail: would you like to combine the two `data.frames` and write them into the pdf? What do you mean by "contains 30 files across each dataframe"? – Seb Jan 22 '16 at 07:20
  • Yes. You can see the above code , I applied two for loops.Eor each for loop i get output as 30 files, Now, I would like to see adding all the 30 files to other 30files in other loop and write them into pdf – Ram Jan 22 '16 at 07:31
  • the code i presented does write the exact same `data.frames` to a pdf. there is no need to re-load all those 30 files and then write them into a pdf as far as I understand. – Seb Jan 22 '16 at 09:14
  • Yeah, But in a dataframe I have 130 rows, by using your code I could only see 26rows as the output – Ram Jan 22 '16 at 09:55
  • I edited the aswer in order to fit 130 rows. The device now breaks after 40 rows and then starts a new page for the next part of the data.frame... next time you ask a question pls provide sample data! – Seb Jan 22 '16 at 14:24
  • Yeah that's fine. I installed gridExtra package,I tried executing it, I received an error as couldn't find the function grid.newpage() – Ram Jan 23 '16 at 11:44
  • 1
    @Ram you might want to improve your question by including the error message. Furthermore you may need to check if this issue has been handled on stackoverflow before. If not, open a new question. Without additional information I cannot help you. – Seb Jan 25 '16 at 12:33