What is the difference of occurrence and density for different types with in 4 different populations

Hi Flora and welcome to RStudio community.

Answering questions like these are very straightforward in R. I'd personally approach it with the tidyverse tool dplyr's group_by and summarize functions.

For example,

library(dplyr)

set.seed(1)
df <- tibble(
  group = c(1,1,2,2,2,3,3,4,4,4),
  type  = sample(LETTERS[1:3],10, replace = TRUE),
  value = runif(10)
)
df
#> # A tibble: 10 x 3
#>    group type  value
#>    <dbl> <chr> <dbl>
#>  1     1 A     0.206
#>  2     1 B     0.177
#>  3     2 B     0.687
#>  4     2 C     0.384
#>  5     2 A     0.770
#>  6     3 C     0.498
#>  7     3 C     0.718
#>  8     4 B     0.992
#>  9     4 B     0.380
#> 10     4 A     0.777

df %>% 
  group_by(group) %>% 
  summarise(
    n = n(),
    mean_value = mean(value),
    median_value = median(value),
    type_n = n_distinct(type)
  )
#> # A tibble: 4 x 5
#>   group     n mean_value median_value type_n
#>   <dbl> <int>      <dbl>        <dbl>  <int>
#> 1     1     2      0.191        0.191      2
#> 2     2     3      0.614        0.687      3
#> 3     3     2      0.608        0.608      1
#> 4     4     3      0.716        0.777      2

Created on 2019-02-08 by the reprex package (v0.2.1)

If all this is super new to you, here's a nice introduction to the dplyr package. Are you looking for pointers to get started with R?

https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html


And this question sounds a lot like a homework question, so just in case, I wanted to point out this forum's homework policy; FAQ: Homework Policy

TL:DR How to Ask a Homework Related Question:

  1. Do not ask verbatim copy-paste questions
  2. Explicitly mention the course you are taking.
  3. Be sure to ask your question as close to a reproducible example (reprex) as you can. Preferably using the reprex-package
1 Like