0

I would like to have a new column with Ntile but it should depend on column 1 - "year" and show the ntile number for column 2 - "mileage".

  year mileage
  <dbl>   <dbl>
1  2011    7413
2  2011   10926
3  2011    7351
4  2011   11613
5  2012    8367
6  2010   25125

mydata$Ntile <- ntile(mydata$mileage, 10)

I know the easy to use function ntile, but I do not know how to make it depend on 2 columns. I would like to have ntiles for mileage but for each year, 2010, 2011 and 2012 to be calculated in new column "Ntile".

PS: I know there is not enough data to calculate Ntiles for 2011 and 2012, it is just an example.

kath
  • 7,624
  • 17
  • 32

1 Answers1

2

I like the data.table approach:

library(data.table)
mydata <- as.data.table(mydata)
mydata[, Ntile:=ntile(mileage,10), by=year]

Best!

LocoGris
  • 4,432
  • 3
  • 15
  • 30