PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 6, 2022

[FIXED] How do I make a more succint, stratified table on R?

 October 06, 2022     r, statistics     No comments   

Issue

I currently have a table of proportions for my project. My proportions are specified by class and region that people live in for the output. Example in the table below.

Class Region Prop
0 1 0.95
0 2 0.32
1 1 0.63
1 2 0.34
2 1 0.87
2 2 0.95
2 3 0.88
3 2 0.95
3 3 0.1515

I have dummy values in place for the class levels and the regions. The prop column is associated with the proportion of the outcome of interest. So for example, in region 1, those in the lowest class (0) have a prevalence of the outcome interest at 95%. How can I better organize this when I want to output a table into my paper. Apologies in advance if this is a simple answer, still trying to grasp R


Solution

Welcome to Stack overflow. For future questions in R, please use dput(df) to output a sample of your data for us to work on.

Your table is in tidy format as is, and this is great for calculations/r-functions and what not. But for humans it is not so good. This question leads to opinionated answers, so here is my opinion: You should widen your table so that each column is a region (1, 2 or 3) and each row is a Class (0, 1, 2, 3) and then each cell value is the Prop value: In R this can be done like this using the package tidyr:

require(tidyr)
set.seed(1)
df <- data.frame(
  Class = c(0, 0, 1, 1, 2, 2, 2, 3, 3),
  Region = c(1, 2, 1, 2, 1, 2, 3, 2, 3),
  Prop = rnorm(9)+1
)

#Takes each unique value of Region and makes a column out of it
readable <- tidyr::pivot_wider(
  data = df,
  names_from = Region,
  values_from = Prop
)

#Change the names to something more human readable
names(readable) <- c("Class", "Region 1", "Region 2", "Region 3")

#Output:
# A tibble: 4 × 4
  Class `Region 1` `Region 2` `Region 3`
  <dbl>      <dbl>      <dbl>      <dbl>
1     0      0.374      1.18       NA   
2     1      0.164      2.60       NA   
3     2      1.33       0.180       1.49
4     3     NA          1.74        1.58


Answered By - brendbech
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing