dataframe - R: Create 2 columns with difference and percentages values of another column -


i have dataframe

id <- c(101,101,101,102,102,102,103,103,103) pt_a <- c(50,100,150,20,30,40,60,80,90) df <- data.frame(id,pt_a)  +-----+------+ | id  | pt_a | +-----+------+ | 101 |   50 | | 101 |  100 | | 101 |  150 | | 102 |   20 | | 102 |   30 | | 102 |   40 | | 103 |   60 | | 103 |   80 | | 103 |   90 | +-----+------+ 

i want create 2 new columns values calculated pt_a column.

df$del_pt_a <- nthrow(pt_a) - 1strow(pt_a) grouped id, n = 1,2,...n df$perc_pt_a <- nthrow(del_pt_a) / 1strow(pt_a) grouped id, n = 1,2,...n 

here desired output

+-----+------+---------+-----------+ | id  | pt_a | del_pt_a | perc_pt_a| +-----+------+---------+-----------+ | 101 |   50 | 0       | 0         | | 101 |  100 | 50      | 1.0       | | 101 |  150 | 100     | 2.0       | | 102 |   20 | 0       | 0         | | 102 |   30 | 10      | 0.5       | | 102 |   40 | 20      | 1.0       | | 103 |   60 | 0       | 0         | | 103 |   80 | 20      | 0.3       | | 103 |   90 | 30      | 0.5       | +-----+------+---------+-----------+ 

i desired result in ms excel want learn in r make work efficient. came across packages dplyr, plyr, data.table etc couldn't solve using those. 1 please me figure out how work around this.

here's data.table way:

library(data.table) setdt(df)[,`:=`(     del       = pt_a - pt_a[1],     perc      = pt_a/pt_a[1]-1 ),by=id] 

which gives

    id pt_a del      perc 1: 101   50   0 0.0000000 2: 101  100  50 1.0000000 3: 101  150 100 2.0000000 4: 102   20   0 0.0000000 5: 102   30  10 0.5000000 6: 102   40  20 1.0000000 7: 103   60   0 0.0000000 8: 103   80  20 0.3333333 9: 103   90  30 0.5000000 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -