Jay
Jay

Reputation: 9

Performing a function with 2 arguments with apply or aggregate

I have data frames with several columns used for splitting data (let's say 2 for now) in addition to "x" and "y" columns. I would like to perform function such as scatter plot or lm on "x" and "y" after subseting through the 2 columns.

I have been storing the "mini" data after each subset and calling further loops until I get the "x" and "y" I required, but it gets complicated after 3 or more loops. Recently, I tried to create a 'key' variable that stores all possible combinations.

Is there an easy way to split data first and call the function? perhaps an extension of apply or aggregate, but all factors to go through all combinations.

country <- rep(c("Australia","UK"), 2, each = 6)
gender <- rep(c("M","F"),1, each = 12)
X_height  <- rep(seq(150,200,10),4)
Y_weight  <- c(seq(70,95,5), 
               seq(71,96,5),
               seq(65,90,5),
               seq(66,91,5))



df <- data.frame(country, gender, X_height, Y_weight)

df[,"Key"] <- paste(df$country, df$gender, sep="_")

keys <- unique(df$Key)
par(mfrow = c(2,2))
sapply(1:length(keys), function(i){
  with(subset(df, Key == keys[i]),plot(x = X_height, Y_weight, main = keys[i]))
})

Thanks

Upvotes: 0

Views: 51

Answers (1)

jeremycg
jeremycg

Reputation: 24945

It sounds like you want the aggregating function, ave or the packages dplyr or data.table.

If you are just plotting, you can use ggplot2 with facet_wrap. Facet wrap takes a short formula based on what you want to group by:

library(ggplot2)
ggplot(df, aes(x = X_height, y = Y_weight)) +
       geom_line()  +
       facet_wrap(~country + gender, ncol = 2)
#the same as
ggplot(df, aes(x = X_height, y = Y_weight)) + 
       geom_line()  +
       facet_wrap(~Key, ncol = 2)

enter image description here

Upvotes: 1

Related Questions