-
Notifications
You must be signed in to change notification settings - Fork 0
/
surrogate pairs.Rmd
73 lines (55 loc) · 3.13 KB
/
surrogate pairs.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
title: "Surrogate pairs"
author: "Céline Carlsen"
date: "15/11/2017"
output: html_document
---
```{r}
setwd("/Users/celinecarlsen/Cognitive Science/5. semester/TA Methods III/Assingments/A4_coordination/A4/downsampled")
prepro_filelist = list.files(pattern = "Study1") # load the preprocessed files
num=str_extract_all(prepro_filelist, "\\d+") #extract all numbers in the file name (i.e Study?_G?_T?)
df= as.data.frame(t(as.data.frame(num))) #create a transposed (i.e. "tipped") dataframe of these number
rownames(df)<-NULL #Remove rownames.
pairs=as.numeric(unique(df$V2)) #create pairs, i.e. unique group numbers
SurrogateList <- expand.grid(a = pairs, b = pairs) # expand.grid combines all possible combinations of a given vector
SurrogateList = subset(SurrogateList, a != b) # Ensure that pair 1 (i.e group1) doesn't get paired with it self.
# Per each condition
for (co in c("Synchronous","TurnTaking","SelfPaced")){ #loop through a list of condintions
for (i in 1:nrow(SurrogateList)){ #and loop through all the pairings of the surrogate list
#start recreate filenames from the working directory based on the matching from the surrogatelist
f1=paste0('Study1_G',SurrogateList$a[i]) #Insert group numeber from Surrogatelist
f2=paste0('Study1_G',SurrogateList$b[i])
file1=prepro_filelist[grepl(f1,prepro_filelist) & grepl(co,prepro_filelist)] #insert conditions as well.
file2=prepro_filelist[grepl(f2,prepro_filelist) & grepl(co,prepro_filelist)]
d1=read.csv(file1) #Load the recreated filenames
d2=read.csv(file2)
#Ensure both dataframes have the same length
if (nrow(d1)>nrow(d2)){
d1=d1[1:nrow(d2),]
} else {
d2=d2[1:nrow(d1),]
}
#creation of surrogate files. (Riccardo's idea of having fun and completely overdoing it ;-) )
#per each 2 pairs there are 4 possible surrogate pairs:
#pair1person1 w pair2person2;
d3=data.frame(Resp1=d1$Resp1,Resp2=d2$Resp2,HR1=d1$HR1,HR2=d2$HR2,Resp1S=d1$Resp1S,Resp2S=d2$Resp2S,HR1S=d1$HR1S,HR2S=d2$HR2S)
#pair1person2 w pair2person2;
d4=data.frame(Resp1=d2$Resp1,Resp2=d1$Resp2,HR1=d2$HR1,HR2=d1$HR2,Resp1S=d2$Resp1S,Resp2S=d1$Resp2S,HR1S=d2$HR1S,HR2S=d1$HR2S)
#pair1person1 w pair2person1;
d5=data.frame(Resp1=d1$Resp1,Resp2=d2$Resp1,HR1=d1$HR1,HR2=d2$HR1,Resp1S=d1$Resp1S,Resp2S=d2$Resp1S,HR1S=d1$HR1S,HR2S=d2$HR1S)
#pair1person2 w pair2person2
d6=data.frame(Resp1=d2$Resp2,Resp2=d1$Resp2,HR1=d2$HR2,HR2=d1$HR2,Resp1S=d2$Resp2S,Resp2S=d1$Resp2S,HR1S=d2$HR2S,HR2S=d1$HR2S)
#Make filenames that tells which pairs have been mixed
file3=paste0("Study1_SurrogateGroup",SurrogateList$a[i],"&",SurrogateList$b[i],"a_",co,".csv")
file4=paste0("Study1_SurrogateGroup",SurrogateList$a[i],"&",SurrogateList$b[i],"b_",co,".csv")
file5=paste0("Study1_SurrogateGroup",SurrogateList$a[i],"&",SurrogateList$b[i],"c_",co,".csv")
file6=paste0("Study1_SurrogateGroup",SurrogateList$a[i],"&",SurrogateList$b[i],"d_",co,".csv")
#write csv files
write_csv(d3,file.path("surrogate",file3))
write_csv(d4,file.path("surrogate",file4))
write_csv(d5,file.path("surrogate",file5))
write_csv(d6,file.path("surrogate",file6))
}
}
##NOW DO THE SAME FOR STUDY 2##
```