Re: [R] Subtracting elements of data.frame

2011-01-25 Thread Dennis Murphy
Hi:

 df = data.frame(x = c(1, 14, 3, 21, 11), y = c(102, 500, 40, 101, 189))
 apply(df, 2, function(x) x - x[1])
  x   y
[1,]  0   0
[2,] 13 398
[3,]  2 -62
[4,] 20  -1
[5,] 10  87

HTH,
Dennis

On Tue, Jan 25, 2011 at 1:20 AM, Vincy Pyne vincy_p...@yahoo.ca wrote:

 Dear R helpers

 I have a dataframe as

 df = data.frame(x = c(1, 14, 3, 21, 11), y = c(102, 500, 40, 101, 189))

  df
x   y
 1  1 102
 2 14 500
 3  3  40
 4 21 101
 5 11 189

 # Actually I am having dataframe having multiple columns. I am just giving
 an example.

 I need to subtract all the rows of df by the first row of df i.e. I need to
 subtract each element of 'x' column by 1. Likewise I need to subtract all
 elements of column 'y' by 11. Thus I need an output like

  df_new
x   y
 1  0   0
 2 13 398
 3  2 -62
 4 20  -1
 5 10  87

 As I had mentioned above, I have number of columns in reality and thus I
 can't use the command
  say

 df_new = data.frame(x = df$x-df$x[1], y = df$y-df$y[1])

 Kindly guide

 Thanking you all in advance

 Regards

 Vincy



[[alternative HTML version deleted]]


 __
 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]]

__
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.


Re: [R] Subtracting elements of data.frame

2011-01-25 Thread Ivan Calandra

Hi,

Try this:
df_new - as.data.frame(lapply(df, FUN=function(x) x-x[1]))

I hope it works!
Ivan

Le 1/25/2011 10:20, Vincy Pyne a écrit :

Dear R helpers

I have a dataframe as

df = data.frame(x = c(1, 14, 3, 21, 11), y = c(102, 500, 40, 101, 189))


df

x   y
1  1 102
2 14 500
3  3  40
4 21 101
5 11 189

# Actually I am having dataframe having multiple columns. I am just giving an 
example.

I need to subtract all the rows of df by the first row of df i.e. I need to 
subtract each element of 'x' column by 1. Likewise I need to subtract all 
elements of column 'y' by 11. Thus I need an output like


df_new

x   y
1  0   0
2 13 398
3  2 -62
4 20  -1
5 10  87

As I had mentioned above, I have number of columns in reality and thus I can't 
use the command
  say

df_new = data.frame(x = df$x-df$x[1], y = df$y-df$y[1])

Kindly guide

Thanking you all in advance

Regards

Vincy



[[alternative HTML version deleted]]



__
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.


--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php

__
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.


Re: [R] Subtracting elements of data.frame

2011-01-25 Thread Ivan Calandra

I always forget about sapply():
df_new - sapply(df, FUN=function(x) x-x[1])

Ivan

Le 1/25/2011 10:33, Ivan Calandra a écrit :

Hi,

Try this:
df_new - as.data.frame(lapply(df, FUN=function(x) x-x[1]))

I hope it works!
Ivan

Le 1/25/2011 10:20, Vincy Pyne a écrit :

Dear R helpers

I have a dataframe as

df = data.frame(x = c(1, 14, 3, 21, 11), y = c(102, 500, 40, 101, 189))


df

x   y
1  1 102
2 14 500
3  3  40
4 21 101
5 11 189

# Actually I am having dataframe having multiple columns. I am just 
giving an example.


I need to subtract all the rows of df by the first row of df i.e. I 
need to subtract each element of 'x' column by 1. Likewise I need to 
subtract all elements of column 'y' by 11. Thus I need an output like



df_new

x   y
1  0   0
2 13 398
3  2 -62
4 20  -1
5 10  87

As I had mentioned above, I have number of columns in reality and 
thus I can't use the command

  say

df_new = data.frame(x = df$x-df$x[1], y = df$y-df$y[1])

Kindly guide

Thanking you all in advance

Regards

Vincy



[[alternative HTML version deleted]]



__
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.




--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php

__
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.


Re: [R] Subtracting elements of data.frame

2011-01-25 Thread Peter Ehlers

On 2011-01-25 01:20, Vincy Pyne wrote:

Dear R helpers

I have a dataframe as

df = data.frame(x = c(1, 14, 3, 21, 11), y = c(102, 500, 40, 101, 189))


df

x   y
1  1 102
2 14 500
3  3  40
4 21 101
5 11 189

# Actually I am having dataframe having multiple columns. I am just giving an 
example.

I need to subtract all the rows of df by the first row of df i.e. I need to 
subtract each element of 'x' column by 1. Likewise I need to subtract all 
elements of column 'y' by 11. Thus I need an output like


df_new

x   y
1  0   0
2 13 398
3  2 -62
4 20  -1
5 10  87

As I had mentioned above, I have number of columns in reality and thus I can't 
use the command
  say

df_new = data.frame(x = df$x-df$x[1], y = df$y-df$y[1])

Kindly guide


You've already had good simple solutions. Here's one more,
using the plyr package:

 require(plyr)
 df_new - colwise(.fun = function(x) {x - x[1]})(df)

plyr is very handy for all sorts of data manipulations.

Peter Ehlers



Thanking you all in advance

Regards

Vincy



[[alternative HTML version deleted]]



__
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.


Re: [R] Subtracting elements of data.frame

2011-01-25 Thread Henrique Dallazuanna
Try this:

sweep(as.matrix(df), 2, as.matrix(df[1,]))

On Tue, Jan 25, 2011 at 7:20 AM, Vincy Pyne vincy_p...@yahoo.ca wrote:

 Dear R helpers

 I have a dataframe as

 df = data.frame(x = c(1, 14, 3, 21, 11), y = c(102, 500, 40, 101, 189))

  df
x   y
 1  1 102
 2 14 500
 3  3  40
 4 21 101
 5 11 189

 # Actually I am having dataframe having multiple columns. I am just giving
 an example.

 I need to subtract all the rows of df by the first row of df i.e. I need to
 subtract each element of 'x' column by 1. Likewise I need to subtract all
 elements of column 'y' by 11. Thus I need an output like

  df_new
x   y
 1  0   0
 2 13 398
 3  2 -62
 4 20  -1
 5 10  87

 As I had mentioned above, I have number of columns in reality and thus I
 can't use the command
  say

 df_new = data.frame(x = df$x-df$x[1], y = df$y-df$y[1])

 Kindly guide

 Thanking you all in advance

 Regards

 Vincy



[[alternative HTML version deleted]]


 __
 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]]

__
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.