Re: [R] simple question on data frames assignment

2016-04-08 Thread ruipbarradas
Hello, You're right, sorry, I missed the parenthesis: colordata$response <- (colordata$color == 'blue') + 0 Rui Barradas Quoting Michael Artz : > Fyi, This statement returned the following error   > 'Error in "Yes" + 0 : non-numeric argument to binary

Re: [R] simple question on data frames assignment

2016-04-08 Thread PIKAL Petr
Hi > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Michael > Artz > Sent: Friday, April 8, 2016 3:50 AM > To: Hadley Wickham <h.wick...@gmail.com> > Cc: r-help@r-project.org > Subject: Re: [R] simple question on data fra

Re: [R] simple question on data frames assignment

2016-04-08 Thread PIKAL Petr
ybe you shall use dput(yourdata) output together with desired result to help us better understand your task. Cheers Petr From: Michael Artz [mailto:michaelea...@gmail.com] Sent: Thursday, April 7, 2016 4:17 PM To: PIKAL Petr <petr.pi...@precheza.cz> Subject: Re: [R] simple question on data

Re: [R] simple question on data frames assignment

2016-04-07 Thread Michael Artz
Why am I better off with true and false? On Thu, Apr 7, 2016 at 8:41 AM, Hadley Wickham wrote: > == is also vectorised, and you're better off with TRUE and FALSE > rather than 1 and 0, so I'd recommend: > > colordata$response <- colordata$color == 'blue' > > Hadley > > On

Re: [R] simple question on data frames assignment

2016-04-07 Thread Michael Artz
Fyi, This statement returned the following error 'Error in "Yes" + 0 : non-numeric argument to binary operator' On Thu, Apr 7, 2016 at 8:43 AM, wrote: > Hello, > > Or even simpler, without ifelse, > > colordata$response <- colordata$color == 'blue' + 0 > > Hope this

Re: [R] simple question on data frames assignment

2016-04-07 Thread Michael Artz
It all makes so much sense now On Thu, Apr 7, 2016 at 10:04 AM, Jeff Newmiller wrote: > 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 >

Re: [R] simple question on data frames assignment

2016-04-07 Thread Jeff Newmiller
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.

Re: [R] simple question on data frames assignment

2016-04-07 Thread Michael Artz
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 <-

Re: [R] simple question on data frames assignment

2016-04-07 Thread ruipbarradas
Hello, Or even simpler, without ifelse, colordata$response <- colordata$color == 'blue' + 0 Hope this helps, Rui Barradas   Citando David Barron : > ifelse is vectorised, so just use that without the loop. > > colordata$response <- ifelse(colordata$color == 'blue', 1, 0)

Re: [R] simple question on data frames assignment

2016-04-07 Thread Hadley Wickham
== is also vectorised, and you're better off with TRUE and FALSE rather than 1 and 0, so I'd recommend: colordata$response <- colordata$color == 'blue' Hadley On Thu, Apr 7, 2016 at 6:52 AM, David Barron wrote: > ifelse is vectorised, so just use that without the loop. > >

Re: [R] simple question on data frames assignment

2016-04-07 Thread Jeff Newmiller
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

Re: [R] simple question on data frames assignment

2016-04-07 Thread PIKAL Petr
Hi > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Michael > Artz > Sent: Thursday, April 7, 2016 1:57 PM > To: David Barron <dnbar...@gmail.com> > Cc: r-help@r-project.org > Subject: Re: [R] simple question on data fra

Re: [R] simple question on data frames assignment

2016-04-07 Thread Michael Artz
Thaks so much! And how would you incorporate lapply() here? On Thu, Apr 7, 2016 at 6:52 AM, David Barron 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

Re: [R] simple question on data frames assignment

2016-04-07 Thread David Barron
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 wrote: > Hi I'm not sure how to ask this, but its a very easy question to answer for > an R person. > >

[R] simple question on data frames assignment

2016-04-07 Thread Michael Artz
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",