How do I reduce this data frame by groups? - r

I have the following
t <- structure(list(name = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Alice", "Bob",
"Jane Doe", "John Doe"), class = "factor"), school = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Alice School",
"Bob School", "Someother School", "Someschool College"), class = "factor"),
group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("A", "B"), class = "factor"),
question = structure(c(2L, 4L, 6L, 8L, 1L, 3L, 5L, 7L, 2L,
4L, 6L, 8L, 1L, 3L, 5L, 7L, 2L, 4L, 6L, 8L, 1L, 3L, 5L, 7L,
2L, 4L, 6L, 8L, 1L, 3L, 5L, 7L), .Label = c("q1", "q2", "q3",
"q4", "q5", "q6", "q7", "q8"), class = "factor"), mark = c(0L,
0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L,
1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L,
1L), subject = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("C", "M"), class = "factor")), .Names = c("name",
"school", "group", "question", "mark", "subject"), row.names = c(7L,
15L, 23L, 31L, 3L, 11L, 19L, 27L, 8L, 16L, 24L, 32L, 4L, 12L,
20L, 28L, 6L, 14L, 22L, 30L, 2L, 10L, 18L, 26L, 5L, 13L, 21L,
29L, 1L, 9L, 17L, 25L), class = "data.frame")
and I need to produce a data frame in which each student has one combined mark for each subject. The combination is simply a sum of the marks on each question. So, for example, Jane Doe will have 3 on subject C and 2 on subject M. I've been banging my head for long enough with Reduce and other approaches. I could possibly solve this in a very procedural way, but if I could do that with a one-liner (or close approximation), I'd be happier. I'm sure it can be done...

You said it in your question; you want to group_by student and subject and compute the sum
library(tidyverse)
asdf %>%
group_by(name, subject) %>%
summarise(score = sum(mark))

Here a data.table solution:
library(data.table)
setDT(t)[, sum(mark), by = list(name, subject)]

And just for completeness, base R:
aggregate(mark ~ name + subject, data=t, sum)
This says "aggregate the response variable mark by the grouping variables name and subject, using sum as the aggregation function".

Related

Comparing the mean, radius and concavity of benign and malignant stage cancer

