简体   繁体   中英

R: finding absolute difference with dplyr and group_by

I have the following example.

I want to create a new column with the absolute difference in AGE compared to each Treat==1 in the same PairID.

Desired output should be as shown below.

I have tried using dplyr with:

Data complete:

Treat <- c(1,0,0,1,0,0,1,0)
PairID <- c(1,1,1,2,2,2,3,3)
Age <- c(30,60,31,20,20,40,50,52)

D <- data.frame(Treat,PairID,Age)
D 

D %>%
  group_by(PairID) %>%
  abs(Age - Age[Treat == 1]) 

期望的 AGE 绝对差异输出

in Base-R:

D$absD <- unlist(lapply(split(D,D$PairID), function(x) abs(x$Age - x$Age[x$Treat==1])))

> D
  Treat PairID Age absD
1     1      1  30    0
2     0      1  60   30
3     0      1  31    1
4     1      2  20    0
5     0      2  20    0
6     0      2  40   20
7     1      3  50    0
8     0      3  52    2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM