【发布时间】:2021-12-09 05:34:48
【问题描述】:
我有一个数据表,其中包含两列(crop 和 plotnum),用于标识组(crop)和组的元素(plotnum)。我想计算组内子组的值与组中子组之一的值的比率(例如 plot_4)。这个想法是在组内比较子组的表现。
这是一个示例数据表。
DT <- data.table(value = c(71.03528, 212.1139, 91.72795, 37.65809, 194.7215, 334.2902, 371.2177, 122.9278, 218.7671, 552.3567, 193.3274, 147.1677, 197.4963, 194.9721, 156.7755, 184.1638),
crop = c("BellPepper", "BellPepper", "BellPepper", "BellPepper", "ChiliPepper", "ChiliPepper", "ChiliPepper", "ChiliPepper", "Eggplant", "Eggplant", "Eggplant", "Eggplant", "Kale", "Kale", "Kale", "Kale"),
plotnum = c("plot_1", "plot_2", "plot_3", "plot_4", "plot_1", "plot_2", "plot_3", "plot_4", "plot_1", "plot_2", "plot_3", "plot_4", "plot_1", "plot_2", "plot_3", "plot_4"
))
执行此操作的一种方法是在新列中复制组的 plot_4 值,然后计算值列与 plot_4 列的比率,但我不知道如何获取子组值并填充只是 plot_4 列中组的单元格。也欢迎其他方法!
【问题讨论】:
标签: r data.table