简体   繁体   English

如何根据 R 中另一列的值计算一列中最常见的变量?

[英]How to calculated the most common variable from one column based on values from another column in R?

I have a dataframe that looks like this:我有一个 dataframe,看起来像这样:

 plot sp value sum
1  A  1a  1    3   
2  A  1b  1    3
3  A  1a  1    3
4  B  1a  2    4
5  B  1a  2    4
6  C  1b  3    9
7  C  1b  3    9
8  C  1b  3    9

I calculated then the share of sum for each line and got this:然后我计算了每一行的sum份额并得到了这个:

 plot sp value sum share
1  A  1a  1    3    0.3
2  A  1b  1    3    0.3
3  A  1a  1    3    0.3
4  B  1a  2    4    0.5
5  B  1a  2    4    0.5
6  C  1b  3    9    0.3
7  C  1b  3    9    0.3
8  C  1b  3    9    0.3

I want to know now what is the most common sp for each plot based on the share .我现在想知道基于share的每个plot最常见的sp是什么。 In the case of this example I would like it to look like this:在这个例子中,我希望它看起来像这样:

 plot sp value sum share dom.sp
1  A  1a  1    3    0.3    1a
2  A  1b  1    3    0.3    1a
3  A  1a  1    3    0.3    1a
4  B  1a  2    4    0.5    1a
5  B  1a  2    4    0.5    1a
6  C  1b  3    9    0.3    1b
7  C  1b  3    9    0.3    1b
8  C  1b  3    9    0.3    1b

Very similar solution to your previous question与您之前的问题非常相似的解决方案

> ave(df$sp,df$plot,FUN=function(x){names(table(x))[1]})
[1] "1a" "1a" "1a" "1a" "1a" "1b" "1b" "1b"

If I understood correctly this might work:如果我理解正确,这可能会起作用:

library(dplyr)

df <-
structure(list(account = c("M205109", "M205109", "M201212", "M205668", 
"M207954", "M208966", "M203465", "M207622", "M201869", "M201869"
), age = c(20, 20, 18, 29, 21, 19, 19, 23, 22, 22)), class = "data.frame", row.names = c(NA, 
-10L))

df %>% 
  group_by(plot) %>% 
  mutate(dom.sp = relper::cat_mode(sp))

# A tibble: 8 x 5
# Groups:   plot [3]
  plot  sp      sum share dom.sp
  <chr> <chr> <dbl> <dbl> <chr> 
1 A     1a        3   0.3 1a    
2 A     1b        3   0.3 1a    
3 A     1a        3   0.3 1a    
4 B     1a        4   0.5 1a    
5 B     1a        4   0.5 1a    
6 C     1b        9   0.3 1b    
7 C     1b        9   0.3 1b    
8 C     1b        9   0.3 1b   

I used a function called cat_mode that get the mode of a variable, here the package if you want to try:我使用了一个名为cat_mode的 function 来获取变量的模式,如果你想尝试,这里是 package:

remotes::install_github("vbfelix/relper")

暂无
暂无

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

相关问题 (R) 如何根据 R 中的另一列和 ID 从一列复制粘贴值 - (R) How to copy paste values from one column based on another column and ID in R 如何根据一列中的值对数据进行装箱,并汇总R中另一列中的出现次数? - How to bin data based on values in one column, and sum occurrences from another column in R? 如何基于R中另一列的值从数据框中的一列提取值 - How to extract values from one column in a data frame based on values in another in R R 中,如何向数据集添加一列,该数据集从一列中添加值并从另一列中减去值? - How to add a column to a dataset which adds values from one column and subtracts values from another column in R? R,根据 R 中另一列的值从一列中获取整数 - R, get entires from one column based on values of another column in R 根据 R 中另一列的值对列中的值进行分组 - Group values from a column based on another column's values in R 如何使 R 根据条件从另一列取值? - How to make R take values from another column based on criteria? 如何根据r中多个列的值创建变量 - How to create a variable based on the values from more than one column in r 如何根据R中的一列列表将一个数据框中的值汇总到另一个数据框中 - How to sum values from one data frame into another based on a column of lists in R 如何根据 R 中的另一列获取一列的所有值? - How to get all values of one column based on another column in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM