我想基于StartAge列的离散仓,在数据框age.model中使用[使用点阵的xyplot()]绘制数据。
我正在使用以下代码:
# set up boundaries for intervals/bins
breaks <- c(0,3,4,5,6,8,13,15,17,18,19,20,22)
# specify interval/bin labels
labels <- c("<3", "3-4)", "4-5)","5-6)", "6-8)","8-13)", "13-15)","15-17)","17-18)","18-19)","19-20)",">=20")
# bucketing data points into bins
bins <- cut(age.model$StartAge, breaks, include.lowest = T, right=FALSE, labels=labels)
# inspect bins
summary(bins)
在cut()的第一个参数中,我指定了要离散化的列。但是,返回的因子不包括整个DF。我该怎么办?
使用dput可重现:
structure(list(Height = c(0.207224416925809, -1.19429150954007,
0.0247585682642494, 0.023546515879641, 1.51423735121426, -1.09376538778425,
-0.125209484617016, -0.63639210765747, 0.305071992864995, -0.422021082477656
), Weight = c(-0.366133564723644, -1.06969961340686, -0.0793604259237282,
-0.708230200986797, 1.71593234004357, -0.685215310472794, -1.20353653394014,
-0.490399232488568, 0.742874184424376, -0.331519044995803), Training = c(19,
27, 27, 24, 35, 23, 15, 14, 47, 7), StartAge = c(13, 19, 20,
20, 14, 2, 8, 4, 17, 18)), row.names = c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10"), class = "data.frame")
答案 0 :(得分:1)
如果您使用xyplot
来浏览数据,请考虑在代码中使用equal.count()
或shingle()
。玩弄(毫无头绪)数据,对于较低的StartAge
箱,重量和高度之间的近似线性关系似乎不成立,如第一个示例所示。
# Starting with data in age.model
library(lattice)
xyplot(Weight ~ Height | equal.count(StartAge), age.model, type = c("p", "r"))
equal.count
的默认箱数为6。可以轻松更改以查看其他分组:
# Create four groups of equal counts to explore
xyplot(Weight ~ Height | equal.count(StartAge, 4), age.model, type = c("p", "r"))
shingle()
功能允许重叠箱,如下所示。
# Create three groups that overlapping with each other
bins <- cbind(lower = c(0,8,16), upper = c(13,18,24))
xyplot(Weight ~ Height | shingle(StartAge, bins), age.model, type = c("p", "r"))
答案 1 :(得分:0)
要将bin添加到数据框,只需在新列中对其进行影响即可:
age.model$bins <- bins