name_masked
name_masked

Reputation: 9794

Parameterize ddply in R

For the data frame:

exampleDF <- structure(list(val1 = structure(c(1L, 2L, 1L, 3L), .Label = c("MX", 
"SS", "VF"), class = "factor"), var2 = c(1, 2, 3, 4)), .Names = c("val1", 
"var2"), row.names = c(NA, -4L), class = "data.frame")

instead of doing:

ddply(exampleDF, .(val1), summarize, sum(as.numeric(var2)))

Is it possible to parameterize the ddply call (something as follows, though I tried it and didn't work):

colname <- 'var2'
ddply(exampleDF, .(val1), summarize, sum(as.numeric(colname)))

which results in ..

  val1 ..1
1   MX  NA
2   SS  NA
3   VF  NA
Warning messages:
1: In eval(expr, envir, enclos) : NAs introduced by coercion
2: In eval(expr, envir, enclos) : NAs introduced by coercion
3: In eval(expr, envir, enclos) : NAs introduced by coercion

We have to call ddply for a set of columns in the data frame and generate plots for each result of ddply. Hence we wanted to parameterize the ddply call instead of repeating the same line for n number of columns

Upvotes: 0

Views: 514

Answers (1)

Paul Hiemstra
Paul Hiemstra

Reputation: 60944

This is actually a challenge with summarize, not ddply. You could try and do something with parse and eval, but in general this is not a good idea. I would do something like:

colname <- 'var2'
ddply(exampleDF, .(val1), function(sub_dat) sum(as.numeric(sub_dat[[colname]])))

You say you want to create multiple plots like this, however, I've almost always been able to create such a set of plots using facetting in ggplot2 (created by the same author as plyr). See e.g. the documentation of facet_wrap and facet_grid.

Upvotes: 2

Related Questions