[R] Finding a Diff within a Dataframe columns

jim holtman jholtman at gmail.com
Mon Jan 31 04:28:08 CET 2011


try this:

> x
    A   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?



More information about the R-help mailing list