使用 mapply 为切点添加标签
Adding labels to cut points with mapply
我有数据如下:
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"))
value
列中的每一行都有 5 个值。使用 cut2
,我可以将这些值用作切点,如下所示:
dat <- setDT(dat)[, cats:= mapply(Hmisc:: cut2, Inc, value, oneval=FALSE)]
问题是 ?cut2
似乎不允许使用标签。结果我尝试了 cut
,但无论我做什么,它都说标签和类别不匹配。
dat <- setDT(dat)[, cats := mapply(cut, Inc, breaks=value, labels=c("cat1", "cat2", "cat3", "cat4"))]
期望的输出:
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
因为您已经在 value 列中有了间隔(可能是其他地方 cut()
的结果),您可以按如下方式使用 findInterval()
:
setDT(dat)[, cats:= paste0('cat', mapply(FUN = findInterval, x = Inc, vec = value))]
(确保 value 中的分隔符是有序的)
你可以使用 santoku::chop()
:
setDT(dat)[, cats := mapply(chop, Inc, value,
MoreArgs = list(labels = letters[1:4]))]
我有数据如下:
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"))
value
列中的每一行都有 5 个值。使用 cut2
,我可以将这些值用作切点,如下所示:
dat <- setDT(dat)[, cats:= mapply(Hmisc:: cut2, Inc, value, oneval=FALSE)]
问题是 ?cut2
似乎不允许使用标签。结果我尝试了 cut
,但无论我做什么,它都说标签和类别不匹配。
dat <- setDT(dat)[, cats := mapply(cut, Inc, breaks=value, labels=c("cat1", "cat2", "cat3", "cat4"))]
期望的输出:
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
因为您已经在 value 列中有了间隔(可能是其他地方 cut()
的结果),您可以按如下方式使用 findInterval()
:
setDT(dat)[, cats:= paste0('cat', mapply(FUN = findInterval, x = Inc, vec = value))]
(确保 value 中的分隔符是有序的)
你可以使用 santoku::chop()
:
setDT(dat)[, cats := mapply(chop, Inc, value,
MoreArgs = list(labels = letters[1:4]))]