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

Friday, October 7, 2022

[FIXED] How to make and organise a table of means and sds in R, according to different groupings

 October 07, 2022     r, statistics, summary     No comments   

Issue

Here is an example of what my data looks like:

group  valueA1 valueA2 valueB1 valueB2
1       2       3       4       5
1       4       4       5       5
2       5       4       3       2

I want to create a table with summary statistics organised in a few different ways. Firstly, I need my results organised by group, then by whether it contains A or B, then by whether it contains 1 or 2 in the variable name.

I understand that I can use something like this;

Agroup <- mydata %>%
    select(contains("valueA"))%>%
    summarise_all(list(mean=mean, sd=sd), na.rm = TRUE)

This gives me the list of means and sd, however. How can I print these as a table?


Solution

mydata = data.frame(
  group = c(1, 1, 2),
  valueA1 = c(2, 4, 5),
  valueA2 = c(3, 4, 4),
  valueB1 = c(4, 5, 3),
  valueB2 = c(5, 5, 2)
)

Agroup <- mydata %>%
  select(contains("valueA")) %>%
  summarise_all(list(mean=mean, sd=sd), na.rm = TRUE)
Agroup

gives you:

  valueA1_mean valueA2_mean valueA1_sd valueA2_sd
1     3.666667     3.666667   1.527525  0.5773503

and

Agroup <- mydata %>%
  select(contains("1")) %>%
  summarise_all(list(mean=mean, sd=sd), na.rm = TRUE)
Agroup

gives you:

  valueA1_mean valueB1_mean valueA1_sd valueB1_sd
1     3.666667            4   1.527525          1

Same applies to contains("valueB") and "2".



Answered By - Marc
Answer Checked By - David Goodson (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