我对使用data.table中的值填充data.frame有疑问。我已经卡住了withba data.table a
,其中我有4列,我需要选择最大Average
并在新的data.frame value
中返回z
。来自data.frame a
我正在使用Average
选择要放入Value
的{{1}}。我试图设置的方式是如果平均> 80而不是行z
和a
以及red_a
的选择和重复值,否则使用red_b
==最高平均值和Average[1]
==第二大值来选择Average[2]
。有没有办法自动生成red_a和red_b,而不会轻易填充我丢失的表格。我是要获得最大值,但一直坚持使用单个值来填充表格。我把目前为止尝试过的东西放在data.tables中。
示例数据集
Values
所需的输出
set_a <- c("a","a","a","a","b","b","b","b","c","c","c","c")
set_b <- c("red","red","red","red","red","red","red","red","red","red","red","red")
value <- c(42,68,90,91,22,65,89,98,78,88,91,33)
Average <- c(94,3,2,1,50,40,5,5,80,9,1,1)
a = data.frame(set_a,set_b,value,Average)
目前已使用R: 我已经能够获得最大值,但无法设置它以获得第二个值。
z <- data.frame(set_a = c("a","a","b","b"),set_b = c("red_a","red_b","red_a","red_b"),value = c(42,42,22,65))
library(reshape2)
example <- acast(z,set_a ~ set_b, value.var = "value")
red_a red_b
a 42 42
b 22 65