Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.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-获取每个ID的最高值_R_Analytics - Fatal编程技术网

R-获取每个ID的最高值

R-获取每个ID的最高值,r,analytics,R,Analytics,我有以下建议: >> animals_df: animal_name age cat 1 cat 1 cat 2 cat 3 cat 3 dog 1 dog 1 dog 3 dog 4 dog 4 dog 4 hor

我有以下建议:

>> animals_df:

animal_name    age
cat             1
cat             1
cat             2
cat             3
cat             3
dog             1
dog             1
dog             3
dog             4
dog             4
dog             4
horse           1
horse           3
horse           5
horse           5
horse           5
我只想从每个物种中提取年龄最高的动物。所以我想得到以下输出:

animal_name    age
    cat         3
    cat         3
    dog         4
    dog         4
    dog         4
    horse       5
    horse       5
    horse       5
我试过使用:

animals_df = do.call(rbind,lapply(split(animals_df, animals_df$animal_name), function(x) tail(x, 1) ) )
但这只会给出以下每种动物的一个实例:

animals_name    age
    cat          3
    dog          4
    horse        5

使用
dplyr
/
tidyverse
,这很容易:

library(tidyverse)

# How I read your data in, ignore since you already have your data available
df = read.table(file="clipboard", header=TRUE)
df %>%
    group_by(animal_name) %>%
    filter(age == max(age))

# Output:
Source: local data frame [8 x 2]
Groups: animal_name [3]

  animal_name   age
       <fctr> <int>
1         cat     3
2         cat     3
3         dog     4
4         dog     4
5         dog     4
6       horse     5
7       horse     5
8       horse     5
库(tidyverse)
#我如何读取您的数据,请忽略,因为您已经有了可用的数据
df=read.table(file=“clipboard”,header=TRUE)
df%>%
分组依据(动物名称)%>%
过滤器(年龄==最大(年龄))
#输出:
来源:本地数据帧[8 x 2]
分组:动物名称[3]
动物名称年龄
1第3类
2第3类
3狗4
狗4
5狗4
6匹马5
7匹马5
8匹马5

另一个
数据。表
选项为:

library(data.table)
setDT(df)
df[, .SD[which(age == max(age))], by = animal_name]

#       animal_name age
#1:         cat   3
#2:         cat   3
#3:         dog   4
#4:         dog   4
#5:         dog   4
#6:       horse   5
#7:       horse   5
#8:       horse   5
dat[带(dat,age==ave(age,animal_name,FUN=max)),]