简体   繁体   English

R,哪个行值包含最多相同的列值

[英]R, which row value contains the most same column values

Hello I have data set like this. 您好,我有这样的数据集。

Age  Sallary  
24   >50k  
17   <=50k  
31   >50k  
24   >50k  

I need to find the age which has the most >50k sallary 我需要找到> 50k最高的年龄

going with akrun's table comment, 和akrun的table评论一起去,

names(which.max(table(df)[, ">50k"]))
[1] "24"

table calculates the cross-tab of these two columns. table计算这两列的交叉表。 [, ">50K"] subsets to the column of salaries you are looking for, then which.max pulls out the first element of this column that contains the maximum count. [, ">50K"]子集到您要查找的薪水列中,然后which.max拉出该列中包含最大数量的第一个元素。 Finally, since a named vector is returned by each of these functions, we can extract the age with names . 最后,由于每个函数都返回了一个命名向量,因此我们可以使用names提取年龄。

With a data.frame with additional columns, you could replace table(df) with table(df$Age, df$Sallary) to select these variables from the data.frame. 对于具有其他列的data.frame,可以将table(df)替换为table(df$Age, df$Sallary)以从data.frame中选择这些变量。

so 所以

names(which.max(table(df$Age, df$Sallary)[, ">50k"]))
[1] "24"

also works for the example dataset. 也适用于示例数据集。

data 数据

df <- 
structure(list(Age = c(24L, 17L, 31L, 24L), Sallary = structure(c(2L, 
1L, 2L, 2L), .Label = c("<=50k", ">50k"), class = "factor")), .Names = c("Age", 
"Sallary"), class = "data.frame", row.names = c(NA, -4L))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R-告诉我在哪一行中,行和列中的值不相同 - R - Telling me in which Row, the Value is not the Same in Row & Column 在 R 中的 dataframe 中添加一个新列,其中包含每行中最频繁的值 - Adding a new column to the dataframe in R that contains the most frequent value in each row 在 R 中:将 NA 替换为其他行的值,但其他列中的值相同 - In R: Replace NAs with values of other row but same value in other column R Tidyverse 判断哪一列值不一样,返回两个值 - R Tidyverse Determine which column value not the same, return two values 如何找到哪一列包含与 R 中另一指定列相同的值? - How can I find which column contains the same value as another specified column in R? 查找给定列中最频繁包含最大值的行 - Find the row that most frequently contains the largest value in a given column R:仅当(相同)行(不同)列中的值为true时,才将值添加到[行,列]中 - R : adding the values in a [row,column] only if value is true in (same) row, (different) column 子集在一列中具有相同值的所有行,按另一列分组,其中第三列的至少一行包含 R 中的特定字母 - subset all rows with the same value in one column, grouped by another column, where at least one row of third column contains a specific letter in R 根据同一列中的其他行值替换 R 数据框中的行值 - Replacing row values in R data frame based on other row value in same column R:仅当同一列中的两行中的值为true时,才在[row,column]中添加值 - R : adding the values in a [row,column] only if value is true in two rows within the same column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM