0

I have similar dataset like this

structure(list(id = c(1, 1, 1, 1), time1 = c(1682, 1682, 1683, 
1683)), class = "data.frame", row.names = c(NA, -4L))

and I want to create an extra column (column 2) where the maximum value (taken from time1 column) is repeated for the id column

structure(list(id = c(1, 1, 1, 1), time1 = c(1682, 1682, 1683, 
1683), time2 = c(1683, 1683, 1683, 1683)), class = "data.frame", row.names = c(NA, -4L))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
kam
  • 345
  • 1
  • 8
  • `df <- transform(df, time2 = ave(time1, id, FUN = max))` – Ronak Shah Jul 06 '21 at 12:07
  • It worked on my dummy df but on my original data I am getting error "error in unique.default(x, max = max) unique()applies only to vectors. – kam Jul 06 '21 at 12:16
  • You might have factor columns in your original data instead of numeric. Check `str(df)`. convert factor to numeric https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-integer-numeric-without-loss-of-information and then use the answer. – Ronak Shah Jul 06 '21 at 12:30

0 Answers0