[R] Replacing a value in a dataframe

Uwe Ligges ligges at statistik.tu-dortmund.de
Wed Nov 30 13:03:31 CET 2011



On 30.11.2011 12:26, Bert Gunter wrote:
> er... Uwe, shouldn't that be, e.g.
>
> dataframe$Cheque<- as.integer(dataframe$Cheque)


Sure, thanks.

> ## or building on Rolf's suggestion
>
> dataframe<- within(dataframe, Cheque<- as.integer(Cheque))
>
> While I am at it, is there any practical difference in efficiency
> between these two approaches?

Well, just profile it. The latter has some overhead, of course:

  d <- data.frame(a=c(TRUE, FALSE))
  system.time(for(i in 1:1e4) {d <- data.frame(a=c(TRUE, FALSE)); d$a <- 
as.integer(d$a)})
  system.time(for(i in 1:1e4) {d <- data.frame(a=c(TRUE, FALSE)); d <- 
within(d, a <- as.integer(a))})


Uwe Ligges



> -- Bert
>
>
> 2011/11/30 Uwe Ligges<ligges at statistik.tu-dortmund.de>:
>>
>>
>> On 30.11.2011 09:16, arunkumar1111 wrote:
>>>
>>> hi
>>>
>>> I have data like this in a dataframe
>>>
>>> Var  Value Cheque
>>> X1    40    FALSE
>>> X2    20    FALSE
>>> X3    28    TRUE
>>>
>>> I want to replace it FLASE  with 0 and TRUE with 1.
>>>
>>> is there any method by which i can do without using LOOP
>>
>>
>>
>> dataframe$Cheque<- as.integer(Cheque)
>>
>> Uwe Ligges
>>
>>
>>>
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/Replacing-a-value-in-a-dataframe-tp4122188p4122188.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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.
>>
>>
>> ______________________________________________
>> 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.
>
>
>



More information about the R-help mailing list