我有一个以下的数据集:
structure(list(time = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L),
x = c(40.8914337158203, 20.0796813964844, 13.9093618392944,
17.1513957977295, 18.5109558105469, 40.7868537902832, 19.9750995635986,
13.804780960083, 16.8376483917236, 18.4063758850098, 40.6822700500488,
19.7659358978271, 13.7001991271973, 16.6284866333008, 18.3017921447754,
40.5776901245117, 19.66135597229, 13.5956182479858, 16.3147411346436,
18.1972122192383, 40.5776901245117, 19.5567722320557, 13.4910354614258,
16.1055774688721, 17.9880485534668), y = c(0.603550314903259,
-8.24852085113525, 9.65680503845215, -19.0118350982666, 6.43787002563477,
0.704141974449158, -8.34911251068115, 9.75739574432373, -19.2130165100098,
6.43787002563477, 0.704141974449158, -8.44970417022705, 9.75739574432373,
-19.5147914886475, 6.43787002563477, 0.704141974449158, -8.65088748931885,
9.85798835754395, -19.8165683746338, 6.33727836608887, 0.704141974449158,
-8.85207080841064, 9.85798835754395, -20.1183433532715, 6.33727836608887
), object = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -25L), .Names = c("time",
"x", "y", "object"))
现在,我想为每个time
的值计算凸包(使用chull
函数),并将其存储在同一数据集中(因为我之后想用ggplot2
进行绘图)。我可以使用with
函数对每个时间值使用chull
。
chull(filter(data_sample, time == 1)$x, filter(data_sample, time == 1)$y)
该代码返回一个向量4 3 1
。因此,我认为首先可以按时间分组,然后使用类似以下内容的方法计算组内的凸包点。
data_sample %>% group_by(time) %>% summarise(pts = chull(data_sample$x, data_sample$y))
问题在于我无法将向量存储在一行中。将每个顶点分别存储在不同的列中是一个选择,但以下方式可能更好:
data_sample %>% group_by(time) %>% summarise(pt1 = chull(data_sample$x, data_sample$y)[1])
不给出合理的结果。所以我的问题是:
1. 我该如何为每一行存储一个向量在一个列中?我已经读到tibble实际上可以有一个列表列,但是在我的情况下我该如何创建它?
2. 在每个组内计算chull
时我的尝试出了什么问题?
- (额外问题,如果可以的话) 为什么
data_sample %>% filter(time == 1) %>% chull(.$x, .$y)
不起作用?这是因为chull
不支持管道的使用和吗?