Решение на основе gather
и spread
для расчета разницы, а затем left_join
к исходному фрейму данных.
library(dplyr)
library(tidyr)
dat2 <- dat %>%
gather(Column, Value, -group, -time) %>%
separate(Column, into = c("Letter", "Number"), sep = "_") %>%
spread(Number, Value) %>%
mutate(Diff = `1` - `3`) %>%
mutate(Letter = paste0(Letter, "_diff")) %>%
select(-`1`, -`3`) %>%
spread(Letter, Diff) %>%
left_join(dat, ., by = c("group", "time"))
dat2
# group time A_1 A_3 B_1 B_3 C_1 C_3 A_diff B_diff C_diff
# 1 1 100 7 5 7 3 5 3 2 4 2
# 2 1 200 8 4 5 6 1 2 4 -1 -1
# 3 1 300 5 6 8 9 2 1 -1 -1 1
# 4 1 400 3 5 7 8 2 1 -2 -1 1
# 5 2 100 3 5 7 6 3 2 -2 1 1
# 6 2 200 4 5 6 0 1 4 -1 6 -3
# 7 2 300 3 3 4 5 3 2 0 -1 1
# 8 2 400 6 5 3 1 3 7 1 2 -4
Или вы можете использовать следующий метод lapply
.
re <- lapply(c("A", "B", "C"), function(x){
dat[[paste0(x, "_1")]] - dat[[paste0(x, "_3")]]
})
names(re) <- paste0(c("A", "B", "C"), "_diff")
dat2 <- cbind(dat, as.data.frame(re))
dat2
# group time A_1 A_3 B_1 B_3 C_1 C_3 A_diff B_diff C_diff
# 1 1 100 7 5 7 3 5 3 2 4 2
# 2 1 200 8 4 5 6 1 2 4 -1 -1
# 3 1 300 5 6 8 9 2 1 -1 -1 1
# 4 1 400 3 5 7 8 2 1 -2 -1 1
# 5 2 100 3 5 7 6 3 2 -2 1 1
# 6 2 200 4 5 6 0 1 4 -1 6 -3
# 7 2 300 3 3 4 5 3 2 0 -1 1
# 8 2 400 6 5 3 1 3 7 1 2 -4
DATA
dat <- read.table(text = "group time A_1 A_3 B_1 B_3 C_1 C_3
1 100 7 5 7 3 5 3
1 200 8 4 5 6 1 2
1 300 5 6 8 9 2 1
1 400 3 5 7 8 2 1
2 100 3 5 7 6 3 2
2 200 4 5 6 0 1 4
2 300 3 3 4 5 3 2
2 400 6 5 3 1 3 7",
header = TRUE, stringsAsFactors = FALSE)