r - Loop for applying mask and creating a vector of means -


i want create vector of means after applying mask number of text files (1,408). each file corresponding variable & year 1950-2013, 64 files variable , there 22 variables. coding applied in r:

datadir <- "c:\\dir\\" patternc <-"var1_" filessizec = sort(list.files(datadir,patternc))    (i in 1:length(filessizec)) {    thedata<-read.table(paste(datadir,filessizec[i],sep=""),header=f,sep=",") }  mask <- read.csv("http://dl.dropbox.com/s/2tbffe65i53afj1/examplemask.txt", header=f)  product <- mask * thedata product[product == 0] <- na mean(product$v1, na.rm=true) 

this gives me 1 value, mean, 64 text files. want mean each text file after masking has been applied. amended coding this, try , give me vector of means each year:

for (i in 1:length(filessizec)) { thedata<-read.table(paste(datadir,filessizec[i],sep=""),header=f,sep="\t")   pdt <- mask*thedata  if (i>0) {     themeanvalues <- c(themeanvalues,mean(pdt))    } else {   themeanvalues <- c(mean(pdt))  } } 

the error message is:

error: object 'themeanvalues' not found 

i'm not sure how change want.

so want achieve this:

year | var1_masked_mean | var2_masked_mean | etc... 1950 1951  .  .  . 2013 

i hope i'm asking makes sense!

thanks

r indexed @ 1 not 0 in example...

for (i in 1:length(filessizec)) { 

so change this

if (i>0) {   ...   } 

to

if (i>1) {   ...   } 

and in first iteration of loop when i==1 create themeanvalues variable.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -