简体   繁体   English

R dplyr根据乐趣指数汇总一个列值(另一列)

[英]R dplyr summarise one column value based on index of fun(another column)

I have a data frame as this, and want the output as shown desired at the end. 我有一个数据框,并希望最后显示所需的输出。 Instead, I get the NA output in the middle. 相反,我在中间得到NA输出。 Is there any way to do what I want using dplyr? 有没有办法用dplyr做我想做的事情?

x <- c(1234, 1234, 1234, 5678, 5678)
y <- c(95138, 30004, 90038, 01294, 15914)
z <- c('2014-01-20', '2014-10-30', '2015-04-12', '2010-2-28', '2015-01-01')
df <- data.frame(x, y, z)
df$z <- as.Date(df$z)
df %>% group_by(x) %>% summarise(y = y[max(z)])

What I get:
     x  y
1 1234 NA
2 5678 NA

Desired Output:
     x     y 
1 1234 90038
2 5678 15914

You can try which.max to get the numeric index of max values that can be used for subsetting the 'y' element. 您可以尝试使用which.max来获取可用于对'y'元素进行子集化的max的数字索引。 Using max just gives the maximum values of z . 使用max只给出z的最大值。

df %>%
    group_by(x) %>%
    summarise(y= y[which.max(z)])
#     x     y
#1 1234 90038
#2 5678 15914

dplyr使用filtermax

df%>%group_by(x)%>%filter(z==max(z))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM