简体   繁体   English

R 的初学者使用计数表创建比例表

[英]Beginner to R using table of counts to create table of proportions

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:我在 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?我正在学习基本的 r 代码,但是我的代码是否有问题,为什么它不能特别工作?

I need to be able to produce a stacked bar plot with the data, however, am stuck trying to achieve this.我需要能够使用数据生成堆叠条 plot,但是,我一直试图实现这一目标。

From your first two pieces of code, I'm assuming that you wanted to make a prop.table using species and island .从您的前两段代码中,我假设您想使用speciesisland制作一个prop.table 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).您可以在select( speciesisland旁边的行中添加更多您感兴趣的变量(或者您可以使用df完成整个 dataframe ,但会很多)。

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

Here is an alternative that just uses base R commands.这是仅使用基本 R 命令的替代方法。 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:这为您提供了与 tidyverse 方法相同的 output :

         Biscoe
  Adelie      1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM