lapply(colordata2[ -1 ], f ) When you put the parentheses on, you are calling the function yourself before lapply gets a chance. The error pops up because you are giving a vector of numbers (the answer f gave you) to the second argument of lapply instead of a function. -- Sent from my phone. Please excuse my brevity.
On April 7, 2016 7:31:18 AM PDT, Michael Artz <michaelea...@gmail.com> wrote: >If you are not using an anonymous function and say you had written the >function out > >The below gives me the error > 'f(colordata2$color1)' is not a >function, >character or symbol' But then how is the anonymous function working? > > >f <- function(col){ > ifelse(col == 'blue', 1, 0) >} >responses <- lapply(colordata2[ -1 ], f(colordata2$color1) ) > >'f(colordata2$color1)' is not a function, character or symbol' > >then how could you then use this fuction in lapply if not for the >anonymous >function? > >On Thu, Apr 7, 2016 at 8:17 AM, Jeff Newmiller ><jdnew...@dcn.davis.ca.us> >wrote: > >> Lapply is not a vectorized function. It is compact to read, but it >would >> not be worth using for this calculation. >> >> However, if your data frame had multiple color columns in your data >frame >> that you wanted to make responses for then you might want to use >lapply as >> a more compact version of a for loop to repeat this operation. >> >> colordata2 <- data.frame(id = c(1,2,3,4,5), color1 = c("blue", "red", >> "green", "blue", "orange"), color2 = c("orange", "green", >> "blue", "red", "red")) >> responses <- lapply( colordata2[ -1 ], function(col) { ifelse(col == >> 'blue', 1, 0) } ) >> names(responses) <- names( colordata2 )[-1] >> >> where each of the columns other than the first is handed in turn to >the >> anonymous function that does the response calculation. The result is >a data >> frame (list of columns) with no column names, so I give the new >columns >> names based on the old column names. You could choose different >names, e.g. >> >> names(responses) <- paste0( "response", 1:2 ) >> >> but you have to be careful to fix that code whenever you change the >> colordata2 data frame to have more columns. >> -- >> Sent from my phone. Please excuse my brevity. >> >> On April 7, 2016 4:57:04 AM PDT, Michael Artz ><michaelea...@gmail.com> >> wrote: >>> >>> Thaks so much! And how would you incorporate lapply() here? >>> >>> On Thu, Apr 7, 2016 at 6:52 AM, David Barron <dnbar...@gmail.com> >wrote: >>> >>> ifelse is vectorised, so just use that without the loop. >>>> >>>> colordata$response <- ifelse(colordata$color == 'blue', 1, 0) >>>> >>>> David >>>> >>>> On 7 April 2016 at 12:41, Michael Artz <michaelea...@gmail.com> >wrote: >>>> >>>> Hi I'm not sure how to ask this, but its a very easy question to >answer >>>>> for >>>>> an R person. >>>>> >>>>> What is an easy way to check for a column value and then assigne >a new >>>>> column a value based on that old column value? >>>>> >>>>> For example, Im doing >>>>> colordata <- data.frame(id = c(1,2,3,4,5), >>>>> color = c("blue", "red", >>>>> "green", "blue", "orange")) >>>>> for (i in 1:nrow(colordata)){ >>>>> colordata$response[i] <- ifelse(colordata[i,"color"] == >"blue", 1, 0) >>>>> } >>>>> >>>>> which works, but I don't want to use the for loop I want to >"vecotrize" >>>>> this. How would this be implemented? >>>>> >>>>> [[alternative HTML version deleted]] >>>>> >>>>> ------------------------------ >>>>> >>>>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>>>> 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. >>>> >>>> >>>> >>> >>> [[alternative HTML version deleted]] >>> >>> ------------------------------ >>> >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> 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. >>> >>> [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.