R building a subset based on value in previous row -
i have problem figuering out: suppose how data looks like:
num condition y 1 1 2 2 3 3 4 b 4 5 b 5 6 b 6 7 c 7 8 c 8 9 c 9 10 b 10 11 b 11 12 b 12
i want make calculation (e.g., mean) on b, depending on whether value in row before b, in example or c? help!!! angelika
is want?
# in order separate between different runs of condition 'b', # length , value of runs of equal values of 'condition' rl <- rle(x = df$condition) df$run <- rep(x = seq_len(length(rl$lengths)), times = rl$lengths) # calculate sum of y, on data grouped condition , run, , condition 'b' aggregate(y ~ condition + run, data = df, subset = condition == "b", sum)
Comments
Post a Comment