Say I have a dataframe, df, such that:
set.seed(123)
df1 <- 0
df1$visit <- rep(c("scr", "1mo", "3mo", "6mo0", "12mo", "2yr"), 2)
df1 <- as.data.frame(df1)
df1$id <- rep(c("101","102"), each = 6)
df1 <- df1[ ,-c(1)]
df1$x <- sample(0:30, 12, replace = T)
df1$want = c(0, -16, -12, -17, -28, -21, 0, 4, -7, -13, 2, -4)
What I would like to do: Subtract every row (X values) after the screening row (can be negative) to create a change variable from screening visit ONLY. So it's essentially looping through the set to calculate the change from screening visit, then repeats that for each ID/set of visits (this dummy set has essentially two ID's).
I've tried: looking on here for similar answers, and closest I could get to was using mutate() from dplyr. All answers I found either tell me how to subtract lagging or leading rows or mutate when certain conditions match.
I could do this in excel maybe but I will reuse this frequently in future analyses.
edit: added variable that would be exactly the right values.
This will work, we just need to group by id
, then take advantage of the first()
function to take the difference versus the first value of x for each group.
library(tidyverse)
df1 %>% group_by(id) %>% mutate(new = x - first(x))
# A tibble: 12 x 5
# Groups: id [2]
visit id x want new
<fct> <chr> <int> <dbl> <int>
1 scr 101 30 0 0
2 1mo 101 14 -16 -16
3 3mo 101 18 -12 -12
4 6mo0 101 13 -17 -17
5 12mo 101 2 -28 -28
6 2yr 101 9 -21 -21
7 scr 102 17 0 0
8 1mo 102 21 4 4
9 3mo 102 10 -7 -7
10 6mo0 102 4 -13 -13
11 12mo 102 19 2 2
12 2yr 102 13 -4 -4
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.