I am trying to implement a series of box plots which demonstrate the area, radius and concavity of human cells. The variable I am trying to plot is 'characters' split into two subsets 'Malignant' or 'Benign'.
I keep receiving the following error messages:
> Error in FUN(X[[i]], ...) : object 'Class_mean' not found
> Error in FUN(X[[i]], ...) : object 'Class_radius' not found
Please see my code:
ggplot(wisconsin, aes(x= Class, y=Class_mean, fill="pink")) +
geom_boxplot(fill= "yellow")+
ggtitle("radius of benign and malignant stage")
ggplot(wisconsin, aes(x= Class, y=Class_radius))+
geom_boxplot()+
ggtitle("area of benign and malignant stage")
ggplot(wisconsin, aes(x= Class, y=concavity_mean))+
geom_boxplot()+
ggtitle("concavity of benign and malignant stage")
Any ideas on how I could figure out the radius, mean and concavity object to Y variable?
All suggestions welcome
Please see head of data:
structure(list(Cl.thickness = c(5L, 5L, 3L, 6L, 4L, 8L, 1L, 2L,
2L, 4L, 1L, 2L, 5L, 1L, 8L, 7L, 4L, 4L, 10L, 6L), Cell.size = c(1L,
4L, 1L, 8L, 1L, 10L, 1L, 1L, 1L, 2L, 1L, 1L, 3L, 1L, 7L, 4L,
1L, 1L, 7L, 1L), Cell.shape = c(1L, 4L, 1L, 8L, 1L, 10L, 1L,
2L, 1L, 1L, 1L, 1L, 3L, 1L, 5L, 6L, 1L, 1L, 7L, 1L), Marg.adhesion = c(1L,
5L, 1L, 1L, 3L, 8L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 10L, 4L,
1L, 1L, 6L, 1L), Epith.c.size = c(2L, 7L, 2L, 3L, 2L, 7L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 7L, 6L, 2L, 2L, 4L, 2L), Bare.nuclei = c(1L,
10L, 2L, 4L, 1L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 9L, 1L,
1L, 1L, 10L, 1L), Bl.cromatin = c(3L, 3L, 3L, 3L, 3L, 9L, 3L,
3L, 1L, 2L, 3L, 2L, 4L, 3L, 5L, 4L, 2L, 3L, 4L, 3L), Normal.nucleoli = c(1L,
2L, 1L, 7L, 1L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 5L, 3L, 1L,
1L, 1L, 1L), Mitoses = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L,
1L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 2L, 1L), Class = c("benign",
"benign", "benign", "benign", "benign", "malignant", "benign",
"benign", "benign", "benign", "benign", "benign", "malignant",
"benign", "malignant", "malignant", "benign", "benign", "malignant",
"benign")), row.names = c(NA, 20L), class = "data.frame")
> dput(head(wisconsin, 20))
structure(list(Cl.thickness = c(5L, 5L, 3L, 6L, 4L, 8L, 1L, 2L,
2L, 4L, 1L, 2L, 5L, 1L, 8L, 7L, 4L, 4L, 10L, 6L), Cell.size = c(1L,
4L, 1L, 8L, 1L, 10L, 1L, 1L, 1L, 2L, 1L, 1L, 3L, 1L, 7L, 4L,
1L, 1L, 7L, 1L), Cell.shape = c(1L, 4L, 1L, 8L, 1L, 10L, 1L,
2L, 1L, 1L, 1L, 1L, 3L, 1L, 5L, 6L, 1L, 1L, 7L, 1L), Marg.adhesion = c(1L,
5L, 1L, 1L, 3L, 8L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 10L, 4L,
1L, 1L, 6L, 1L), Epith.c.size = c(2L, 7L, 2L, 3L, 2L, 7L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 7L, 6L, 2L, 2L, 4L, 2L), Bare.nuclei = c(1L,
10L, 2L, 4L, 1L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 9L, 1L,
1L, 1L, 10L, 1L), Bl.cromatin = c(3L, 3L, 3L, 3L, 3L, 9L, 3L,
3L, 1L, 2L, 3L, 2L, 4L, 3L, 5L, 4L, 2L, 3L, 4L, 3L), Normal.nucleoli = c(1L,
2L, 1L, 7L, 1L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 5L, 3L, 1L,
1L, 1L, 1L), Mitoses = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L,
1L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 2L, 1L), Class = c("benign",
"benign", "benign", "benign", "benign", "malignant", "benign",
"benign", "benign", "benign", "benign", "benign", "malignant",
"benign", "malignant", "malignant", "benign", "benign", "malignant",
"benign")), row.names = c(NA, 20L), class = "data.frame")
If you want to plot each character per class, then the code below might solve the problem.
This type of problems generally has to do with reshaping the data. The format should be the long format and the data is in wide format. See this post on how to reshape the data from wide to long format. I will use package tidyr, function pivot_longer.
library(ggplot2)
wisconsin |>
tidyr::pivot_longer(-Class, names_to = "characters") |>
ggplot(aes(x = Class, y = value)) +
geom_boxplot(fill = "lightblue") +
facet_wrap(~ characters) +
theme_bw()
Created on 2022-10-19 with reprex v2.0.2

R: Can I make rename accept a function as a new varname

