Dear group, I am losing my mind with a simple question. Sorry if obvious, but I maybe start to be confused after days and days of reading documentations. Df : df <- structure(list(a = 1:3, b = 4:6, c = structure(c(1L, 1L, 1L), class "factor", .Label = "w")), .Names = c("a", "b", "c"), row.names = c(NA, -3L), class = "data.frame") I want to multiply first and second by -1.> x$a<-x$a*-1 > x$b<-x$b*-1This returns what I want, but there must be an one line command to do it, right? TY for help
On Apr 30, 2010, at 9:08 AM, arnaud Gaboury wrote:> Dear group, > > I am losing my mind with a simple question. Sorry if obvious, but I > maybe > start to be confused after days and days of reading documentations. > > Df : > > > df <- > structure(list(a = 1:3, b = 4:6, c = structure(c(1L, 1L, 1L), class > "factor", .Label = "w")), .Names = c("a", > "b", "c"), row.names = c(NA, -3L), class = "data.frame") > > I want to multiply first and second by -1. > >> x$a<-x$a*-1 >> x$b<-x$b*-1 > > This returns what I want, but there must be an one line command to > do it, > right?> df[ , -3] <- df[ , -3]*-1 > df a b c 1 -1 -4 w 2 -2 -5 w 3 -3 -6 w>-- David Winsemius, MD West Hartford, CT
df[1:2]<-df[1:2]*-1 ----- Lanna Jin lannajin at gmail.com 510-898-8525 -- View this message in context: http://r.789695.n4.nabble.com/short-question-about-data-frame-manipulation-tp2076891p2076899.html Sent from the R help mailing list archive at Nabble.com.
Hi: On Fri, Apr 30, 2010 at 6:08 AM, arnaud Gaboury <arnaud.gaboury@gmail.com>wrote:> Dear group, > > I am losing my mind with a simple question. Sorry if obvious, but I maybe > start to be confused after days and days of reading documentations. > > Df : > > > df <- > structure(list(a = 1:3, b = 4:6, c = structure(c(1L, 1L, 1L), class > "factor", .Label = "w")), .Names = c("a", > "b", "c"), row.names = c(NA, -3L), class = "data.frame") > > I want to multiply first and second by -1. > > > x$a<-x$a*-1 > > x$b<-x$b*-1 >This is a rather dangerous syntax; if you must write it this way, it's better to enclose the -1 in parentheses so that the intent is clear. One way is, as others have suggested, x[, 1:2] <- x[, 1:2] * (-1) For this example, it's just as simple to write x[, 1:2] <- -df[, 1:2] and note that I made sure to space between the assignment and the minus sign for readability... HTH, Dennis> > This returns what I want, but there must be an one line command to do it, > right? > > TY for help > > ______________________________________________ > 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. >[[alternative HTML version deleted]]