I have a dataframe that looks like this:
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:
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
.
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
CodePudding user response:
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:
remotes::install_github("vbfelix/relper")
CodePudding user response:
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"