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]]
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.
Try also: df1 <- data.frame(a = LETTERS[1:2], b = LETTERS[3:4], c = 1:2) df1$a <- as.character(df1$a) #if is factor df1$b <- as.character(df1$b) #if is factor df1[df1$c == 2, ] <- c(subset(df1, c == 2, select = c("b", "a")), 2) On Thu, Apr 17, 2008 at 11:52 AM, Michael Kubovy <kubovy@virginia.edu> wrote:> 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/<http://www.people.virginia.edu/%7Emk9y/> > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]