0

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)
}
  • You can generate the random date date vector of the proper length and then just assign `acq_date` directly to that. e.g. `df$acq_date <- randomdates` No loops required. – SteveM Dec 15 '20 at 19:45

1 Answers1

2

The problem is that you are taking the row out of the dataframe, edit it, but don't put it back.

To get your code working, you can do

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

But i would suggest creating the randomdates all at one via

randomDates <- sample(seq(as.Date('2020/12/15'), as.Date('2021/01/30'), by="day"), nrow(df))

and then insert them into the df via

df$acq_date <- randomDates 
Jonas
  • 1,760
  • 1
  • 3
  • 12