I have data as follows:
library(data.table)
library(Hmisc)
dat <- structure(list(Inc= c(120995, 238097.2, 103993.9, 93801,
255422.769863014, 257038.28739726, 1045388.66465753, 1040685.40328767,
715660.096547945, 484324), value = list(c(0, 15659.7, 78212.8,
419000, 1e 09), c(0, 16136.4, 89658.3, 464800, 1e 09), c(25075,
98208.05, 164627, 276586.59, 1e 09), c(25003.35, 91842, 149675.7,
256661.25, 1e 09), c(25024.7, 54664.75, 123463.2, 258610.05,
1e 09), c(25081, 56070, 120899.15, 282341.03, 1e 09), c(44852.9,
299214, 514994.25, 802947.38, 1e 09), c(36074, 311042.59, 528348.81,
754085.44, 1e 09), c(25540.8, 181958.59, 326804.69, 538702.19,
1e 09), c(28311.2, 175799.41, 316315, 507802.56, 1e 09))), row.names = c(NA,
-10L), class = c("data.table", "data.frame"))
Each row in value
column has 5 values. With cut2
, I can use those values as cut points as follows:
dat <- setDT(dat)[, cats:= mapply(Hmisc:: cut2, Inc, value, oneval=FALSE)]
The problem is that ?cut2
does not seem to allow using labels. As a result I tried cut
, but whatever I do, it says that the labels and the categories do not match up.
dat <- setDT(dat)[, cats := mapply(cut, Inc, breaks=value, labels=c("cat1", "cat2", "cat3", "cat4"))]
Desired output:
Inc value cats
1: 120995.0 0.00000e 00,1.56597e 04,7.82128e 04,4.19000e 05,1.00000e 09 cat3
2: 238097.2 0.00000e 00,1.61364e 04,8.96583e 04,4.64800e 05,1.00000e 09 cat3
3: 103993.9 2.507500e 04,9.820805e 04,1.646270e 05,2.765866e 05,1.000000e 09 cat2
...
10: 484324.0 28311.2, 175799.4, 316315.0, 507802.6,1000000000.0 cat3
CodePudding user response:
Since you already have the intervals in column value (perhaps as a result of a cut()
elsewhere) you could use findInterval()
as follows:
setDT(dat)[, cats:= paste0('cat', mapply(FUN = findInterval, x = Inc, vec = value))]
(make sure that the breaks in value are ordered)