How to do this calculation in R? -
i have dataset looks this:
groups <- c(1:20) values1 <- c(1,3,2,4,2,5,1,6,2,7,3,5,2,6,3,5,1,5,3,4) values2 <- c(3,2,4,1,5,2,4,1,3,2,6,1,4,2,5,3,7,1,4,2) sample.data <- data.frame(groups,values1,values2) head(sample.data) groups values1 values2 1 1 1 3 2 2 3 2 3 3 2 4 4 4 4 1 5 5 2 5 6 6 5 2
description: dataframe has 20 sets of values (values1 , values2) of 20 different groups. numbers follow these patterns: (1) numbers alternatively highs , lows (2) if number in values1
column higher 2 adjacent numbers, corresponding number in values2
column lower 2 adjacent numbers (as can seen in sample)
what want calculate these numbers called "delta value" (dv) - calculated differences between high-value number , mean of 2 low-value numbers adjacent it. example, first 3-number set of values1
column (1,3,2), first dv 3-((2+1)/2)=1.5; take last number of set first number of next set, same calculation, second dv 4-((2+2)/2)=2; , on. record of these dv values values1
column in new column called dv1
. same thing values2
column , record dv values in new dv2
column. note in values2
column, first number higher second one, ignore , start calculation next 3-number set (2,4,1) , repeat calculation above.
so how execute calculation in r?
thank in advance!
p/s: sorry, long question think it's got required information calculation.
because want use dv
values plotting, might easiest twice, once each column
library(dplyr) sample.data1 <- sample.data %>% mutate(alt = row_number()%%2, mean_adj_1 = (lag(values1) + lead(values1))/2, dv1 = (values1 - mean_adj_1) * (1-alt)) %>% filter(alt == 0 & !is.na(dv1)) %>% select(-c(alt:mean_adj_1)) sample.data.2 <- sample.data %>% mutate(alt = row_number()%%2, mean_adj_2 = (lag(values2) + lead(values2))/2, dv2 = (values2 - mean_adj_2) * alt) %>% filter(alt == 1 & !is.na(dv2)) %>% select(-c(alt:mean_adj_2))
explanation: done in fewer steps way can see what's going on. mean_adj
columns mean of row before , after, , alt
column lets put zeroes in rows don't want dv
calculated for. the dv
column value - mean_adj
.
result:
> sample.data.1 groups values1 values2 dv1 1 2 3 2 1.5 2 4 4 1 2.0 3 6 5 2 3.5 4 8 6 1 4.5 5 10 7 2 4.5 6 12 5 1 2.5 7 14 6 2 3.5 8 16 5 3 3.0 9 18 5 1 3.0 > sample.data.2 groups values1 values2 dv2 1 3 2 4 2.5 2 5 2 5 3.5 3 7 1 4 2.5 4 9 2 3 1.5 5 11 3 6 4.5 6 13 2 4 2.5 7 15 3 5 2.5 8 17 1 7 5.0 9 19 3 4 2.5
Comments
Post a Comment