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.

Reply via email to