vendredi 13 septembre 2019

For loop to replace values in column with previous year's value based on another column

I want to create a for loop that will replace a value in a row with a value from the previous year (same month), based on if two columns are matching.

I have created the structure of a for loop, but I have not made progress in determining how to get the for loop to reference a value from a previous year.

Here is an example dataset:

fish <- c("A","A","B","B","C","C")
fish_wt<-c(2,3,4,5,5,7)
fish_count<-c(2,200,47,78,5,845)
date <- as.Date(c('2010-11-1','2009-11-1','2009-11-1','2008-11-1','2008-2-1','2007-2-1'))

data <- data.frame(fish,fish_wt,fish_count,date)
data$newcount<-0

Here is my for loop so far:

for  (i in 1:nrow(data)) {
  if (data$fish_wt[i] == data$fish_count[i]) {
    data$newcount[i] <- 10
  } else {
    data$newcount[i] <- data$fish_count[i]
  }
}

I have tried a number of things that display my lack of understanding of forloops, including

for  (i in 1:nrow(data)) {
  if (data$fish_wt[i] == data$fish_count[i]) {
    data$newcount[i] <- data$newcount[data$date==data$date[i])-1,]
 } else {
    data$newcount[i] <- data$fish_count[i]
  }
}

This is what I want my dataset to look like:

      fish    fish_wt  fish_count  date  newcount

1      A          2        2    2010-11-01  200
2      A          3        200  2009-11-01  200
3      B          4        47   2009-11-01  47
4      B          5        78   2008-11-01  78
5      C          5        5    2008-02-01  845
6      C          7        845  2007-02-01  845

This is my first experience with a large dataset and having to use for loops, suggestions for teaching materials are welcome.

Aucun commentaire:

Enregistrer un commentaire