I’m new to R and I’m attempting to do something that I believe should be very simple but code online has not helped.
data <- structure(list(Group = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3),
Time = c(1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2), mean_PctPasses = c(68.26,
60.2666666666667, 62.05, 66.3833333333333, 59.7333333333333,
69.7714285714286, 57.1888888888889, 63.8875, 61.1833333333333,
59.775, 66.2666666666667, 62.12), mean_AvgPassing = c(7.3,
7.01111111111111, 6.35, 9.26666666666667, 6.68333333333333,
8.78571428571429, 5.87777777777778, 8.3125, 7.63333333333333,
7.7, 8.38333333333334, 6.89), mean_AvgRush = c(0.3, -0.3,
3.5, 0.75, 5, 1.47142857142857, 5.71111111111111, 3.3875,
2.74, 6.6, 4.5, 5), mean_Int = c(0.2, 0.777777777777778,
0.25, 0.5, 1.5, 0.857142857142857, 0.777777777777778, 0.75,
0.666666666666667, 0.75, 0.833333333333333, 1.1), mean_Rate = c(99.3,
88.5222222222222, 80.5, 106.45, 77.2333333333333, 102.885714285714,
76.8888888888889, 100.075, 92.1166666666667, 78.55, 98.05,
79.56)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-12L), .Names = c("Group", "Time", "mean_PctPasses", "mean_AvgPassing",
"mean_AvgRush", "mean_Int", "mean_Rate"))
Using this dataset I have 2 grouping variables “Group” and “Time”. I would like to get the means and confidence intervals in a table format for each of these combinations for the variables mean_PctPasses to mean_Rate and save the result so it is in a table. I need it to be in a table because I will refer to it later in plotly. Doing this in SPSS is quite easy.
I’ve attempted several functions and below is issues I’ve had with each
library(rcompanion)
ci.mean(mean_PctPasses~Group+Time, data = data)
library(DescTools)
MeanCI(data$mean_PctPasses)
library(Rmisc)
CI(data$mean_PctPasses, ci=0.95)
MeanCI, ci.mean and CI don’t allow for multiple variables to be listed and are saved as a table (only shows up in console)
by(data = data, data$Group, FUN = stat.desc)
This won’t allow me to group my data according to both Group and Time. Below is an example of the graph I hope to build in R (shown in SPSS).
Any help/assistance on this would be great. Let me know if any clarifications are needed and I’ll be sure to edit my initial post.
UPDATE
After some great answers (thank you Rob and Steven) I felt I needed to clarify my question a little bit.
I would like to get statistics for each group (not individually) for all statistics (mean_PctPasses to mean_Rate). An example of a function that produces the stats I would like for one variable is shown below using Rmisc
library(Rmisc)
group.UCL(mean_PctPasses~Group+Time , data, FUN=CI)
This gives me the following output only for mean_PctPasses
Output Using Rmisc
But what I would like to have is the following (which I have photoshopped)
Image of Desired Ouput
This could, of course, be shown in the other orientation (example below with SPSS and SEM).
Alternative Orientation example in SPSS
5
Answers
Assuming you just want the usual non-pooled t confidence interval for each group you can do
Doing this with
R
is easy, too.Another way, using
CI()
fromRmisc
:Admittedly, I’m not a big fan of the “magic numbers” after the call to
CI()
.Plotting the data is equally simple.
You may be interested to reproduce the SPSS style with base R graphics.
Nevertheless you should consider using a dotplot for displaying your data.
Update tidyr 1.0.0
As an elegant alternative to the
summarise
solutions given before, it is good to know that the new tidyr 1.0.0 contained a function often overlooked:unnest_wider
.With that, you can simplify the code to the following:
which gives
You can plot this easily with
which gives you
Maybe it is easier to work on one variable at a time. There is a simpler way of doing this.
You need to install/load the Hmisc package using library()
Here is the output:
It looks nicer/neater if you group_by() using one variable at a time (and repeat for all the quantitative variables):
With the output: