简体   繁体   English

从其他数据中获取最高水平的值

[英]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.

相关问题 如何从多级R data.table中组内列的最高值排名 - How to rank from highest value in column within group in multi level R data.table 在每个级别的列中使用最大值来计算data.table中的相对值 - Use highest value in column per level to calculate relative values in data.table 从列表中提取和排序数据-找到最大值 - Extracting and sorting Data from a list- find the highest value 在R中-通过查找不同值的最大值的规则从列中提取值 - In R - Extracting a value from a column, by the rule of finding the highest value of a different value 有3个相同尺寸的矩阵 - 我想获得三个不同矩阵的每个单元格的最高值 - Have 3 matrices of same dimensions - I want to get the highest value of each cell of the three different matrices 汇总计数数据并返回R中因子的最高水平 - Summarizing count data and returning highest level of a factor in R 如何根据同一行另一列的最高值从 1 列中获取值? - How do I get the value from 1 column based on highest value of another column of the same row? 在R中写一个function,从数据帧列表中提取每个数据帧中某个变量的最大值 - Write a function in R to extract the highest value of a variable in each data frame from a list of data frames 具有最高行值的子集数据帧 - Sub set data frame with highest row value 如何在数据框中找到最高价值? - How to find highest value in a data frame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM