mercredi 16 décembre 2020

Copy values from a vector of dates to a numeric vector

I have data frame with 4 columns (a,b,c,d) a,b are numeric vector , c contains zero and d is a date factor.

When I create in my loop the condition if a[i] != a[i-1] then c<-d, while d is a date factor and c is a numeric factor, I get numbers in vector c instead of dates.

so my question is how can I put in the places that meet the condition (in column c) the values of factor d (dates)?

note that I work with internal network so if there is a demand I will provide sample data...

> df
  a b c          d
1 1 0 0 2010-01-01
2 2 5 0 2011-01-01
3 3 6 0 2012-01-01
4 4 1 0 2013-01-01
5 5 2 0 2014-01-01
6 6 2 0 2015-01-01

 
for (i in 2:6) {
  if (a[i] != a[i-1]){
    c[i-1] <- d[i-1]
  }
  
}
 

Aucun commentaire:

Enregistrer un commentaire