ruipbarradas at sapo.pt
2016-Apr-07 13:43 UTC
[R] simple question on data frames assignment
Hello, Or even simpler, without ifelse, colordata$response <- colordata$color == 'blue' + 0 Hope this helps, Rui Barradas ? Citando David Barron <dnbarron at gmail.com>:> 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 <michaeleartz at 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 at 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 at 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.htmland provide commented, > minimal, self-contained, reproducible code.? [[alternative HTML version deleted]]
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, <ruipbarradas at sapo.pt> wrote:> Hello, > > Or even simpler, without ifelse, > > colordata$response <- colordata$color == 'blue' + 0 > > Hope this helps, > > Rui Barradas > > > Citando David Barron <dnbarron at gmail.com>: > > 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 <michaeleartz at 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 at 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 at 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.htmland provide commented, > minimal, self-contained, reproducible code. > > > >[[alternative HTML version deleted]]
ruipbarradas at sapo.pt
2016-Apr-08 10:58 UTC
[R] simple question on data frames assignment
Hello, You're right, sorry, I missed the parenthesis: colordata$response <- (colordata$color == 'blue') + 0 Rui Barradas Quoting Michael Artz <michaeleartz at gmail.com>:> 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, <ruipbarradas at sapo.pt> wrote: >> >> Hello, >> >> Or even simpler, without ifelse, >> >> colordata$response <- colordata$color == 'blue' + 0 >> >> Hope this helps, >> >> Rui Barradas >> ? >> >> Citando David Barron <dnbarron at gmail.com>: >>> >>> 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 >>> <michaeleartz at 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 at 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 at 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.htmland provide commented, >>> minimal, self-contained, reproducible code. >> >> >> ? >> >> > > >[[alternative HTML version deleted]]