Hi, I have a Dataframe. A B C D 0.1 0.7 0.9 0.8 0.20 0.60 0.80 0.70 0.40 0.80 0.70 0.76 I need a resultant dataframe (A-B) (C-D) -0.6 0.1 -0.40 0.1 -0.40 -0.06 Any suggestion would be of a great help Thanks Ramya -- View this message in context: http://r.789695.n4.nabble.com/Finding-a-Diff-within-a-Dataframe-columns-tp3247943p3247943.html Sent from the R help mailing list archive at Nabble.com.
Please take a look at the introduction to R too ...> df <- read.delim(textConnection(Lines),sep="") > dfA B C D 1 0.1 0.7 0.9 0.80 2 0.2 0.6 0.8 0.70 3 0.4 0.8 0.7 0.76> d['A-B'] <- with(df, A-B)Error in d["A-B"] <- with(df, A - B) : object 'd' not found> d$v1 <- with(df, A-B)Error in d$v1 <- with(df, A - B) : object 'd' not found> dError: object 'd' not found> df$v1 <- with(df, A-B) > dfA B C D v1 1 0.1 0.7 0.9 0.80 -0.6 2 0.2 0.6 0.8 0.70 -0.4 3 0.4 0.8 0.7 0.76 -0.4> df$v2 <- with(df, C-D) > dfA B C D v1 v2 1 0.1 0.7 0.9 0.80 -0.6 0.10 2 0.2 0.6 0.8 0.70 -0.4 0.10 3 0.4 0.8 0.7 0.76 -0.4 -0.06 On Mon, Jan 31, 2011 at 7:02 AM, Ramya <ramya.victory at gmail.com> wrote:> > Hi, > > I have a Dataframe. > > A ? ? ? B ? ? ?C ? ? ? D > 0.1 ? ?0.7 ? 0.9 ? 0.8 > 0.20 ?0.60 0.80 ?0.70 > 0.40 ?0.80 ?0.70 0.76 > > I need a resultant dataframe > > (A-B) ? (C-D) > -0.6 ? ? 0.1 > -0.40 ? ?0.1 > -0.40 ? -0.06 > > Any suggestion would be of a great help > > Thanks > Ramya > > -- > View this message in context: http://r.789695.n4.nabble.com/Finding-a-Diff-within-a-Dataframe-columns-tp3247943p3247943.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. >
try this:> xA B C D 1 0.1 0.7 0.9 0.80 2 0.2 0.6 0.8 0.70 3 0.4 0.8 0.7 0.76> source('clipboard') > x <- read.table(textConnection("A B C D+ 0.1 0.7 0.9 0.8 + 0.20 0.60 0.80 0.70 + 0.40 0.80 0.70 0.76"), header = TRUE)> closeAllConnections() > sapply(seq(from = 1, by = 2, length = ncol(x) %/% 2), function(a){+ x[[a]] - x[[a + 1]] + }) [,1] [,2] [1,] -0.6 0.10 [2,] -0.4 0.10 [3,] -0.4 -0.06> >On Sun, Jan 30, 2011 at 8:32 PM, Ramya <ramya.victory at gmail.com> wrote:> > Hi, > > I have a Dataframe. > > A ? ? ? B ? ? ?C ? ? ? D > 0.1 ? ?0.7 ? 0.9 ? 0.8 > 0.20 ?0.60 0.80 ?0.70 > 0.40 ?0.80 ?0.70 0.76 > > I need a resultant dataframe > > (A-B) ? (C-D) > -0.6 ? ? 0.1 > -0.40 ? ?0.1 > -0.40 ? -0.06 > > Any suggestion would be of a great help > > Thanks > Ramya > > -- > View this message in context: http://r.789695.n4.nabble.com/Finding-a-Diff-within-a-Dataframe-columns-tp3247943p3247943.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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Hi jholtman, Thanks a ton it just worked. if you dont mind can you explain the code it a little sapply(seq(from = 1, by = 2, length = ncol(x) %/% 2), function(a){ + x[[a]] - x[[a + 1]] + }) wat is the purpose of double percent sign and is the sapply the function we generally use for the Dataframe? Thanks Ramya -- View this message in context: http://r.789695.n4.nabble.com/Finding-a-Diff-within-a-Dataframe-columns-tp3247943p3248066.html Sent from the R help mailing list archive at Nabble.com.
Hi: This also works on your example data. Using the 'x' data frame from Jim Holtman's post, subset(transform(x, diffAB = A - B, diffCD = C - D), select = c('diffAB', 'diffCD')) diffAB diffCD 1 -0.6 0.10 2 -0.4 0.10 3 -0.4 -0.06 transform() allows you to do the subtractions in one line, and subset() is used to select the difference variables. Not necessarily a 'better' solution, but rather another alternative. If you need to be taking numerous differences between odd numbered and even numbered columns of your data frame, then Jim's solution is ideal. HTH, Dennis On Sun, Jan 30, 2011 at 5:32 PM, Ramya <ramya.victory@gmail.com> wrote:> > Hi, > > I have a Dataframe. > > A B C D > 0.1 0.7 0.9 0.8 > 0.20 0.60 0.80 0.70 > 0.40 0.80 0.70 0.76 > > I need a resultant dataframe > > (A-B) (C-D) > -0.6 0.1 > -0.40 0.1 > -0.40 -0.06 > > Any suggestion would be of a great help > > Thanks > Ramya > > -- > View this message in context: > http://r.789695.n4.nabble.com/Finding-a-Diff-within-a-Dataframe-columns-tp3247943p3247943.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]
Try this: aggregate(t(x), list(gl(2, 2)), FUN = 'diff') On Sun, Jan 30, 2011 at 11:32 PM, Ramya <ramya.victory@gmail.com> wrote:> > Hi, > > I have a Dataframe. > > A B C D > 0.1 0.7 0.9 0.8 > 0.20 0.60 0.80 0.70 > 0.40 0.80 0.70 0.76 > > I need a resultant dataframe > > (A-B) (C-D) > -0.6 0.1 > -0.40 0.1 > -0.40 -0.06 > > Any suggestion would be of a great help > > Thanks > Ramya > > -- > View this message in context: > http://r.789695.n4.nabble.com/Finding-a-Diff-within-a-Dataframe-columns-tp3247943p3247943.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]