3
我有以下DF:的R - 获得最高值为每个ID
>> 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
'DAT [与(DAT,年龄== AVE(年龄,animal_name,FUN =最大值)),]'在基R. – thelatemail