I have a data set and want to create a time series of means by education and by race. However, I am struggling to make rename() accept a command rather than just a string as a new variable name.
My code:
#libraries
install.packages(c("tidyverse", "spatstat"))
lapply(c("tidyverse", "spatstat"), require, character.only = TRUE)
#calculate weighted medians by race and education
wmedians <- lapply(data[,c("race", "education")],function(vars){
data %>%
group_by((vars), year) %>%
summarize(w_median = weighted.median(wealth, weight))%>%
rename(colnames(vars) = "(vars)")
})
This gives me the following error:
Error: unexpected '=' in:
" summarize(w_median = weighted.median(wealth, weight))%>%
rename(colnames(vars) ="
Desired Output:
At the moment I get a list with two tibbles where the column name of the first column is "(vars)". Instead I would like the column name to be "race" in the first tibble and "education" in the second one.
I tried around a bit and my guess is that the rename-function generally does not accept any function (such as colnames()) as a new variable name. Do you have any idea how to get around this?
Sample of my data:
structure(list(year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L), .Label = c("1989", "1992", "1995", "1998", "2001",
"2004", "2007", "2010", "2013", "2016"), class = "factor"), weight = c(9084.9691295,
1571.9511258, 191.75635451, 204.62890325, 204.62890325, 20.462890325,
10.412082059, 144.25723032, 66.170395167, 17169.253056, 5240.2917738,
768.75688855, 152.72765752, 166.52285228, 4165.0038712, 12.696579164,
1.3158341152, 3574.4768327, 18.640983311, 687.040202, 6738.6966881,
0.9648849583, 2.9597366608, 20.019564258, 895.51359665, 1.3152534108,
3244.207427, 2.0000789024, 750.95122778, 1.6580375994, 4007.7581965,
4.7569235917, 180.73948443, 237.26008744, 2.8105880617, 2.8105880617,
1.7964957199, 4883.711226, 17.268444467, 2.9783310762, 354.15138196,
162.00933944, 1.6450475811, 1.3755398392, 4174.6347012, 44.17020127,
4987.2079388, 1.3755398392, 18.01293584, 3.3426730968, 1.5455142055,
904.20169275, 12.578831203, 10051.580218, 162.70814346, 2.1257090517,
2.1257090517, 1027.7013368, 8166.4587927, 1.7239086827, 2.1374243666,
0.901741906, 2.9900010571, 33.443685091, 12913.631224, 5973.1098594,
9527.6211412, 2.2331957715, 376.47055359, 2.9488054663, 2.9488054663,
2.8570458091, 8.0625166988, 4.7867036342, 923.0539464, 2.7381019933,
5800.0572063, 4.3379657179, 0.8195417131, 6108.0937784, 23.232719795,
121.97520298, 1684.0365357, 4.7867036342, 4.8109344834, 479.31438165,
4.8109344834, 3.7209489469, 5257.7592767, 3220.9438379, 3.1738803883,
3.8910375552, 3.7209489469, 30.231850875, 3277.3748665, 32.237421329,
2.7966498146, 3208.415157, 34.375315295, 30.200771547, 31.811971048,
20.701306688, 2.7966498146, 31.968899323, 33.280487562, 2548.6399138,
32.94638396, 2.7966498146, 2.6033164134, 30.815250688, 23.702590485,
31.465956118, 29.46116036, 760.36545895, 8.3114397117, 23.702590485,
4.6504256805, 3204.2440292, 6.8002099257, 35.813850525), race = structure(c(1L,
1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L,
1L, 4L, 1L, 1L, 4L, 4L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L,
1L, 4L, 4L, 4L, 1L, 4L, 4L, 1L, 1L, 4L, 1L, 1L, 4L, 1L, 4L, 4L,
4L, 4L, 1L, 4L, 1L, 1L, 4L, 1L, 1L, 1L, 4L, 4L, 4L, 1L, 1L, 1L,
1L, 1L, 4L, 1L, 4L, 4L, 4L, 4L, 4L, 1L, 4L, 1L, 4L, 4L, 1L, 1L,
1L, 1L, 4L, 4L, 1L, 4L, 4L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 4L, 4L,
1L, 1L, 4L, 1L, 4L, 4L, 4L, 1L, 1L, 1L, 4L, 4L, 1L, 1L, 1L, 4L,
1L, 4L, 4L, 4L, 1L, 4L, 1L), .Label = c("black", "Hispanic",
"other", "white"), class = "factor"), education = structure(c(2L,
1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L,
2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("college degree", "no college",
"some college"), class = "factor"), wealth = c(370932.98, 10170000.94,
12598660.39, 114293258.81, 211275067.86, 290691670.17, 85726008.16,
230065771.49, 246480115.73, 349587.55, 378694.96, 3164512.87,
8495442.34, 6020105.92, 505133.05, 367073411.82, 2928346179.67,
533643.21, 554996993.66, 5648836.48, 392098.63, 692454429.71,
872798466.66, 352917443.83, 798505.65, 1250534235.01, 645694.94,
570963643.71, 10983328.56, 360732249.01, 717840.77, 365513260.49,
7204246.71, 8901952.1, 473348324.51, 686068914.54, 566191645.55,
451622.81, 376484717.9, 702785331.9, 6677625.87, 5826581.94,
538339875.62, 81126854.16, 1073395.38, 595512233.49, 769008.48,
363748981.28, 203466108.48, 801021687.19, 744505545.07, 5219227.59,
132716087.05, 813859.01, 4429075.76, 376023173.93, 418753292.21,
591392.47, 986060.58, 740193054.24, 305729499.91, 815008777.34,
292998224.45, 25586473.36, 1482007.57, 832890.03, 1431390.64,
465587944.43, 31578347.08, 771207206.27, 774386788.46, 283388639.66,
491131539.34, 1106105605.16, 15792325.59, 448260665.36, 1598512.35,
256789830.23, 1327614423.44, 1034918.02, 44314918.71, 47310703.56,
1174945.19, 1050196871.69, 791040687.75, 40226229.67, 1193945180.43,
882118783.02, 865693.49, 598203.92, 94409003.49, 321989895.39,
611845894.05, 414941965.04, 1697121.78, 192234467.65, 1200517207.56,
1522890.02, 42378401.16, 269560657.85, 49377539.54, 450366559.75,
1362371354.42, 254034284.01, 65726116.22, 840350.29, 87111742.24,
1116803883.14, 1703800000, 248761000, 104962000, 111565400, 1224715000,
1405000, 1196478000, 981045000, 535052000, 2077700, 444554200,
41907500)), row.names = c(NA, -120L), groups = structure(list(
year = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L,
10L), .Label = c("1989", "1992", "1995", "1998", "2001",
"2004", "2007", "2010", "2013", "2016"), class = "factor"),
education = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
2L), .Label = c("college degree", "no college", "some college"
), class = "factor"), race = structure(c(1L, 4L, 1L, 4L,
1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L,
4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L, 1L, 4L,
1L, 4L, 1L, 4L, 1L, 4L), .Label = c("black", "Hispanic",
"other", "white"), class = "factor"), .rows = structure(list(
c(2L, 3L, 12L), c(5L, 6L, 9L), c(1L, 10L, 11L), c(4L,
7L, 8L), c(13L, 14L, 20L), c(17L, 22L, 23L), c(15L, 18L,
21L), c(16L, 19L, 24L), c(29L, 33L, 34L), c(26L, 35L,
36L), c(25L, 27L, 31L), c(28L, 30L, 32L), c(41L, 42L,
44L), c(37L, 40L, 43L), c(38L, 45L, 47L), c(39L, 46L,
48L), c(52L, 55L, 57L), c(50L, 51L, 60L), c(54L, 58L,
59L), c(49L, 53L, 56L), c(63L, 64L, 69L), c(62L, 70L,
71L), 65:67, c(61L, 68L, 72L), c(75L, 81L, 82L), c(74L,
79L, 84L), c(77L, 80L, 83L), c(73L, 76L, 78L), c(86L,
91L, 92L), c(85L, 87L, 88L), c(89L, 90L, 95L), c(93L,
94L, 96L), c(101L, 105L, 107L), c(97L, 103L, 108L), c(98L,
99L, 106L), c(100L, 102L, 104L), 110:112, c(109L, 113L,
115L), c(114L, 118L, 120L), c(116L, 117L, 119L)), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -40L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Thanks a lot in advance!
Pass column names in lapply and use .data to refer them in group_by so you don't need rename at all as column names would be maintained.
library(dplyr)
library(spatstat)
lapply(c("race", "education"),function(vars){
data %>%
group_by(.data[[vars]], year) %>%
summarize(w_median = weighted.median(wealth, weight))
}) -> result
result

drawing line segments connecting sets of points

I am trying to connect sets of (two) points at each level of x, in each facet. Here is a reproducible example:
datum <- structure(list(frequency = c(8L, 7L, 6L, 18L, 5L, 11L, 16L, 15L,
9L, 8L, 8L,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L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L
), .Label = c("1", "2"), class = "factor"), knowledge = structure(c(2L,
3L, 1L, 3L, 1L, 1L, 3L, 3L, 1L, 3L, 1L, 3L, 2L, 2L, 1L, 1L,
1L, 1L, 3L, 3L, 1L, 2L, 3L, 1L, 1L, 2L, 2L, 1L, 1L, 3L, 2L,
3L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 3L, 3L, 1L, 1L, 2L, 3L,
3L, 2L, 2L, 3L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 3L, 1L, 1L, 2L,
1L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 3L, 2L, 2L, 1L, 2L, 3L, 2L,
1L, 2L, 3L, 3L, 2L, 1L, 3L, 1L, 3L, 2L, 1L, 3L, 2L, 2L, 3L,
1L, 1L, 2L, 1L, 2L, 3L, 1L, 3L, 1L), .Label = c("1", "2",
"3"), class = "factor")), .Names = c("frequency", "band",
"test", "knowledge"), row.names = c(NA, -100L), class = "data.frame")
Here is the code I have so far:
ggplot(datum, aes(knowledge, frequency, color=test)) +
stat_summary(fun.y='mean', geom='point', position=position_dodge(width=.9), size=3) +
facet_grid(~band) +
labs(y='number of words (max = 20)', x='self-report knowledge') +
scale_x_discrete(labels=c('none', 'form', 'meaning'))
Looking at the left-most facet ('1') in the graph, I would like a line to connect the pretest to posttest in the none column, another line connecting pretest to posttest in the form column, and a line connecting the pretest to the posttest in the meaning column. I would like this done in each facet.
I hope that makes sense, and thanks!
I find relying on ggplot too much for data manipulation/summarizing can hurt more than it helps. I have no idea how to connect the position-dodged points with a line. Instead, I'd do something like this:
library(dplyr)
datsum = datum %>%
group_by(band, knowledge, test) %>%
summarize(mean = mean(frequency)) %>%
ungroup %>%
mutate(knowledge_fac = factor(knowledge, labels = c('none', 'form', 'meaning')))
ggplot(datsum, aes(x = test, y = mean)) +
geom_path(aes(group = band:knowledge)) +
geom_point(aes(color = factor(test))) +
facet_grid(band ~ knowledge_fac) +
labs(y='number of words (max = 20)', x='self-report knowledge')
Borrowing from Gregor's work in munging the data, I think this does what was requested. The mutate() chunk creates Test to be a numeric offset of -0.1 for test 1 and 0.1 for test 2. This is then added to the numeric value of knowledge. The result is the numeric x passed to ggplot2. Gregor correctly defined the groups, so the rest is straightforward.
library(dplyr)
datsum <- datum %>%
group_by(band, knowledge, test) %>%
summarize(mean = mean(frequency)) %>%
mutate(Test = 0.1 * (2 * (test == 2) - 1),
Knowledge = as.numeric(knowledge) + Test) %>%
ungroup
ggplot(datsum, aes(x = Knowledge, y = mean, color = test)) +
geom_path(aes(group = band:knowledge), color = "black") +
geom_point(size = 3) +
facet_wrap(~ band, nrow = 1) +
labs(y='number of words (max = 20)', x='self-report knowledge') +
scale_color_manual(values = c("orange", "blue")) +
scale_x_continuous(limits = c(0.5, 3.5), breaks = 1:3,
labels = c("none", "form", "meaning"))

