'R - using functions of 'apply' family to get a list of calls to read.xlsx

I tried using mapply to create a list of data frames of elements from sheets in an Excel file.

To be precise, every column of the data table I want to create as one element of the list is a column of a separate sheet in the Excel file. There are 7 files; they have differing numbers of sheets though each sheet has the same dimensions. Each element of my final list, which I call RAINS, should refer to one of 7 files.

### Excel files


weather_files <- list()

weather_files <- list.files(pattern = "[M-m][0-9]{4}\\.xlsx")


year = c(1:7)
dateseq = c(5:26)

rainsheet <- list()
RAIN <- list()
RAINS <- list()

## List of vectors of sheet numbers for each file 

for (i in seq_along(years)) {
    y[[i]] <- c(5:length(excel_sheets(weather_files[i])))
}

### Function 'raindate' which calls read.xlsx

raindate <- function(j,i) {
rainsheet <- read.xlsx(weather_files[i],sheet=j,startRow=2,colNames=TRUE,rowNames=FALSE,detectDates=FALSE,rows=c(4:108),cols=c(2),check.names=FALSE)
}

### Create data frame using cbind

for (i in seq_along(year)) {
        RAIN <- read.xlsx(weather_files[1],sheet=5,startRow=2,colNames=TRUE,rowNames=FALSE,detectDates=FALSE,rows=c(4:108),cols=c(1),check.names=FALSE)
        RAINS[[i]] <- cbind(RAIN,mapply(raindate,y[[i]],year))
    }

The problem I have is that mapply, increments on pairs of elements of the vectors 'y' and 'year'. This gives me data frames where each successive column increments the excel file and the sheet, completely mixing up the data. What I need is incrementing over all values of y within one year, then incrememting y.

Is there a method in R to replace mapply in the above code to achieve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source