-2

I'm trying to join the mean, median, sd and var of a dataset but I'm failing to do so with dplyr.

Here is a SS with the Data

I'm just failing to obtain a table containing mean, median, sd and var for each header (Congruent and Incongruent)

The header of the table would still be Congruent and Incongruent and the rows would be each function described above.

This is probably a dupe but I have read a lot of similar questions and I just couldn't achieve what I'm trying, sorry for my rookieness.

Many thanks

EduGord
  • 139
  • 2
  • 13
  • Please don't include pictures of data. Include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) in your question with sample input and the code you tried. Make it clear what the desired output is for your sample. – MrFlick Dec 15 '16 at 17:09
  • No need for `dplyr`, just use: `res <- data.frame(Congruent=c(mean(df$Congruent),median(df$Congruent),sd(df$Congruent)),Incongruent=c(mean(df$Incongruent),median(df$Incongruent),sd(df$Incongruent)))` where `df` is your original `data.frame`. – aichao Dec 15 '16 at 17:10
  • Thanks very much @aichao that actually worked and helped me a lot. I'm going to try ekstroem's approach now to see how it goes. Thank you all very much! – EduGord Dec 15 '16 at 18:58
  • Sorry for that @MrFlick but I'm really just starting so it's difficult for me to include reproducible examples, as soon as I get to rnorm and stuff I'll certainly shape my questions accordingly. – EduGord Dec 15 '16 at 18:58

1 Answers1

1

There are several approaches and packages for doing this - using the basic R functions as suggested in the comments works well too. One example is to use the Publish package:

## Install package
library(devtools)
install_github('tagteam/Publish')
library(Publish)

Then you can run

# Simulating some data for you
DF <- stack(data.frame(cong = rnorm(20), incong=rnorm(20)))
univariateTable(ind ~ values, data=DF)

which splits up on ind (the group) and produces a "standard Table 1"-like output

  Variable     Level cong (n=20) incong (n=20) Total (n=40)
1   values mean (sd)  -0.3 (1.0)    -0.2 (0.9)   -0.2 (0.9)
  p-value
1  0.7721

There are tons of options, you can add several variable to get summary statistics for each of them, and you can also request, say, medians and IQR:

univariateTable(ind ~ Q(values), data=DF, compareGroups=FALSE)

which produces

  Variable        Level      cong (n=20)    incong (n=20)
1   values median [iqr] -0.3 [-0.8, 0.3] -0.2 [-0.5, 0.2]
      Total (n=40)
1 -0.3 [-0.8, 0.2]
ekstroem
  • 5,957
  • 3
  • 22
  • 48
  • Thanks for your response @ekstroem , that was helpful! Apparently, the only thing that is missing is the `library(Publish)` before running the univariateTable function. – EduGord Dec 17 '16 at 13:51
  • Ah yes. I'll edit the response to keep it complete. Thanks – ekstroem Dec 17 '16 at 23:16