Tuesday 15 January 2013

adding a new column after performig subtraction and division between two columns in R -



adding a new column after performig subtraction and division between two columns in R -

i have info set :

dataset a,

id bidding_price sale_price 10 74.88 67.27 11 23.1 18.14 12 62.5 56.14 13 34.5 27.09 14 55.32 49.69 15 900 706.77 16 260.84 260.84

i add together column diff performing next operation

diff =(bidding_price-sale_price)/(sale_price*100%)

and output should this:

id bidding_price sale_price diff 10 74.88 67.27 0.113126208 11 23.1 18.14 0.273428886 12 62.5 56.14 0.113288208 13 34.5 27.09 0.273532669 14 55.32 49.69 0.113302475 15 900 706.77 0.273398701 16 260.84 260.84 0.00

any help on appreciated.

use awesome dplyr package

library(dplyr) df <- data.frame("id"=c(1, 2, 3), "bidding_price"=c(10,11,12), "sale_price"=c(9,10,11)) df <- mutate(df, diff=(bidding_price - sale_price)/sale_price)

the output is

id bidding_price sale_price diff 1 1 10 9 0.11111111 2 2 11 10 0.10000000 3 3 12 11 0.09090909

r

No comments:

Post a Comment