Calculating Mean and SD by several groups in the dataframe

I have a set of data for which I need to calculate descriptive statistics such as Mean and SD. The trick is that there are two grouping variables that I am trying to use to compute these statistics. The end goal is to obviously plot the mean with SD as error bars but I've left that for the end. I am only able to compute the mean and SD grouped by 1 variable.
I have added some data from my dataset for all of you to understand what I'm trying to achieve. I want the mean and SD to be calculated for each variable for a particular treatment. For example, mean and SD for NBR for control, low, medium and high separately rather than mean for all values in NBR.
Any help would be greatly appreciated!
structure(list(Plot = structure(c(1L, 1L, 1L, 1L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 2L, 2L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L, 1L), .Label = c("1Y",
"PB", "W2", "W6"), class = "factor"), Index_Name = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L), .Label = c("NDVI",
"TCARI", "TCARI.OSAVI", "PRI", "WI", "MSI", "NDWI", "WI.NDVI",
"SIPI", "NPQI", "ARI1", "NPCI", "NBR", "BAI", "CSI", "MIRBI",
"dr525", "dr720", "dr705", "dr1550", "dr1030", "dr1215"), class = "factor"),
value = c(0.49052608, 0.561715025, 0.681058775, 0.418972935,
0.598040793, 0.325178174, 0.39611861, 0.482473151, 0.54914233,
0.326459616, 0.449550569, 0.374982537, 0.498916873, 0.157797056,
0.375598585, 0.369668549, 0.067487106, 0.056591414, 0.043807341,
0.051725487, 0.093754202, 0.015804118, 0.010208146, 0.069180561,
0.047256836, 0.014956843, 0.046819988, 0.024258394, 0.043112507,
0.00452289, 0.016060574, 0.019127195, 0.008477461, 0.006499842,
0.002487178, 0.010566068, 0.009558856, 0.002362453, 0.001000077,
0.011176193, 0.005692711, 0.001454276, 0.002671122, 0.004389175,
0.005963771, 0.000633278, 0.001738936, 0.00297559, -0.077436131,
-0.063224907, -0.04141906, -0.066029217, -0.078353929, -0.06269534,
-0.062645246, -0.071945342, -0.074503314, -0.039682707, -0.033944239,
-0.067220924, -0.068048716, -0.022911819, -0.055515436, -0.058149026,
0.954379644, 0.961920174, 0.97662393, 0.94470929, 0.967260471,
0.914987902, 0.913176432, 0.97220911, 0.964781397, 0.980127277,
0.959769039, 0.950881126, 0.944166816, 0.979386244, 0.936922322,
0.955133338, 1.175592718, 1.035411255, 0.800772417, 1.336485183,
0.901900085, 1.958346653, 1.803438856, 1.161775069, 0.991080828,
1.341931259, 1.367156399, 1.509288183, 1.312893994, 1.906225698,
1.55938558, 1.669204336, -0.152945727, -0.133348314, -0.094084016,
-0.171933735, -0.120064992, -0.224291293, -0.222158781, -0.118233127,
-0.122948258, -0.091499501, -0.158288059, -0.156905855, -0.182074425,
-0.146780258, -0.145409057, -0.192813307, 1.945624674, 1.712470081,
1.433978924, 2.254821761, 1.617382095, 2.813804784, 2.305310603,
2.015053288, 1.756887684, 3.002292558, 2.134952338, 2.535801087,
1.892433123, 6.206619232, 2.494477773, 2.583756021, 0.742911497,
0.780168303, 0.843457583, 0.65195971, 0.830975899, 0.574818388,
0.616816538, 0.719949791, 0.772449293, 0.53750892, 0.731024759,
0.612688442, 0.716754763, 0.252652391, 0.578930733, 0.572089182,
-0.100561356, -0.099296831, -0.092483347, -0.075723921, -0.142851785,
-0.089130121, -0.04298129, -0.102317471, -0.106134004, -0.071568325,
-0.037759085, -0.074943034, -0.082143985, -0.013550746, -0.042738224,
-0.052687304, 5.676531166, 6.233612201, 7.784421521, 3.641213998,
5.227190069, 9.309509369, 20.49898004, 3.561438434, 6.481028072,
11.57997948, 5.13213622, 5.522131497, 6.185040981, 7.365686981,
13.08585062, 6.613297282, 0.454955786, 0.474862574, 0.426419642,
0.416520911, 0.508605292, 0.419544434, 0.392348758, 0.434385238,
0.481246028, 0.30321057, 0.306903479, 0.418355778, 0.437338379,
0.13683308, 0.344513171, 0.355125174, 0.326893516, 0.387399288,
0.515154218, 0.182065945, 0.500676583, -0.209412435, -0.076292248,
0.241900919, 0.365695503, -0.114812299, 0.054850362, -0.014567095,
0.236467347, -0.44330064, -0.169288639, -0.063890848, 9.714595654,
8.915965336, 13.82459669, 6.675855319, 6.569665271, 21.75446394,
36.33854707, 6.696758415, 8.98382931, 38.25326354, 30.84745383,
11.95769473, 9.717936813, 42.50114638, 30.2370565, 16.44987732,
0.937570572, 1.067734668, 1.381944935, 0.81371232, 1.234718748,
0.537791913, 0.594631013, 0.919821798, 1.108794746, 0.775895297,
0.775232557, 0.698743732, 0.835058861, 0.53024459, 0.671650699,
0.632847585, 0.759036354, 0.783179738, 1.238926572, 0.505364128,
0.516207899, 1.726260299, 1.740690206, 0.673977376, 0.93851288,
1.992771034, 1.706586328, 1.351753865, 0.71549755, 2.224957641,
1.954708561, 1.480237458, 0.000674694, 0.000664894, 0.000510151,
0.000696155, 0.000952195, 0.000223176, 0.000117448, 0.000866633,
0.000569965, 0.000152732, 0.000509423, 0.000387613, 0.000515502,
5.22e-05, 0.000179509, 0.000274014, 0.001953683, 0.002430971,
0.002398101, 0.002139202, 0.003496132, 0.000307836, 0.000251248,
0.002999544, 0.002325187, 0.000269912, 0.000755669, 0.001121944,
0.001959135, 0.000112208, 0.000368882, 0.000816404, 0.002392897,
0.002322436, 0.00184051, 0.002173463, 0.003689693, 0.000462046,
0.000309551, 0.002912204, 0.002086132, 0.00041697, 0.001201466,
0.001087332, 0.001915154, 0.000122092, 0.000485394, 0.000828789,
0.000452513, 0.000597348, 0.000447671, 0.000567325, 0.000644769,
0.000219354, 0.000211073, 0.000596602, 0.000469899, 9.16e-05,
0.000291864, 0.000349806, 0.00054511, 6.09e-05, 0.000205434,
0.000265793, 0.000319714, 0.000274149, 0.000279715, 0.000402482,
0.000500617, 0.000228484, 9.38e-05, 0.000318863, 0.000378217,
2.97e-05, 0.000130812, 0.000221053, 0.000385489, 9.62e-06,
6.79e-05, 0.000215798, 0.00034812, 0.000394904, 0.000240183,
0.000427487, 0.000415141, 0.000205784, 0.000123726, 0.000314875,
0.000378047, 5.26e-05, 0.000105128, 0.000218972, 0.000400508,
4.07e-05, 8.06e-05, 0.000233541)), .Names = c("Plot", "Landcover",
"Epoch", "Index_Name", "value"), row.names = c(NA, -352L), class = "data.frame")
mean(df$value[df$Plot == 'control' & df$Index_Name == 'NBR'])
summary(df$value[df$Plot == 'control' & df$Index_Name == 'NBR'])
is it what you want?
df$Plot == 'control' & df$Index_Name == 'NBR'
can choose 'control' and 'NBR'
it return a logical varible,so df$value[df$Plot =='control' & df$Index_Name == 'NBR'] can choose the value for for NBR and control.
Similarly,
mean(df$value[df$Plot == 'low' & df$Index_Name == 'NBR'])
mean(df$value[df$Plot == 'medium ' & df$Index_Name == 'NBR'])
mean(df$value[df$Plot == 'high' & df$Index_Name == 'NBR'])

