I have two data frames like the ones below..
df1 <- data.frame(row.names = c(1,2,3,4,5,6), Week = c(1,1,2,2,52,52), NAME = c("Florida", "Georgia","Florida", "Georgia","Florida", "Georgia"), X2001 = c(25,16,83,45,122,132), X2002 = c(3, 78, 22, 5, 166, 103))
df2 <- data.frame(row.names = c(1,2,3,4,5,6), Week = c(1,1,2,2,52,52), NAME = c("Florida", "Georgia","Florida", "Georgia","Florida", "Georgia"), X2001 = c(18,64,38,15,186,101), X2002 = c(15, 89, 16, 1, 137, 100))
I would now like to combine these dataframes into a single 4d array [dataframe (1:2), state (1:50), week (1:52), year (1:20)] where the values in the year columns can be accessed by indexing this array.
Similar to this example.. R - convert data.frame to multi-dimensional matrix
I have started to do this with the following code..
#Set up array to organize state BA data
State.Names <- unique(df1[,"NAME"])
modis.ba <- array(0,c(2,length(State.Names),52,length(2001:2020)))
modis.years <- 2001:2020
modis.weeks <- 1:52
for(landowner in 1:2){
print(landowner)
#Read in MODIS
if(landowner == 1){
ba.modis <- df2
}
if(landowner == 2){
ba.modis <- df1
}
#Populate the state BA array
for(state in State.Names){
To be continued........
I am having trouble figuring out how to complete this for loop to obtain the array that I want. Any easy solutions for this?