[R] Using a function inside a function

Joshua Wiley jwiley.psych at gmail.com
Thu Jun 9 06:28:37 CEST 2011


Hi Abraham,

Your example runs fine for me.  I get this as the newdf object (you
should be able to copy and paste into your console):

newdf <- structure(list(keyword = structure(c(7L, 3L, 5L, 1L, 8L, 4L,
6L, 2L, 15L, 11L, 13L, 9L, 16L, 12L, 14L, 10L, 20L, 18L, 19L,
17L, 23L, 21L, 24L, 22L, 27L, 25L, 28L, 26L), .Label = c("budget auto
insurance quote",
"budget auto insurance quotes", "budget car insurance quote",
"budget car insurance quotes", "cheap auto insurance quote",
"cheap auto insurance quotes", "cheap car insurance quote", "cheap car
insurance quotes",
"budget auto insurance rate", "budget auto insurance rates",
"budget car insurance rate", "budget car insurance rates", "cheap auto
insurance rate",
"cheap auto insurance rates", "cheap car insurance rate", "cheap car
insurance rates",
"budget auto insurance comparison", "budget car insurance comparison",
"cheap auto insurance comparison", "cheap car insurance comparison",
"low cost auto insurance quote", "low cost auto insurance quotes",
"low cost car insurance quote", "low cost car insurance quotes",
"low cost auto insurance rate", "low cost auto insurance rates",
"low cost car insurance rate", "low cost car insurance rates"
), class = "factor")), .Names = "keyword", row.names = c(NA,
28L), class = "data.frame")

Try re-running the example you gave in your email in a clean R
session, perhaps.  If that does not work, you will need to provide
more information.

Cheers,

Josh

On Wed, Jun 8, 2011 at 5:43 PM, Abraham Mathew <abmathewks at gmail.com> wrote:
> I'm trying to run a function inside a function but get an error message.
>
> lst <- list(roots = c("car insurance", "auto insurance"),
> roots2 = c("insurance"), prefix = c("cheap", "budget"),
> prefix2 = c("low cost"), suffix = c("quote", "quotes"),
> suffix2 = c("rate", "rates"), suffix3 = c("comparison"))
>
> myfunc <- function(lst) {
>      myone <- function(x, y) {
>            nu <- do.call(paste, expand.grid(x, y))
>            mydf <- data.frame(keyword=c(nu))
>            }
>     mytwo <- function(x, y, z){
>           mu <- do.call(paste, expand.grid(x, y, z))
>           mydf2 <- data.frame(keyword=c(mu))
>           }
>     d1 = mytwo(lst$prefix, lst$roots, lst$suffix)
>     d2 = mytwo(lst$prefix, lst$roots, lst$suffix2)
>     d3 = mytwo(lst$prefix, lst$roots, lst$suffix3)
>     d4 = mytwo(lst$prefix2, lst$roots, lst$suffix)
>     d5 = mytwo(lst$prefix2, lst$roots, lst$suffix2)
>     df = rbind(d1, d2, d3, d4, d5)
> }
>
>
> I get the following error message:
>
>> newdf <- myfunc(lst)
> Error in expand.grid(x, y) : object 'x' not found
>
>
> Can anyone help!
> I'm running R 2.13 on Ubuntu 10.10
>
>
> Abraham
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list