Reputation: 11
penguins.table = table
penguins.table( penguins$species, penguins$island )
this created my table of counts.
Next task for to create a table of proportions
penguins.prop= prop.table
penguins.prop = prop.table( penguins.table, 2)
or I've tried this
penguins.prop (penguins.table, 2)
I receive this in R:
Error in marginSums( x, margin) : 'x' is not an array
I'm learning basic r code, but is there something wrong with my code as to why it's not working in particular?
I need to be able to produce a stacked bar plot with the data, however, am stuck trying to achieve this.
Upvotes: 1
Views: 1059
Reputation: 3228
Here is an alternative that just uses base R commands. This way only requires one string of code after you've loaded your data:
#### Load data ####
penguins <- structure(list(species = c("Adelie", "Adelie", "Adelie", "Adelie", "Adelie", "Adelie", "Adelie", "Adelie", "Adelie", "Adelie"),
island = c("Biscoe", "Biscoe", "Biscoe", "Biscoe", "Biscoe", "Biscoe", "Biscoe", "Biscoe", "Biscoe", "Biscoe"),
sex = c("male", "female", "female", "female", "male", "female", "male", "female", "male", "male"),
body_mass_g = c(3750, 3800, 3250, 3450, 3650, 3625, 4675, 3200, 3800, 4400)), row.names = c(NA, -10L ), class = c("tbl_df", "tbl", "data.frame"))
#### Make proportion table ####
prop.table(table(penguins$species,
penguins$island))
Which gives you the same output as the tidyverse method:
Biscoe
Adelie 1
If for some reason I am not understanding your query, here is what you can get for proportions if you mean the numeric variable here, body mass, and the only grouping variable of interest, sex:
library(tidyverse)
library(gt)
gt(penguins)
penguins %>%
group_by(sex) %>%
summarize(Proportion_Mass = body_mass_g/sum(body_mass_g))
This gives you this proportion of mass table:
# A tibble: 10 x 2
# Groups: sex [2]
sex Proportion_Mass
<chr> <dbl>
1 female 0.219
2 female 0.188
3 female 0.199
4 female 0.209
5 female 0.185
6 male 0.185
7 male 0.180
8 male 0.231
9 male 0.187
10 male 0.217
Upvotes: 0
Reputation: 2321
From your first two pieces of code, I'm assuming that you wanted to make a prop.table
using species
and island
. The code below will do that. You can add more variables you're interested in on the select(
line next to species
and island
(or you could just do the whole dataframe with df
, but it will be a lot).
df <- structure(list(species = c("Adelie", "Adelie", "Adelie", "Adelie", "Adelie", "Adelie", "Adelie", "Adelie", "Adelie", "Adelie"),
island = c("Biscoe", "Biscoe", "Biscoe", "Biscoe", "Biscoe", "Biscoe", "Biscoe", "Biscoe", "Biscoe", "Biscoe"),
sex = c("male", "female", "female", "female", "male", "female", "male", "female", "male", "male"),
body_mass_g = c(3750, 3800, 3250, 3450, 3650, 3625, 4675, 3200, 3800, 4400)), row.names = c(NA, -10L ), class = c("tbl_df", "tbl", "data.frame"))
library(tidyverse)
smalldf <- df %>% select(species, island)
prop.table(x = table(smalldf), margin = 2)
#> island
#> species Biscoe
#> Adelie 1
#prop.table(x = table(df), margin = 2)
# ^ a lot of output
Upvotes: 1