Re: [R] Conditionally swap items in a data frame

From: Mark Leeds <markleeds_at_verizon.net>
Date: Thu, 17 Apr 2008 11:24:44 -0400

temp<-df1$a
df$a<-ifelse(df1$c == 2, df$b, df$a)
df$b<-ifelse(df1$c == 2, temp, df$b)


-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Michael Kubovy
Sent: Thursday, April 17, 2008 10:53 AM
To: r-help_at_stat.math.ethz.ch
Subject: [R] Conditionally swap items in a data frame

df1 <- data.frame(a = LETTERS[1:2], b = LETTERS[3:4], c = 1:2)

I am looking for an idiom that swaps the elements of df$a and df$b when (e.g.) df$c == 2, resulting in

df2 <- data.frame(a = LETTERS[c(1, 4)], b = LETTERS[c(3, 2)], c = 1:2)



Professor Michael Kubovy
University of Virginia
Department of Psychology
USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
Parcels:    Room 102        Gilmer Hall
         McCormick Road    Charlottesville, VA 22903
Office:    B011    +1-434-982-4729
Lab:        B019    +1-434-982-4751
Fax:        +1-434-982-4766

WWW: http://www.people.virginia.edu/~mk9y/

        [[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.

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 Thu 17 Apr 2008 - 16:38:03 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 Thu 17 Apr 2008 - 17:30:29 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