[R] MARGIN in sweep refers to a specific column in a second df

Patrick Hausmann patrick.hausmann at uni-bremen.de
Mon Aug 15 18:27:20 CEST 2011


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



More information about the R-help mailing list