set.seed(2218)
exdf <- data.frame(
c(rep(1:105, 28)),
sort(c(rep(1:28, 105))),
sort(rep(rnorm(28), 105)),
sample(0:1, 105*28, replace=TRUE),
rep(rnorm(105), 28)
)
colnames(exdf) <- c("ID", "content", "b", "APMs", "Gf")
View(exdf)
This gives you a good idea of my dataset. Now I would like to turn it into something like this:
content b APMs
1 mean(b) mean(APMs)
2 mean(b) mean(APMs)
3 mean(b) mean(APMs)
... ... ...
28 mean(b) mean(APMs)
As you can see, Gf should be dropped, while I get the mean across 105 IDs for each of the 28 contents. The one solution that comes close is the following, but it can only deal with one variable, it seems.
library(reshape2)
itemwide <- dcast(
data= exdf,
formula= content ~ "b",
value.var= "b",
fun.aggregate= mean, na.rm=TRUE
)
View(itemwide)
Note to self:
what David actually means is this:
itemwide <- aggregate(
formula= cbind(b, APMs) ~ content,
data= exdf,
FUN= mean, na.rm=TRUE
)