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')]
Hi Cassie, I'm not sure exactly what you are trying to get: i assume that each station will have a different ppt value for each year-month combination, but it looks like you're trying to get one ppt value for each station. Steve On Thu, Jul 28, 2011 at 1:30 AM, Wilson, Cassandra J <cwils11 at ku.edu> wrote:> 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')] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
I'm not sure what you're doing... but here are some tips about the parts I can understand. 1) you don't need to use "which" as much. This works fine: stnID <- stnid[!duplicated(stnid)] 2) "which" works within a for loop 3) Do you realize that "stnID" is shorter after you removed duplicates? I think it might be the wrong length for your loop function. 4) You can loop over things directly rather than their index. E.g.: for(stn in stnID) print(stn) On Wed, Jul 27, 2011 at 10:30 AM, Wilson, Cassandra J <cwils11@ku.edu>wrote:> 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')] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Maybe Matching Threads
- Using a 'for' loop : there should be a better way in R
- drawing a line that shifts from solid to broken
- Defining multiple variables in a loop
- linear model coefficients by year and industry, fitted values, residuals, panel data
- Shifting of Principal amount while calculating Present Value