Re: [R] change values in data frames

From: Kenn Konstabel <lebatsnok_at_gmail.com>
Date: Sun, 29 Jun 2008 00:26:07 +0300

Another way to do it (a more or less straightforward translation of what you said):

fun <- function(x) {

  xp <- c("", x)[-(length(x)+1)]                  # to get the "previous
element of x"
  x[1] <- if(x[1]=="i") "l" else x[1]
  x <- ifelse(x == "i" & xp == "c", "l", x)
  x <- ifelse(x == "i" & xp == "i", "h", x)
  x
  }

fun(X$colb)
-- or if I understood you correctly, you would actually want this to be done separately within each level of cola: unlist(tapply(X$colb, X$cola, fun))

Kenn

On Fri, Jun 27, 2008 at 12:17 PM, poul <morteterre_at_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.
>

        [[alternative HTML version deleted]]



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 - 21:31:57 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 - 22:31:20 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