将标签添加到用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
似乎不允许使用标签。结果,我尝试了剪切
,但是无论我做什么,都说标签和类别不匹配。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您已经在 value 列中的间隔(也许是由于
cut()
在其他地方),因此可以使用find Interval()
如下:(确保订购 value 中的断裂)
Since you already have the intervals in column value (perhaps as a result of a
cut()
elsewhere) you could usefindInterval()
as follows:(make sure that the breaks in value are ordered)
您可以使用
santoku :: Chop()
:You could use
santoku::chop()
: