mardi 15 décembre 2020

Replace Values in Column with Random Sequence of Values

I have a dataset like the one below but with many more rows and columns.

df <- data.frame(lat = c(19.3606, 19.2462, 19.3587),
                 long = c(-155.0684, -155.2582, -155.0274),
                 acq_date = c('2020-12-11','2020-12-11','2020-12-13')
)

I want replace the acq_date column values with a random date within a defined time range. I found some codes to create a random date (Generating Random Dates) and to loop through rows (For each row in an R dataframe) but I am not seeing new dates populated in the dataframe. What am I missing here?

for(i in 1:nrow(df)) {
  row <- df[i,]
  row$acq_date <- sample(seq(as.Date('2020/12/15'), as.Date('2021/01/30'), by="day"), 1)
}



Aucun commentaire:

Enregistrer un commentaire