-
Notifications
You must be signed in to change notification settings - Fork 0
/
questions.R
154 lines (114 loc) · 4.47 KB
/
questions.R
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# This file is part of Empathy-viz.
# Copyright (C) 2024 Minet de Wied & SodaScience
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
library(reshape)
respons_items <- c("empathie","counter","distress","compassie","gedrag")
relationship_items <- c("vriend(in)","vreemde","vijand")
style_txt_itms <- function(txt){
strong(p(style="text-align: justify;",
txt,
style = "font-family: 'Tahoma'; font-si116pt; font-weight: 400;"
))
}
get_introduction <- function(intro_fp){
# Read the survey_introduction
df_intro <- read.csv(intro_fp)
return (
lapply(df_intro[,c('Paragraph')],style_txt_itms)
)
}
get_vignettes <- function(stu_fp){
# Read the list of vignettes
df_stu <- read.csv(stu_fp)
return (
df_stu
)
}
filter_vignettes <- function(df_vignettes){
vignette_numbers_all <- c(1, 4, 7, 10, 13, 16)
vignette_numbers_main <- 1:6
# Use logical indexing to filter rows
selected_rows <- df_vignettes[df_vignettes$Vnum %in% vignette_numbers_all, ]
selected_rows$Vnum <- vignette_numbers_main[match(selected_rows$Vnum, vignette_numbers_all)]
# View the selected rows
print(selected_rows)
return(
selected_rows
)
}
get_relationships <- function(sub_stu_fp){
# Read the list of vignettes
df_sub_stu <- read.csv(sub_stu_fp)
return (
df_sub_stu
)
}
get_radioMatrixFrame <- function(rmf_fp){
# Read the radioMatrix rows and columns
df_rmf <- read.csv(rmf_fp)
return(
df_rmf
)
}
get_disclaimer <- function(disclaimer_fp) {
config <- read.csv(disclaimer_fp)
as.list(setNames(config$value, config$key))
}
make.df.survey_result<- function(vignette.no,respons.no){
relationship.no <- length(relationship_items)
vignet <- rep(seq(1,vignette.no/relationship.no), each = relationship.no)
relatie <- rep(relationship_items,times=relationship.no)
df.survey.result <- data.frame(vignet,relatie)
em.names <- respons_items
emval <- rep("", nrow(df.survey.result))
em<-as.data.frame(matrix(emval, nrow=length(emval), ncol=respons.no))
colnames(em)<- em.names
df <- cbind(df.survey.result,em)
df$relatie <- factor(df$relatie, levels = c("vriend(in)","vreemde","vijand") )
df$vignet <- factor(df$vignet)
# add 'vign_cat', to show pain, sad and happiness
cond_pain <- (df$vignet =='1') | (df$vignet =='4')
df[cond_pain,"vign_cat"] <-"pijn"
cond_happy <- (df$vignet =='2') | (df$vignet =='5')
df[cond_happy,"vign_cat"] <-"blijdschap"
cond_sad <- (df$vignet =='3') | (df$vignet =='6')
df[cond_sad,"vign_cat"] <-"verdriet"
df$vign_cat <- factor(df$vign_cat, levels = c("blijdschap","verdriet","pijn") )
print(df)
return(df)
}
nullToNA <- function(x) {
x[sapply(x, is.null)] <- NA
return(x)
}
refactor_df <- function(df){
# wide.to.long
long <- melt(df, id=c("vignet","relatie","vign_cat"))
names(long) <- c("vignet","relatie","vign_cat","respons","value")
long$value<- factor(long$value, levels = c("helemaal niet","een beetje","redelijk","sterk","heel sterk") )
long$value.num <- as.numeric(long$value)
return(long)
}
to_numeric_df <- function(df){
df$empathie_<- as.integer(factor(df$empathie, levels = c("helemaal niet","een beetje","redelijk","sterk","heel sterk")))
df$compassie_<- as.integer(factor(df$compassie, levels = c("helemaal niet","een beetje","redelijk","sterk","heel sterk")))
df$distress_<- as.integer(factor(df$distress, levels = c("helemaal niet","een beetje","redelijk","sterk","heel sterk") ))
df$gedrag_<- as.integer(factor(df$gedrag, levels = c("helemaal niet","een beetje","redelijk","sterk","heel sterk") ))
df$counter_<- as.integer(factor(df$counter, levels = c("helemaal niet","een beetje","redelijk","sterk","heel sterk") ))
cols <- c('vignet','relatie','vign_cat','empathie_','counter_', 'distress_','compassie_', 'gedrag_' )
return(df[,cols])
}
get_ending <- function(end_fp){
# Read the survey_ending
df_ending <- read.csv(end_fp)
return (df_ending)
}