Re: [R] change values in data frames

From: poul <morteterre_at_infonie.fr>
Date: Sat, 28 Jun 2008 08:31:31 -0700 (PDT)

Thanks a lot, it works perfectly .
Bye
Paul

jholtman wrote:
>
> Is this what you want:
>

>> x

> cola colb
> 1 1 c
> 2 1 i
> 3 1 i
> 4 1 c
> 5 2 i
> 6 2 c
> 7 2 c
> 8 2 i
> 9 2 i
>> # generate run lengths
>> z <- rle(x$colb)
>> # calculate offset of each starting sequence
>> offsets <- head(cumsum(c(1, z$lengths)), -1)
>> # now determine where the 'i' is more than 1
>> long <- which(z$lengths > 1 & z$values == 'i')
>> # now replace all 'i' with 'I'
>> x$colb[x$colb == 'i'] <- 'I'
>> # replace lon runs with 'h'
>> for (i in long){

> + x$colb[(offsets[i]+1):(offsets[i] + z$lengths[i] - 1)] <-
> rep('h', z$lengths[i] - 1)
> + }
>> x

> cola colb
> 1 1 c
> 2 1 I
> 3 1 h
> 4 1 c
> 5 2 I
> 6 2 c
> 7 2 c
> 8 2 I
> 9 2 h
>>
>>

>
>
> On Fri, Jun 27, 2008 at 5:17 AM, poul <morteterre@infonie.fr> wrote:
>>
>> Hello every body,
>> I am quite a new user of R so I beg your pardon for this naive question
>> and
>> the lake of syntax with wich I ask it:
>>
>> I have a data frames like this:
>>
>> cola    colb
>> 1       c
>> 1       i
>> 1       i
>> 1       c
>> 2       i
>> 2       c
>> 2       c
>> 2       i
>> 2       i
>> ...
>> 10000
>>
>> I would like ,for each level of cola and for x in colb:
>>
>> -if colb[x]=="i" and colb[x-1] does not exist (first row in dataframes),
>> then replace colb[x] by "l"
>> -if colb[x]=="i" and colb[x-1]=="c" (previous row in data frmes), then
>> replace colb[x] by "l"
>> -if colb[x]=="i" and colb[x-1]=="i" (previous row in data frmes), then
>> replace colb[x] by "h"
>>
>> Thanks in advance
>>
>> Paul
>>
>> --
>> View this message in context:
>> http://www.nabble.com/change-values-in-data-frames-tp18151205p18151205.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.
>>

>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>
> ______________________________________________
> 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.
>
>
-- 
View this message in context: http://www.nabble.com/change-values-in-data-frames-tp18151205p18171911.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.
Received on Sat 28 Jun 2008 - 15:36:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 28 Jun 2008 - 18:30:55 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive