Skip to content

Finding a Diff within a Dataframe columns

9 messages · Ramya, Santosh Srinivas, Henrique Dallazuanna +2 more

#
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
#
Please take a look at the introduction to R too ...
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
Error in d["A-B"] <- with(df, A - B) : object 'd' not found
Error in d$v1 <- with(df, A - B) : object 'd' not found
Error: object 'd' not found
A   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
A   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:
#
try this:
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
+ 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)
+     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 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
#
I see that others have already responded, but will add my point of
view.  You indicated that you wanted to take the difference between
pairs of columns and did not specify exactly how many there were; in
your example there were 4 columns (2 pairs).  If there were only two,
then the solution from Dennis would be the way to go because it is
explicit in what has to be done.  I just created a list of the "odd"
columns [seq(from = 1, by = 2, length = ncol(x) %/%2)] trying to take
care of any odd number of columns.  This was then used in the 'sapply'
to iterate over the columns.
On Sun, Jan 30, 2011 at 10:49 PM, Ramya <ramya.victory at gmail.com> wrote: