Dear R folks,
I am doing some calculations over an array using sweep and apply.
# Sample Data (from help 'addmargins')
Aye <- sample(c("Yes", "Si", "Oui"), 177, replace = TRUE)
Bee <- sample(c("Hum", "Buzz"), 177, replace = TRUE)
Sea <- sample(c("White", "Black", "Red", "Dead"), 177, replace = TRUE)
(A <- table(Aye, Bee, Sea))
apply(A, c(1, 2), sum )
## ok, sweep with fixed MARGIN
round( sweep( apply(A, c(1, 2), sum ), 1 , c(111, 333, 444), FUN = "/"), 2)
# DF with values for sweep MARGIN
DF <- data.frame( answer = c(111, 333, 444), Aye = c("Oui", "Si", "Yes"))
## ok, MARGIN in correct order
round( sweep( apply(A, c(1, 2), sum ), 1 , DF[['answer']], FUN = "/"), 2)
## But if I change the order in DF the result is not what I want...
DF.s <- DF[order(DF$Aye, decreasing = TRUE), ]
DF.s
round( sweep( apply(A, c(1, 2), sum ), 1 , DF.s[['answer']], FUN = "/"), 2)
So, I would like to know, how to set MARGIN in sweep to refer to the
values in DF with notice of the Aye-column?
Thanks for any help
Patrick
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.