[英]How to make a new column in a data.frame so that column counts the number of different row in that data.frame?
[英]view hexbin as data.frame with counts in a column
无论如何将hexbins视为data.frame?
dat = data.frame(x = rnorm(10000), y = rnorm(10000))
hbin <- hexbin::hexbin(dat$x, dat$y, xbins = 40)
as.data.frame(hbin)
as.data.frame.default(hbin) 中的错误:无法将 class 'structure("hexbin", package = "hexbin")' 强制转换为 data.frame
hexbin
对象是 S4 数据(这里是 Hadley Wickham 关于在 R 中操作 S4 的一个很好的资源),所以如果你看到:
require(hexbin)
dat <- data.frame(x = rnorm(10000), y = rnorm(10000))
hbin <- hexbin::hexbin(dat$x, dat$y, xbins = 40)
str(hbin)
#> Formal class 'hexbin' [package "hexbin"] with 16 slots
#> ..@ cell : int [1:1003] 19 96 110 133 148 149 179 180 181 182 ...
#> ..@ count : int [1:1003] 1 1 1 1 1 1 1 1 1 1 ...
#> ..@ xcm : num [1:1003] -0.335 -1.267 1.246 -1.771 0.841 ...
#> ..@ ycm : num [1:1003] -3.75 -3.49 -3.39 -3.28 -3.28 ...
#> ..@ xbins : num 40
#> ..@ shape : num 1
#> ..@ xbnds : num [1:2] -3.55 3.69
#> ..@ ybnds : num [1:2] -3.75 3.28
#> ..@ dimen : num [1:2] 48 41
#> ..@ n : int 10000
#> ..@ ncells: int 1003
#> ..@ call : language hexbin::hexbin(x = dat$x, y = dat$y, xbins = 40)
#> ..@ xlab : chr "dat$x"
#> ..@ ylab : chr "dat$y"
#> ..@ cID : NULL
#> ..@ cAtt : int(0)
然后,您可以使用@
操作对hbin
部分进行子集化。 例如,如果您想在数据框中包含hbin.xcm
和hbin.ycm
(两个向量的长度均为 10003 个元素)。
df <- data.frame(xcm = hbin@xcm, ycm = hbin@ycm)
head(df)
#> xcm ycm
#> 1 -1.15641677 -3.464301
#> 2 -0.67133436 -3.435775
#> 3 0.08136055 -3.422812
#> 4 0.80232574 -3.437158
#> 5 -3.06306998 -3.314999
#> 6 -1.39092669 -3.101475
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.