First, some test data:
set.seed(123)
df <- data.frame(ID = rep(1:3, each = 3),
w1_panas1 = runif(9),
w1_panas2 = runif(9),
w1_panas3 = runif(9),
w2_panas1 = runif(9),
w2_panas2 = runif(9),
w2_panas3 = runif(9),
w3_panas1 = runif(9),
w3_panas2 = runif(9),
w3_panas3 = runif(9))
df
# ID w1_panas1 w1_panas2 w1_panas3 w2_panas1 w2_panas2 w2_panas3 w3_panas1 w3_panas2 w3_panas3
#1 1 0.2875775 0.45661474 0.3279207 0.59414202 0.7584595 0.13880606 0.56094798 0.2743836 0.7101824014
#2 1 0.7883051 0.95683335 0.9545036 0.28915974 0.2164079 0.23303410 0.20653139 0.8146400 0.0006247733
#3 1 0.4089769 0.45333416 0.8895393 0.14711365 0.3181810 0.46596245 0.12753165 0.4485163 0.4753165741
#4 2 0.8830174 0.67757064 0.6928034 0.96302423 0.2316258 0.26597264 0.75330786 0.8100644 0.2201188852
#5 2 0.9404673 0.57263340 0.6405068 0.90229905 0.1428000 0.85782772 0.89504536 0.8123895 0.3798165377
#6 2 0.0455565 0.10292468 0.9942698 0.69070528 0.4145463 0.04583117 0.37446278 0.7943423 0.6127710033
#7 3 0.5281055 0.89982497 0.6557058 0.79546742 0.4137243 0.44220007 0.66511519 0.4398317 0.3517979092
#8 3 0.8924190 0.24608773 0.7085305 0.02461368 0.3688455 0.79892485 0.09484066 0.7544752 0.1111354243
#9 3 0.5514350 0.04205953 0.5440660 0.47779597 0.1524447 0.12189926 0.38396964 0.6292211 0.2436194727
Now the manipulations, using dplyr and tidyr:
library(dplyr) # load the
library(tidyr) # required packages
df <- df %>%
group_by(ID) %>% # group the data by ID
mutate(n = row_number()) %>% # for each ID, create and index n
ungroup() # ungroup the data
df %>%
gather(panas, value, -c(ID, n)) %>% # reshape the data to long format
separate(panas, into = c("week", "number"), sep = "_") %>% # split the column "panas" into two columns based on the "_"
group_by(ID, week, n) %>% # group the data
summarise(mean = mean(value)) %>% # calculate mean values for each group
ungroup() %>% # ungroup..
spread(week, mean) %>% # reshape from long to wide format
left_join(df, ., by = c("ID", "n")) %>% # perform a join with the original data by ID and n so that all data is in one table
select(-n) # drop column "n"
And the result is (notice the last 3 columns w1, w2, w3 which show the averages that you wanted):
#Source: local data frame [9 x 13]
#
# ID w1_panas1 w1_panas2 w1_panas3 w2_panas1 w2_panas2 w2_panas3 w3_panas1 w3_panas2 w3_panas3 w1 w2 w3
#1 1 0.2875775 0.45661474 0.3279207 0.59414202 0.7584595 0.13880606 0.56094798 0.2743836 0.7101824014 0.3573710 0.4971359 0.5151713
#2 1 0.7883051 0.95683335 0.9545036 0.28915974 0.2164079 0.23303410 0.20653139 0.8146400 0.0006247733 0.8998807 0.2462006 0.3405987
#3 1 0.4089769 0.45333416 0.8895393 0.14711365 0.3181810 0.46596245 0.12753165 0.4485163 0.4753165741 0.5839501 0.3104190 0.3504549
#4 2 0.8830174 0.67757064 0.6928034 0.96302423 0.2316258 0.26597264 0.75330786 0.8100644 0.2201188852 0.7511305 0.4868742 0.5944970
#5 2 0.9404673 0.57263340 0.6405068 0.90229905 0.1428000 0.85782772 0.89504536 0.8123895 0.3798165377 0.7178692 0.6343089 0.6957505
#6 2 0.0455565 0.10292468 0.9942698 0.69070528 0.4145463 0.04583117 0.37446278 0.7943423 0.6127710033 0.3809170 0.3836943 0.5938587
#7 3 0.5281055 0.89982497 0.6557058 0.79546742 0.4137243 0.44220007 0.66511519 0.4398317 0.3517979092 0.6945454 0.5504639 0.4855816
#8 3 0.8924190 0.24608773 0.7085305 0.02461368 0.3688455 0.79892485 0.09484066 0.7544752 0.1111354243 0.6156791 0.3974613 0.3201504
#9 3 0.5514350 0.04205953 0.5440660 0.47779597 0.1524447 0.12189926 0.38396964 0.6292211 0.2436194727 0.3791869 0.2507133 0.4189367