John Smith
John Smith

Reputation: 1

R - Adding calculated values to a matrix

I'm trying to figure out how to add calculated mean values from a large dataset and represent it in a 49681 X 25 matrix in R. The 49,681 represents specific locations while the 25 represents years. The code I've written below is a for loop (starting at 1 and go until 49,681) to calculate the mean yearly value of the data for all locations. There are 24 observations made in a year. Also, each data value is anywhere between 0 and 1000. So, the mean value of 1 location in a given year could be 568. Another location in a given year could be 346.

for(I in 1:49681){

mean(as.numeric(veg.data[i,4:27])) # NDVI averages for 1982 (All sites) mean(as.numeric(veg.data[i,28:51])) # NDVI averages for 1983 (All sites) mean(as.numeric(veg.data[i,52:75])) # NDVI averages for 1984 (All sites) mean(as.numeric(veg.data[i,76:99])) # NDVI averages for 1985 (All sites) mean(as.numeric(veg.data[i,100:123])) # NDVI averages for 1986 (All sites) mean(as.numeric(veg.data[i,124:147])) # NDVI averages for 1987 (All sites) mean(as.numeric(veg.data[i,148:171])) # NDVI averages for 1988 (All sites) mean(as.numeric(veg.data[i,172:195])) # NDVI averages for 1989 (All sites) mean(as.numeric(veg.data[i,196:219])) # NDVI averages for 1990 (All sites) mean(as.numeric(veg.data[I,220:243])) # NDVI averages for 1991 (All sites) mean(as.numeric(veg.data[i,244:267])) # NDVI averages for 1992 (All sites) mean(as.numeric(veg.data[i,268:291])) # NDVI averages for 1993 (All sites) mean(as.numeric(veg.data[i,292:315])) # NDVI averages for 1994 (All sites) mean(as.numeric(veg.data[i,316:339])) # NDVI averages for 1995 (All sites) mean(as.numeric(veg.data[i,340:363])) # NDVI averages for 1996 (All sites) mean(as.numeric(veg.data[i,364:387])) # NDVI averages for 1997 (All sites) mean(as.numeric(veg.data[i,388:411])) # NDVI averages for 1998 (All sites) mean(as.numeric(veg.data[i,412:435])) # NDVI averages for 1999 (All sites) mean(as.numeric(veg.data[i,436:459])) # NDVI averages for 2000 (All sites) mean(as.numeric(veg.data[i,460:483])) # NDVI averages for 2001 (All sites) mean(as.numeric(veg.data[i,484:507])) # NDVI averages for 2002 (All sites) mean(as.numeric(veg.data[i,508:531])) # NDVI averages for 2003 (All sites) mean(as.numeric(veg.data[i,532:555])) # NDVI averages for 2004 (All sites) mean(as.numeric(veg.data[i,556:579])) # NDVI averages for 2005 (All sites) mean(as.numeric(veg.data[i,580:603])) # NDVI averages for 2006 (All sites) }

Once I compute all of the yearly means for all 49,681 sites I'm trying to put them in a 49,681 X 25 matrix. However, I'm having trouble getting the means in the matrix. I created an empty matrix with all zeros below but how can I replace them with the computed yearly means for each of the locations?

yearly.avgs <- matrix(c(rep(0,49681*25)), nrows=49681, ncol=25)
View(yearly.avgs)

Upvotes: 0

Views: 39

Answers (1)

akrun
akrun

Reputation: 887501

We could split the dataset by a grouping variable and then use rowMeans

tmp <- veg.data[4:603]
out <- sapply(split.default(tmp, as.integer(gl(ncol(tmp), 23, 
         ncol(tmp)))), rowMeans, na.rm = TRUE)

NOTE: Here it is assumed that 'veg.data' is a data.frame

Upvotes: 0

Related Questions