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