Finding a Diff within a Dataframe columns

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Finding a Diff within a Dataframe columns

Ramya
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Finding a Diff within a Dataframe columns

Santosh Srinivas
Please take a look at the introduction to R too ...

> df <- read.delim(textConnection(Lines),sep="")
> df
    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
> 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
> d
Error: object 'd' not found
> df$v1 <- with(df, A-B)
> df
    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
> df$v2 <- with(df, C-D)
> df
    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 <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Finding a Diff within a Dataframe columns

jholtman
In reply to this post by Ramya
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 <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Finding a Diff within a Dataframe columns

Ramya
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Finding a Diff within a Dataframe columns

djmuseR
In reply to this post by Ramya
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 <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Finding a Diff within a Dataframe columns

Henrique Dallazuanna
In reply to this post by Ramya
Try this:


aggregate(t(x), list(gl(2, 2)), FUN = 'diff')

On Sun, Jan 30, 2011 at 11:32 PM, Ramya <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Finding a Diff within a Dataframe columns

djmuseR
In reply to this post by Ramya
Hi:

I won't speak for Jim, as he's more than capable of responding to this
himself, but I'll give it a shot:

(1) It's not just the 'double percent sign', it's that slash in between
them, too. Certain operators in R happen to be enclosed between two percent
signs.

%/% is the integer division operator in R; it returns the integer part of
the result:
> 10 %/% 2
[1] 5
> 9 %/% 2       # integer part of 4.5
[1] 4
> 11 %/% 2     # integer part of 5.5
[1] 5

%% is the modulo (remainder from integer division) operator in R:
> 10 %% 2
[1] 0
> 11 %% 2
[1] 1
> 12 %% 2
[1] 0

(2) The sapply() function is commonly used to apply a function across a
vector of arguments; e.g., many loops can be replaced with equivalent
sapply() statements. In this case, the sequence used as the first argument
of sapply() consists of the odd numbers up to the number of columns in the
matrix; for each element of that sequence, the function computes the
difference between that odd numbered column in the matrix and the adjacent
even numbered column to its right.

Henrique's solution does the same thing, except he uses a factor variable
(gl(2, 2)) to distinguish odd from even-numbered columns. It's useful to
look at both solutions for a general answer to the problem you posed.

HTH,
Dennis

On Sun, Jan 30, 2011 at 7:49 PM, Ramya <[hidden email]> 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
> --
> 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.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Finding a Diff within a Dataframe columns

Ramya
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Finding a Diff within a Dataframe columns

jholtman
In reply to this post by 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 <[hidden email]> 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
> --
> 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.
>
> ______________________________________________
> [hidden email] 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?

______________________________________________
[hidden email] 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.