I am trying to create a 3-column dataframe df to then generate a stacked bar chart. The problem is that in Survey_data$x x is returned with " before and " after the name of the column, and so does not work. I want to get rid of " " characters. This is my code:
install.packages("readxl")
library(readxl)
install.packages("dplyr")
library(dplyr)
install.packages("ggplot2")
library(ggplot2)
Survey_data <- read_excel("Patient_Safety_Culture_Survey_ready.xlsx")
Survey_data <- as.data.frame(Survey_data)
responses <- c("Agree/Strongly agree", "Neither agree nor disagree", "Disagree/Strongly disagree", "NA")
questions <- names(Survey_data)
questions <- questions[8:nrow(Survey_data)]
percentages <- 0
new_df <- data.frame(
expand.grid(questions = questions, responses = responses, percentages = percentages))
df <- new_df[order(new_df$questions),]
for (i in 1:length(df$percentages)) {
for (x in df$questions[i]) {
print(i)
print(x)
if (df$responses[i] == "Agree/Strongly agree") {
df$percentages[i] <- round((sum(Survey_data$`x` =="Agree" | Survey_data$`x`=="Strongly agree"))/(nrow(Survey_data)-1)*100)
print(df$percentages[i])
}
else if (df$responses[i] == "Neither agree nor disagree") {
df$percentages[i] <- round((sum(Survey_data$`x`=="Neither agree nor disagree"))/(nrow(Survey_data)-1)*100)
print(df$percentages[i])
}
else if (df$responses[i] == "Disagree/Strongly disagree") {
df$percentages[i] <- round((sum(Survey_data$`x`=="Disagree" | Survey_data$`x`=="Strongly disagree"))/(nrow(Survey_data)-1)*100)
print(df$percentages[i])
}
else if (df$responses[i] == "Not Applicable") {
df$percentages[i] <- round((sum(Survey_data$`x`=="DisagreeNot Applicable"))/(nrow(Survey_data)-1)*100)
print(df$percentages[i])
}
}
}
ggplot(data = df, aes(x = questions, y = percentages, fill = responses)) +
geom_bar() + coord_flip()
My dataset:
> dput(Survey_data)
structure(list(`Respondent ID` = c(10696573577, 10694412447,
10694408084, 10694401323, 10694398463, 10694395782, 10694392897,
10694390734, 10694387234, 10694382621, 10694379903, 10694375419,
10694372588, 10694368857, 10694365257, 10694359929, 10694324890,
10694319587, 10694277495), `Collector ID` = c(226710864, 226710864,
226710864, 226710864, 226710864, 226710864, 226710864, 226710864,
226710864, 226710864, 226710864, 226710864, 226710864, 226710864,
226710864, 226710864, 226710864, 226710864, 226710864), `Start Date` = c("04/30/2019 12:19:12 PM",
"04/29/2019 04:59:34 PM", "04/29/2019 04:48:58 PM", "04/29/2019 04:48:56 PM",
"04/29/2019 04:48:54 PM", "04/29/2019 04:48:51 PM", "04/29/2019 04:48:47 PM",
"04/29/2019 04:48:44 PM", "04/29/2019 04:48:42 PM", "04/29/2019 04:48:40 PM",
"04/29/2019 04:48:37 PM", "04/29/2019 04:32:36 PM", "04/29/2019 04:32:34 PM",
"04/29/2019 04:32:31 PM", "04/29/2019 04:32:28 PM", "04/29/2019 04:32:25 PM",
"04/29/2019 04:31:08 PM", "04/29/2019 04:17:52 PM", "04/29/2019 03:54:50 PM"
), `End Date` = c("04/30/2019 12:20:26 PM", "04/29/2019 05:00:33 PM",
"04/29/2019 04:59:07 PM", "04/29/2019 04:56:57 PM", "04/29/2019 04:56:01 PM",
"04/29/2019 04:55:10 PM", "04/29/2019 04:54:12 PM", "04/29/2019 04:53:28 PM",
"04/29/2019 04:52:19 PM", "04/29/2019 04:50:48 PM", "04/29/2019 04:49:53 PM",
"04/29/2019 04:48:25 PM", "04/29/2019 04:47:29 PM", "04/29/2019 04:46:17 PM",
"04/29/2019 04:45:06 PM", "04/29/2019 04:43:21 PM", "04/29/2019 04:32:16 PM",
"04/29/2019 04:30:56 PM", "04/29/2019 04:17:41 PM"), `IP Address` = c("92.122.153.103",
"193.60.246.70", "193.60.246.70", "193.60.246.70", "193.60.246.70",
"193.60.246.70", "193.60.246.70", "193.60.246.70", "193.60.246.70",
"193.60.246.70", "193.60.246.70", "193.60.246.70", "193.60.246.70",
"193.60.246.70", "193.60.246.70", "193.60.246.70", "193.60.246.70",
"193.60.246.70", "193.60.246.70"), `My clinical area is:` = c("Outpatient clinic",
"Operative theatre", "Inpatient ward", "Discharge lounge", "Operative theatre",
"Outpatient clinic", "Medical department", "Medical department",
"Operative theatre", "Outpatient clinic", "Operative theatre",
"Medical department", "cviuhwe alck,#pm", "Operative theatre",
"Inpatient ward", "Operative theatre", "Outpatient clinic", "Medical department",
"Discharge lounge"), `My role is:` = c("Admin and clerical",
"Nurse", "Nurse", "Midwife", "Consultant", "Admin and clerical",
"AHP", "Junior Doctor", "Midwife", "Midwife", "Consultant", "Nurse",
"AHP", "Midwife", "Clinical manager", "Admin and clerical", "Nurse",
"Junior Doctor", "AHP"), `My input is well received in this clinical area.` = c("Strongly agree",
"Strongly disagree", "Strongly agree", "Strongly agree", "Disagree",
"Strongly agree", "Strongly agree", "Agree", "Disagree", "Strongly agree",
"Strongly agree", "Strongly agree", "Neither agree nor disagree",
"Strongly disagree", "Agree", "Strongly disagree", "Disagree",
"Neither agree nor disagree", "Agree"), `In this clinical area, it is difficult to speak up when I perceive a problem with patient care.` = c("Agree",
"Disagree", "Agree", "Strongly disagree", "Strongly disagree",
"Agree", "Agree", "Strongly disagree", "Strongly agree", "Strongly agree",
"Disagree", "Disagree", "Disagree", "Strongly disagree", "Strongly disagree",
"Strongly agree", "Neither agree nor disagree", "Neither agree nor disagree",
"Disagree"), `Disagreements in this clinical area are resolved appropriately (i.e., not who is right but what is best for the patient).` = c("Agree",
"Agree", "Disagree", "Neither agree nor disagree", "Strongly disagree",
"Strongly agree", "Agree", "Agree", "Strongly agree", "Strongly disagree",
"Agree", "Agree", "Strongly agree", "Strongly agree", "Strongly agree",
"Agree", "Strongly disagree", "Disagree", "Agree"), `I have the support I need from my colleagues and other staff groups to care for patients.` = c("Strongly agree",
"Neither agree nor disagree", "Agree", "Disagree", "Disagree",
"Agree", "Neither agree nor disagree", "Disagree", "Strongly disagree",
"Neither agree nor disagree", "Neither agree nor disagree", "Agree",
"Strongly disagree", "Not Applicable", "Neither agree nor disagree",
"Disagree", "Strongly agree", "Agree", "Neither agree nor disagree"
), `It is easy for staff here to ask questions when there is something that they do not understand.` = c("Agree",
"Disagree", "Strongly agree", "Agree", "Disagree", "Strongly agree",
"Agree", "Strongly agree", "Strongly agree", "Disagree", "Agree",
"Agree", "Agree", "Disagree", "Disagree", "Neither agree nor disagree",
"Agree", "Strongly disagree", "Agree"), `The multidisciplinary team here works together as a well-coordinated team.` = c("Agree",
"Strongly agree", "Disagree", "Strongly disagree", "Strongly disagree",
"Agree", "Agree", "Strongly agree", "Agree", "Agree", "Strongly agree",
"Disagree", "Disagree", "Neither agree nor disagree", "Neither agree nor disagree",
"Disagree", "Agree", "Agree", "Disagree"), `I would feel safe to be treated here as a patient.` = c("Agree",
"Agree", "Strongly agree", "Neither agree nor disagree", "Disagree",
"Strongly agree", "Agree", "Strongly disagree", "Disagree", "Agree",
"Disagree", "Disagree", "Neither agree nor disagree", "Strongly agree",
"Neither agree nor disagree", "Agree", "Strongly disagree", "Agree",
"Agree"), `Clinical errors are handled appropriately in this clinical area.` = c("Agree",
"Disagree", "Agree", "Disagree", "Strongly disagree", "Agree",
"Agree", "Strongly disagree", "Agree", "Strongly agree", "Agree",
"Strongly agree", "Disagree", "Strongly disagree", "Strongly disagree",
"Strongly disagree", "Disagree", "Strongly disagree", "Agree"
), `I know the proper channels to direct questions regarding patient safety in this clinical area.` = c("Agree",
"Neither agree nor disagree", "Disagree", "Strongly agree", "Disagree",
"Strongly agree", "Agree", "Agree", "Strongly disagree", "Disagree",
"Strongly agree", "Agree", "Disagree", "Neither agree nor disagree",
"Agree", "Strongly agree", "Neither agree nor disagree", "Strongly agree",
"Strongly agree"), `I receive appropriate feedback about my performance.` = c("Strongly agree",
"Strongly disagree", "Agree", "Strongly disagree", "Strongly disagree",
"Agree", "Agree", "Disagree", "Agree", "Agree", "Strongly agree",
"Strongly agree", "Strongly agree", "Disagree", "Disagree", "Neither agree nor disagree",
"Disagree", "Disagree", "Agree"), `In this clinical area, it is difficult to discuss errors.` = c("Neither agree nor disagree",
"Strongly agree", "Disagree", "Neither agree nor disagree", "Disagree",
"Strongly agree", "Agree", "Agree", "Agree", "Strongly agree",
"Agree", "Agree", "Strongly disagree", "Agree", "Strongly agree",
"Disagree", "Strongly disagree", "Neither agree nor disagree",
"Disagree"), `I am encouraged by my colleagues to report any patient safety concerns I may have.` = c("Neither agree nor disagree",
"Strongly disagree", "Strongly agree", "Disagree", "Strongly disagree",
"Agree", "Agree", "Strongly agree", "Disagree", "Strongly agree",
"Disagree", "Agree", "Strongly disagree", "Strongly agree", "Strongly disagree",
"Strongly disagree", "Agree", "Neither agree nor disagree", "Strongly agree"
), `The culture in this clinical area makes it easy to learn from the errors of others.` = c("Agree",
"Disagree", "Agree", "Agree", "Disagree", "Strongly agree", "Agree",
"Strongly agree", "Agree", "Agree", "Agree", "Strongly agree",
"Neither agree nor disagree", "Strongly disagree", "Agree", "Strongly agree",
"Strongly agree", "Disagree", "Agree"), `My suggestions about safety would be acted upon if I expressed them to management.` = c("Strongly agree",
"Agree", "Disagree", "Strongly disagree", "Disagree", "Agree",
"Agree", "Strongly agree", "Strongly agree", "Agree", "Strongly agree",
"Strongly agree", "Agree", "Agree", "Neither agree nor disagree",
"Strongly disagree", "Strongly disagree", "Disagree", "Disagree"
), `I like my job.` = c("Strongly agree", "Strongly agree", "Strongly disagree",
"Strongly agree", "Strongly disagree", "Strongly agree", "Agree",
"Disagree", "Disagree", "Disagree", "Disagree", "Agree", "Strongly disagree",
"Agree", "Disagree", "Strongly agree", "Agree", "Strongly disagree",
"Strongly agree"), `Working here is like being part of a large family.` = c("Strongly agree",
"Disagree", "Strongly agree", "Disagree", "Strongly disagree",
"Agree", "Agree", "Agree", "Agree", "Agree", "Strongly agree",
"Strongly agree", "Disagree", "Disagree", "Disagree", "Agree",
"Neither agree nor disagree", "Strongly agree", "Disagree"),
`This is a good place to work.` = c("Strongly agree", "Agree",
"Neither agree nor disagree", "Neither agree nor disagree",
"Disagree", "Strongly agree", "Agree", "Agree", "Strongly agree",
"Strongly agree", "Agree", "Strongly agree", "Strongly agree",
"Strongly agree", "Strongly agree", "Disagree", "Disagree",
"Strongly agree", "Agree"), `I am proud to work in this clinical area.` = c("Strongly agree",
"Strongly disagree", "Neither agree nor disagree", "Strongly disagree",
"Disagree", "Agree", "Agree", "Disagree", "Disagree", "Disagree",
"Strongly agree", "Agree", "Neither agree nor disagree",
"Strongly agree", "Strongly agree", "Neither agree nor disagree",
"Neither agree nor disagree", "Agree", "Neither agree nor disagree"
), `Morale in this clinical area is high.` = c("Strongly agree",
"Agree", "Disagree", "Strongly agree", "Strongly disagree",
"Strongly agree", "Agree", "Strongly agree", "Agree", "Agree",
"Disagree", "Agree", "Strongly disagree", "Disagree", "Strongly disagree",
"Strongly disagree", "Strongly agree", "Disagree", "Agree"
), `When my workload becomes excessive, my performance is impaired.` = c("Strongly agree",
"Strongly agree", "Agree", "Disagree", "Disagree", "Agree",
"Agree", "Strongly disagree", "Agree", "Agree", "Agree",
"Disagree", "Agree", "Strongly agree", "Agree", "Agree",
"Strongly disagree", "Agree", "Agree"), `I am less effective at work when fatigued.` = c("Strongly agree",
"Disagree", "Strongly disagree", "Agree", "Strongly disagree",
"Strongly agree", "Agree", "Agree", "Disagree", "Agree",
"Neither agree nor disagree", "Neither agree nor disagree",
"Disagree", "Agree", "Disagree", "Disagree", "Neither agree nor disagree",
"Strongly disagree", "Neither agree nor disagree"), `I am more likely to make errors in tense or hostile situations.` = c("Strongly agree",
"Agree", "Strongly agree", "Agree", "Disagree", "Agree",
"Agree", "Disagree", "Neither agree nor disagree", "Disagree",
"Disagree", "Agree", "Strongly agree", "Strongly disagree",
"Agree", "Neither agree nor disagree", "Strongly agree",
"Strongly agree", "Agree"), `Fatigue impairs my performance during emergency situations.` = c("Agree",
"Strongly disagree", "Agree", "Disagree", "Strongly disagree",
"Strongly agree", "Neither agree nor disagree", "Neither agree nor disagree",
"Strongly agree", "Strongly agree", "Agree", "Strongly agree",
"Strongly disagree", "Strongly agree", "Neither agree nor disagree",
"Neither agree nor disagree", "Strongly disagree", "Not Applicable",
"Neither agree nor disagree"), `Unit management supports my daily efforts.` = c("Strongly agree",
"Strongly agree", "Disagree", "Strongly agree", "Strongly disagree",
"Agree", "Agree", "Agree", "Agree", "Strongly agree", "Strongly agree",
"Disagree", "Strongly agree", "Strongly agree", "Agree",
"Strongly agree", "Neither agree nor disagree", "Disagree",
"Agree"), `Hospital management supports my daily efforts.` = c("Strongly agree",
"Agree", "Agree", "Strongly disagree", "Disagree", "Strongly agree",
"Agree", "Disagree", "Disagree", "Agree", "Agree", "Agree",
"Agree", "Agree", "Disagree", "Strongly disagree", "Agree",
"Strongly agree", "Agree"), `Unit management does not knowingly compromise patient safety.` = c("Strongly agree",
"Disagree", "Agree", "Strongly agree", "Disagree", "Strongly agree",
"Agree", "Strongly agree", "Strongly agree", "Strongly agree",
"Agree", "Disagree", "Disagree", "Disagree", "Neither agree nor disagree",
"Neither agree nor disagree", "Disagree", "Disagree", "Agree"
), `Hospital management does not knowingly compromise patient safety.` = c("Strongly agree",
"Agree", "Strongly agree", "Neither agree nor disagree",
"Disagree", "Agree", "Agree", "Strongly agree", "Disagree",
"Disagree", "Disagree", "Strongly agree", "Strongly agree",
"Agree", "Strongly disagree", "Disagree", "Strongly agree",
"Neither agree nor disagree", "Agree"), `Unit management is doing a good job.` = c("Strongly agree",
"Strongly disagree", "Strongly agree", "Agree", "Strongly disagree",
"Strongly agree", "Agree", "Strongly disagree", "Agree",
"Agree", "Strongly agree", "Strongly agree", "Disagree",
"Strongly agree", "Strongly agree", "Neither agree nor disagree",
"Strongly disagree", "Neither agree nor disagree", "Agree"
), `Hospital management is doing a good job.` = c("Strongly agree",
"Strongly agree", "Strongly disagree", "Disagree", "Strongly disagree",
"Agree", "Agree", "Strongly agree", "Disagree", "Agree",
"Neither agree nor disagree", "Agree", "Strongly agree",
"Disagree", "Strongly disagree", "Agree", "Agree", "Strongly disagree",
"Agree"), `Difficult colleagues are dealt with constructively by our Unit management.` = c("Agree",
"Neither agree nor disagree", "Neither agree nor disagree",
"Strongly agree", "Strongly disagree", "Strongly agree",
"Neither agree nor disagree", "Strongly disagree", "Strongly agree",
"Neither agree nor disagree", "Disagree", "Disagree", "Disagree",
"Agree", "Neither agree nor disagree", "Disagree", "Neither agree nor disagree",
"Agree", "Agree"), `Difficult colleagues are dealt with constructively by our Hospital management.` = c("Agree",
"Disagree", "Agree", "Strongly disagree", "Disagree", "Agree",
"Neither agree nor disagree", "Agree", "Strongly agree",
"Strongly agree", "Strongly agree", "Strongly agree", "Agree",
"Strongly agree", "Agree", "Neither agree nor disagree",
"Strongly agree", "Strongly disagree", "Agree"), `I get adequate, timely information about developments that might affect my work, from Unit management.` = c("Strongly agree",
"Agree", "Disagree", "Neither agree nor disagree", "Disagree",
"Strongly agree", "Agree", "Disagree", "Disagree", "Disagree",
"Agree", "Strongly agree", "Disagree", "Disagree", "Disagree",
"Strongly disagree", "Strongly disagree", "Strongly agree",
"Agree"), `I get adequate, timely information about developments that might affect my work, from Hospital management.` = c("Strongly agree",
"Strongly agree", "Strongly agree", "Disagree", "Strongly disagree",
"Agree", "Agree", "Neither agree nor disagree", "Agree",
"Agree", "Disagree", "Strongly disagree", "Strongly agree",
"Strongly agree", "Strongly disagree", "Strongly agree",
"Agree", "Strongly disagree", "Agree"), `The staffing levels in this clinical area are sufficient to handle the numbers of patients.` = c("Strongly agree",
"Strongly disagree", "Agree", "Agree", "Disagree", "Strongly agree",
"Agree", "Disagree", "Disagree", "Agree", "Strongly agree",
"Agree", "Disagree", "Agree", "Strongly agree", "Agree",
"Agree", "Agree", "Agree"), `This hospital does a good job of training new staff.` = c("Strongly agree",
"Agree", "Strongly agree", "Strongly agree", "Disagree",
"Agree", "Agree", "Strongly agree", "Agree", "Strongly disagree",
"Agree", "Strongly agree", "Agree", "Disagree", "Agree",
"Strongly disagree", "Neither agree nor disagree", "Neither agree nor disagree",
"Agree"), `All the necessary information for diagnostic and therapeutic decisions is routinely available to me.` = c("Agree",
"Disagree", "Agree", "Strongly disagree", "Strongly disagree",
"Strongly agree", "Neither agree nor disagree", "Disagree",
"Strongly agree", "Disagree", "Neither agree nor disagree",
"Disagree", "Disagree", "Agree", "Neither agree nor disagree",
"Neither agree nor disagree", "Disagree", "Disagree", "Agree"
), `Trainees in my discipline are adequately supervised.` = c("Agree",
"Strongly agree", "Agree", "Agree", "Disagree", "Agree",
"Neither agree nor disagree", "Strongly agree", "Strongly disagree",
"Agree", "Strongly agree", "Agree", "Agree", "Strongly agree",
"Strongly disagree", "Agree", "Strongly agree", "Agree",
"Agree"), `Communication breakdowns affecting the delivery of care are common.` = c("Strongly agree",
"Disagree", "Disagree", "Disagree", "Disagree", "Strongly agree",
"Agree", "Strongly agree", "Disagree", "Strongly agree",
"Agree", "Disagree", "Disagree", "Disagree", "Agree", "Strongly agree",
"Strongly disagree", "Strongly agree", "Agree"), `Please add any further comments below.` = c("cool",
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA)), row.names = c(NA, -19L), class = "data.frame")
Can someone please help?
Aucun commentaire:
Enregistrer un commentaire