[R] transform a df with a condition

Patrick Hausmann patrick.hausmann at uni-bremen.de
Sun Jan 16 15:19:01 CET 2011


Arrg, sorry - of course I don't want *new* variables. So this is my 
correct example:

df <- data.frame(A = c(1,1,3,2,2,3,3),
                  B = c(2,1,1,2,7,8,7),
                  K = c("a.1", "d.2", "f.3",
                        "a.1", "k.4", "f.9", "f.5"))

x1 <- within(df[df$A ==3, ], {
           B <- 5
           K <- gsub("f","m", K)
  })

x2 <- transform(df[df$A==3, ], B = 5, K = gsub("f","m", K))

Thanks
Patrick

Am 16.01.2011 15:13, schrieb Patrick Hausmann:
> Dear all,
>
> for each A == 3 in 'df' I would like to change the variables B and K.
> My result should be the whole df and not the subset (A==3)...
>
> df <- data.frame(A = c(1,1,3,2,2,3,3),
> B = c(2,1,1,2,7,8,7),
> K = c("a.1", "d.2", "f.3",
> "a.1", "k.4", "f.9", "f.5"))
>
> x1 <- within(df[df$A ==3, ], {
> B1 <- 5
> K1 <- gsub("f","m", K)
> })
>
> x2 <- transform(df[df$A==3, ], B1 = 5, K1 = gsub("f","m", K))
>
> Thanks for any help!
> Patrick
>
> ______________________________________________
> 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