Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-在新数据帧中显示具有最大值的行_R_Indexing_Subset_Extract - Fatal编程技术网

R-在新数据帧中显示具有最大值的行

R-在新数据帧中显示具有最大值的行,r,indexing,subset,extract,R,Indexing,Subset,Extract,我有这样一个数据帧: x1= c("Station1", "Station2", "Station3", "Station4", "Station5", "Station6", "Station7", "Station8", "Station9", "Station10") x2= seq(-2,10 , length=10) x3= seq(30, 45, length=10) x4= c(1, 3, 2, 1, 4, 2, 4, 3, 3, 1) x5= seq(10, 100, lengt

我有这样一个数据帧:

x1= c("Station1", "Station2", "Station3", "Station4", "Station5", "Station6", "Station7", "Station8", "Station9", "Station10")
x2= seq(-2,10 , length=10)
x3= seq(30, 45, length=10)
x4= c(1, 3, 2, 1, 4, 2, 4, 3, 3, 1)
x5= seq(10, 100, length=10)
df = data.frame(Station=x1, Lon=x2, Lat=x3, Number=x4, Size=x5)

>df
    Station        Lon      Lat Number Size
1   Station1 -2.0000000 30.00000      1   10
2   Station2 -0.6666667 31.66667      3   20
3   Station3  0.6666667 33.33333      2   30
4   Station4  2.0000000 35.00000      1   40
5   Station5  3.3333333 36.66667      4   50
6   Station6  4.6666667 38.33333      2   60
7   Station7  6.0000000 40.00000      4   70
8   Station8  7.3333333 41.66667      3   80
9   Station9  8.6666667 43.33333      3   90
10 Station10 10.0000000 45.00000      1  100
这些站点分为4个不同的组,可以在$Number列中看到。现在,我想为每个组提取df$Size列中具有最大值的电台。因此,我希望有一个新的数据帧,它有4行(每组一行)和相同的列

应该是这样的:

    Station        Lon      Lat Number Size
6   Station6  4.6666667 38.33333      2   60
7   Station7  6.0000000 40.00000      4   70
9   Station9  8.6666667 43.33333      3   90
10 Station10 10.0000000 45.00000      1  100
我已经试过那样去获取行号了,但是没有用

index1 = df$Number=="1" 
index2 = df$Number=="2" 
index3 = df$Number=="3" 
index4 = df$Number=="4" 

df[index1][which.max(df$Size),]
有什么想法吗?

可能是:

library(dplyr)
library(magrittr)

df %>%
  group_by(Number) %>%
  arrange(desc(Size)) %>%
  top_n(1)
还有这个:还有这个: