1

I have the following code example:

library(readxl)
library(tidyverse)
N <- 5 #number of datasets to pull data from
ind <- c("2010", "2011", "2012", "2013", "2015")

A <- array(rep(1, 91*144*5), dim=c(91,144,5))

for (k in seq_along(ind)) {
   A <- read_excel(paste0("~R/data", ind[k], ".xlsx"), range="B3:EO94")
}

I know that this is not reproducible due to inability to upload data. The above code captures my thought process in what I am trying to do. I want to create a matrix A(i,j,k) where i and j are rows and columns, respectively, from the k excel files. So in my case, k is the years in ind. I have those 5 excel files and they have identical dimensions.

What I want out of this process is the ability to take a particular set of i,j and create a new vector that has a length=k, although this is not the question I am asking here. For context, i and j are longitude and latitude and k is year.

14
  • 1
    If you add a minimal reproducible exampleits way easier for others to find a solution to your problem. By defintion,a MRE is not putting your whole code and database in the question but creating an example with as little code and data as needed to replicate the problem! The MRE will make it easier for others to find and test a answer to your question. Commented Mar 4, 2020 at 21:48
  • This is honestly all I have at this point, I have no idea what to do next Commented Mar 4, 2020 at 21:59
  • if all entires are of the same class you could make an array with 3 dimensions. Commented Mar 4, 2020 at 22:07
  • @GordonShumway that is exactly what I am trying to do I just don't know how to Commented Mar 4, 2020 at 22:23
  • 1
    @Brennan I'm not sure what's going on in your loop. You are assigning k a string each time but not using it in the loop (your file name is a static string). Also, you are just overwriting A each time, when you want A to be a 3D array receiving a different 2D slice in each cycle of the loop. So you should declare A as a 91 * 150(ish) * 5 array ahead of the loop, change the for to for(k in seq_along(ind)), change A to A[,,k] and "~R/data[k]" to paste0("~R/data", ind[k]) Commented Mar 4, 2020 at 22:42

1 Answer 1

1

It is possible to write the sheets to a list, then create an array from the unlisted contents:

library(readxl)

ind <- c("2010", "2011", "2012", "2013", "2015")
A <- list()

for (k in seq_along(ind)) {
   A[k] <- read_excel(paste0("~R/data", ind[k], ".xlsx"), range="B3:EO94")
}

result <- array(unlist(A), dim=c(91,144,5))
Sign up to request clarification or add additional context in comments.

2 Comments

Follow-up question: Will the A[k] import the excel files with i rows and j columns? It does not seem to properly match up with the excel file
Disregard my comment, I will ask a new question as it is more involved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.