I need help on my code #596
-
I have a trouble with R because i have a data base with a few cateories and i want to do a thing like these excel table on R |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
@R-icntay a wee boost here? |
Beta Was this translation helpful? Give feedback.
-
Hello @SKAIWO , @jlooper , Firstly, apologies for seeing this late. @SKAIWO, you would simply need the # Load dplyr and palmerpenguins dataset
library(tidyverse)
library(palmerpenguins)
penguins %>%
slice_head(n = 5)
#> # A tibble: 5 x 8
#> species island bill_length_mm bill_depth_mm flipper_length_~ body_mass_g sex
#> <fct> <fct> <dbl> <dbl> <int> <int> <fct>
#> 1 Adelie Torge~ 39.1 18.7 181 3750 male
#> 2 Adelie Torge~ 39.5 17.4 186 3800 fema~
#> 3 Adelie Torge~ 40.3 18 195 3250 fema~
#> 4 Adelie Torge~ NA NA NA NA <NA>
#> 5 Adelie Torge~ 36.7 19.3 193 3450 fema~
#> # ... with 1 more variable: year <int>
# Count the number of male and female penguins for each species
penguins %>%
# Drop missing values aka NAs
drop_na() %>%
count(sex, species)
#> # A tibble: 6 x 3
#> sex species n
#> <fct> <fct> <int>
#> 1 female Adelie 73
#> 2 female Chinstrap 34
#> 3 female Gentoo 58
#> 4 male Adelie 73
#> 5 male Chinstrap 34
#> 6 male Gentoo 61 Created on 2022-06-11 by the reprex package (v2.0.1) There you go! This is what we would call # Load tidyverse and palmerpenguins dataset
library(tidyverse)
library(palmerpenguins)
# Count the number of male and female penguins for each species
penguins %>%
# Drop missing values aka NAs
drop_na() %>%
count(sex, species) %>%
pivot_wider(names_from = species, values_from = n)
#> # A tibble: 2 x 4
#> sex Adelie Chinstrap Gentoo
#> <fct> <int> <int> <int>
#> 1 female 73 34 58
#> 2 male 73 34 61 Created on 2022-06-11 by the reprex package (v2.0.1) We have covered such tasks here: https://docs.microsoft.com/en-us/learn/modules/explore-analyze-data-with-r/?WT.mc_id=academic-59300-cacaste Happy learning 🙂. |
Beta Was this translation helpful? Give feedback.
Hello @SKAIWO , @jlooper ,
Firstly, apologies for seeing this late.
@SKAIWO, you would simply need the
count
verb fromdplyr
🙂. See the example below: