[英]Get a value with the highest level from a different data
Suppose I have data including some information of names and scores in school. 假设我有数据,其中包括一些学校名称和分数的信息。
name = c('Ann','Dann','Pann', 'Sam', 'Lora', 'Peter')
score = c(30,30,50,70,20,10)
school = data.frame(name=name, score=score)
I may use 'which.max' to find the highest level of the data. 我可能会使用“ which.max”来查找最高级别的数据。
data = school[which.max(school$score),]
data$name
[1] Sam
Now I want to find the name with the highest score in Aclass set. 现在,我想在Aclass集合中找到得分最高的名称。
Aclass = c('Peter','Pann', 'Lora', 'Smith')
The outcome should be 结果应该是
[1] 'Pann'
Please notice 'Smith' is not a part of school data. 请注意,“ Smith”不是学校数据的一部分。 Regardless of how many new names are included in Aclass, what I want to find is just a name with the highest score in school data.
无论Aclass中包含多少个新名称,我都想找到的只是在学校数据中得分最高的名称。 What code should I make to get it?
我应该得到什么代码?
This method searches through a subset of the data.frame school using with
to reduce typing. 此方法使用
with
来减少data.frame学校的子集,以减少键入。
with(school[school$name %in% Aclass,], name[which.max(score)])
[1] Pann
Levels: Ann Dann Lora Pann Peter Sam
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.