Hi Abraham, mylist <- list(roots = "car", prefix = "cheap") myfoo <- function(x) { print(mylist$x) }
myfoo(roots) ## fails, but in a "sneaky" way ## you actually extract variable x from mylist ## but there is no variable x (it is just NULL) ## so while no error is thrown, you get nothing myfoo <- function(x) { print(mylist[[x]]) } myfoo(roots) ## still fails but... myfoo("roots") ## works So I would rewrite 'myone' and 'mytwo' to use: lst[[x]] and then put the quotes around the names mytwo("prefix", "roots2", "suffix") etc. Also look at ?debug to see ways to debug your function so you can more easily find where the problem starts rather than what the final death blow was. The usage is simple, just: debug(myfunc) ## now next time myfunc() is called, it will be debugged myfunc(lst) ## and off you go Hope this helps, Josh On Wed, Jun 8, 2011 at 8:45 PM, Abraham Mathew <abmathe...@gmail.com> wrote: > I'm writing a function and keep getting the following error message. > > myfunc <- function(lst) { > 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")) > myone <- function(x, y) { > nu <- do.call(paste, expand.grid(lst$x, lst$y)) > mydf <- data.frame(keyword=c(nu)) > } > mytwo <- function(x, y, z){ > mu <- do.call(paste, expand.grid(lst$x, lst$y, lst$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) > d6 = mytwo(prefix2, roots, suffix3) > d7 = mytwo(prefix, roots2, suffix) > d8 = mytwo(prefix, roots2, suffix2) > d9 = mytwo(prefix, roots2, suffix3) > d10 = mytwo(prefix2, roots2, suffix) > d11 = mytwo(prefix2, roots2, suffix2) > d12 = mytwo(prefix2, roots2, suffix3) > d13 = myone(prefix, roots) > d14 = myone(prefix2, roots) > d15 = myone(prefix, roots2) > d16 = myone(prefix2, roots2) > d17 = myone(roots, suffix) > d18 = myone(roots, suffix2) > d19 = myone(roots, suffix3) > d20 = myone(roots2, suffix) > d21 = myone(roots2, suffix2) > d22 = myone(roots2, suffix3) > d23 = myone(state, roots) > d24 = myone(city, roots) > d25 = myone(cityst, roots) > d26 = myone(inscompany, roots) > d27 = myone(state, roots2) > d28 = myone(city, roots2) > d29 = myone(cityst, roots2) > d30 = myone(inscompany, roots2) > d31 = mytwo(state, roots, suffix) > d32 = mytwo(city, roots, suffix) > d33 = mytwo(cityst, roots, suffix) > d34 = mytwo(inscompany, roots, suffix) > d35 = mytwo(state, roots, suffix2) > d36 = mytwo(city, roots, suffix2) > d37 = mytwo(cityst, roots, suffix2) > d38 = mytwo(inscompany, roots, suffix2) > d39 = mytwo(state, roots, suffix3) > d40 = mytwo(city, roots, suffix3) > d41 = mytwo(cityst, roots, suffix3) > d42 = mytwo(inscompany, roots, suffix3) > d43 = mytwo(state, roots2, suffix) > d44 = mytwo(city, roots2, suffix) > d45 = mytwo(cityst, roots2, suffix) > d46 = mytwo(inscompany, roots2, suffix) > d47 = mytwo(state, roots2, suffix2) > d48 = mytwo(city, roots2, suffix2) > d49 = mytwo(cityst, roots2, suffix2) > d50 = mytwo(inscompany, roots2, suffix2) > d51 = mytwo(state, roots2, suffix3) > d52 = mytwo(city, roots2, suffix3) > d53 = mytwo(cityst, roots2, suffix3) > d54 = mytwo(inscompany, roots2, suffix3) > d55 = mytwo(prefix, state, roots) > d56 = mytwo(prefix, city, roots) > d57 = mytwo(prefix, cityst, roots) > d58 = mytwo(prefix, inscompany, roots) > d59 = mytwo(prefix2, state, roots) > d60 = mytwo(prefix2, city, roots) > d61 = mytwo(prefix2, cityst, roots) > d62 = mytwo(prefix2, inscompany, roots) > d63 = mytwo(prefix, state, roots2) > d64 = mytwo(prefix, city, roots2) > d65 = mytwo(prefix, cityst, roots2) > d66 = mytwo(prefix, inscompany, roots2) > d67 = mytwo(prefix2, state, roots2) > d68 = mytwo(prefix2, city, roots2) > d69 = mytwo(prefix2, cityst, roots2) > d70 = mytwo(prefix2, inscompany, roots2) > mydf <- rbind(d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, > d15, > d16, d17, d18, d19, d20, d21, d22, d23, d24, d25, d26, d27, d28, d29, > d30, d31, d32, d33, d34, d35, d36, d37, d38, d39, d40, d41, d42, d43, > d44, d45, d46, d47, d48, d49, d50, d51, d52, d53, d54, d55, d56, d57, > d58, d59, d60, d61, d62, d63, d64, d65, d66, d67, d68, d69, d70) > library(stringr) > inscompany_match <- str_c(inscompany, collapse = "|") > state_match <- str_c(state, collapse = "|") > city_match <- str_c(city, collapse = "|") > mydf$inscompany <- as.numeric(str_detect(mydf$keyword, inscompany_match)) > mydf$state <- as.numeric(str_detect(mydf$keyword, state_match)) > mydf$city <- as.numeric(str_detect(mydf$keyword, city_match)) > for (i in 1:nrow(mydf)) { > Words = strsplit(as.character(mydf[i, 'keyword']), " ")[[1]] > if(any(Words == 'Colorado')){ > if(Words[which(Words == 'Colorado') + 1] == 'Springs') mydf[i, 'state'] <- 0 > } > if(any(Words == 'Virginia')){ > if(Words[which(Words == 'Virginia') + 1] == 'Beach') mydf[i, 'state'] <- 0 > } > if(any(Words == 'Oklahoma')){ > if(Words[which(Words == 'Oklahoma') + 1] == 'City') mydf[i, 'state'] <- 0 > } > if(any(Words == 'Kansas')){ > if(Words[which(Words == 'Kansas') + 1] == 'City') mydf[i, 'state'] <- 0 > } > if(any(Words == 'Washington')){ > if(Words[which(Words == 'Washington') + 1] == 'DC') mydf[i, 'state'] <- 0 > } > if(any(Words == 'York')){ > if(Words[which(Words == 'York') + 1] == 'City') mydf[i, 'state'] <- 0 > } > if(any(Words == "Indianapolis")){ > mydf[i, 'state'] <- 0 > } > if(any(Words == "AARP")){ > mydf[i, 'state'] <- 0 > } > if(any(Words == "ANPAC")){ > mydf[i, 'state'] <- 0 > } > if(any(Words == "AMICA")){ > mydf[i, 'state'] <- 0 > } > if(any(Words == "GMAC")){ > mydf[i, 'state'] <- 0 > } > if(any(Words == "USAA")){ > mydf[i, 'state'] <- 0 > } > return(mydf) > } > } > > > newdf <- myfunc(lst) > > >> newdf <- myfunc(lst) > Error in if (any(Words == "Colorado")) { : > missing value where TRUE/FALSE needed > > What's going on? > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ ______________________________________________ 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.