3 Factor Nested ANOVA in R

I am trying to replicate a 3 Factor nested ANOVA anlaysis in a paper: Underwood, AJ (1993) The Mechanics of spatially replicated sampling programmes to detect environmental impacts in a variable world.
The data for the example (from Table 3, Underwood 1993) can be produced by:
dat <-
structure(list(B = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("A", "B"), class = "factor"), C = structure(c(2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("C", "I"), class = "factor"),
Times = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"),
Locations = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L,
2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L), X = c(59L, 51L, 45L, 46L, 40L, 32L, 39L, 32L, 25L, 51L,
44L, 37L, 55L, 47L, 41L, 31L, 38L, 45L, 41L, 47L, 55L, 43L,
36L, 29L, 23L, 30L, 37L, 57L, 50L, 43L, 36L, 44L, 51L, 39L,
29L, 23L, 38L, 44L, 52L, 31L, 38L, 45L, 42L, 35L, 28L, 52L,
44L, 37L, 51L, 43L, 37L, 38L, 31L, 24L, 60L, 52L, 46L, 30L,
37L, 44L, 41L, 34L, 27L, 53L, 46L, 39L, 40L, 34L, 26L, 21L,
27L, 35L), Times.unique = structure(c(5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("A_1", "A_2", "A_3",
"A_4", "B_1", "B_2", "B_3", "B_4"), class = "factor")), .Names = c("B",
"C", "Times", "Locations", "Y", "Times.unique"), row.names = c(NA,
-72L), class = "data.frame")
dat
The data frame dat has 4 factors:
B - has two levels "A" and "B" (before v after)
Times - 8 levels, 4 within before "B" and 4 within after "A", coded as 1:4 within each. note that variable Times.unique is the same thing but with a unique code for each time (before and after)
Locations - has three levels, all measured every time both before and after
C - has two levels control (C) and (I). note: two locations are control and one is impact
While I am clear on how to analyse such a design using mixed models (lmer), I would like to replicate his example exactly so that I can run some simulations to compare his method.
In particular I am attempting to replicate the SS values presented in table 4 under column "a". He fits a design that has SS and df values for the following terms:
B -> SS = 66.13, df = 1
Times(B) -> SS = 280.64, df = 6
Locations -> SS = 283.86, df = 2
B x Locations -> SS = 29.26, df = 2
Times(B) x Locations-> SS = 575.45, df = 12
Residual -> SS = 2420.00, df = 48
Total -> SS = 6208.34, df = 71
I assume the Times(B) term represents Times nested within the Before/After treatment "B". For this example he ignores that Locations are from control and impact treatments and leaves out factor C altogether.
I have tried all possible combinations I can think of to reproduce this nested anova, using both unique Times coding and Times coded as 1:4 within B (before and after). I have tried using %in%, / and Error() arguments, as well as Anova from car to change the type of SS calculated. Examples of the %in% and / nested fits include:
aov(Y~B+Locations+Times%in%B+B:Locations+Times%in%B:Locations, data=dat)
aov(Y~B+Locations+B/Times+B:Locations+B/Times:Locations, data=dat)
I seem to be unable to replicate Underwood's SS values exactly, particularly for the two interaction terms. A friend let me fit the model in statistix, where the SS values can be reproduced exactly, so it is possible to obtain the above SS values for this model.
Can anyone help me fit this model in R? I wish to embed it in a larger simulation and really need to be able to run the model in R, such that the Underwood 1993 SS values are reproduced exactly?
Your problem is that dat$Locations is an integer, when it should be a factor (three unique locations). One hint is that your ANOVA line thinks Locations takes up only 1 df, while Underwood gives it 2.
Simply add the line:
dat$Locations = factor(dat$Locations)
And then your line of code reproduces the Underwood results perfectly:
aov(Y~B+Locations+B/Times+B:Locations+B/Times:Locations, data=dat)
#Call:
# aov(formula = Y ~ B + Locations + B/Times + B:Locations + B/Times:Locations,
# data = dat)
#
#Terms:
# B Locations B:Times B:Locations B:Locations:Times
#Sum of Squares 66.1250 2836.8611 280.6389 29.2500 575.4444
#Deg. of Freedom 1 2 6 2 12
# Residuals
#Sum of Squares 2420.0000
#Deg. of Freedom 48

Resources