[R] for loop help

Wilson, Cassandra J cwils11 at ku.edu
Wed Jul 27 17:30:48 CEST 2011


I am having a hard time putting the below into a loop, where it pulls out ppt from all he stations I have versus having to go through and hard code the data to the specific stations. I tried
stnID <- stnid[which(duplicated(stnid)==FALSE)]
for(i in 1:length(stnID))
{
ppt[i] <- ppt[which(stnid==[i])]
}

but it doesn't like to use the which function inside a for loop? Any idea's here is a sample of my code... thanks-Cassie

### label data ###
stnid <- dat1[,1] #list of stations
lat <- dat1[,2] #latitude of station
lon <- dat1[,3] #longitude of station
year <- dat1[,4] # data year
month <- dat1[,5] #data month
ppt <- dat2 #precipiation data


#### station's missing zero years ###
ppt2  <- matrix(ppt[,which(stnid == '140365')],ncol=1)
year2 <- year[which(stnid == '140365')]
year2 <- year2[which(duplicated(year2) == FALSE)]
month2 <- month[which(stnid == '140365')]

ppt3  <- matrix(ppt[,which(stnid == '140405')],ncol=1)
year3 <- year[which(stnid == '140405')]
year3 <- year3[which(duplicated(year3) == FALSE)]
month3 <- month[which(stnid == '140405')]



More information about the R-help mailing list