library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl)


Mydata <-read.csv2("C:/Users/DELL/Desktop/Work from old PC/reproductive.xlsx", sep="\t")

set.seed(123)

#Randomly sample 500 rows from the dataset
Mydata_sampled <- Mydata %>%
  sample_n(size = 500, replace = F)

# Assuming your dataset is named 'your_data' and the variable to change is 'your_variable'
Mydata_sampled$I.think.that.i.would.like.to.use.this.system.frequency <- tolower(Mydata_sampled$I.think.that.i.would.like.to.use.this.system.frequency)
# Assuming your dataset is named 'your_data' and the variable to change is 'your_variable'
Mydata_sampled$I.found.the.system.unnecessary.complex <- tolower(Mydata_sampled$I.found.the.system.unnecessary.complex)
# Assuming your dataset is named 'your_data' and the variable to change is 'your_variable'
Mydata_sampled$I.thought.the.system.was.easy.to.use <- tolower(Mydata_sampled$I.thought.the.system.was.easy.to.use)
Mydata_sampled$I.think.that.i.would.need.the.support.of.technical.person.to.be.able.to.use.this.system <- tolower(Mydata_sampled$I.think.that.i.would.need.the.support.of.technical.person.to.be.able.to.use.this.system)

# Assuming your dataset is named 'your_data' and the variable to change is 'your_variable'
Mydata_sampled$I.found.the.various.functions.in.this.system.were.well.integrated <- tolower(Mydata_sampled$I.found.the.various.functions.in.this.system.were.well.integrated)

# Assuming your dataset is named 'your_data' and the variable to change is 'your_variable'
Mydata_sampled$I.thought.there.was.too.much.inconsistency.in.this.system <- tolower(Mydata_sampled$I.thought.there.was.too.much.inconsistency.in.this.system )

# Assuming your dataset is named 'your_data' and the variable to change is 'your_variable'
Mydata_sampled$I.would.imagine.that.most.people.would.learn.to.use.this.system.very.quickly <- tolower(Mydata_sampled$I.would.imagine.that.most.people.would.learn.to.use.this.system.very.quickly)

# Assuming your dataset is named 'your_data' and the variable to change is 'your_variable'
Mydata_sampled$I.found.the.system.very.cumbersome.to.use <- tolower(Mydata_sampled$I.found.the.system.very.cumbersome.to.use)

# Assuming your dataset is named 'your_data' and the variable to change is 'your_variable'
Mydata_sampled$I.felt.very.confident.using.the.system <- tolower(Mydata_sampled$I.felt.very.confident.using.the.system)

# Assuming your dataset is named 'your_data' and the variable to change is 'your_variable'
Mydata_sampled$I.needed.to.learn.a.lot.of.things.before.i.could.get.going.with.the.system <- tolower(Mydata_sampled$I.needed.to.learn.a.lot.of.things.before.i.could.get.going.with.the.system)

Mydata_sampled <- Mydata_sampled %>% select(x1 = I.think.that.i.would.like.to.use.this.system.frequency,
                            x2 =I.found.the.system.unnecessary.complex ,
                            x3=I.thought.the.system.was.easy.to.use,
                            x4=I.think.that.i.would.need.the.support.of.technical.person.to.be.able.to.use.this.system,
                            x5=I.found.the.various.functions.in.this.system.were.well.integrated,
                            x6=I.thought.there.was.too.much.inconsistency.in.this.system,
                            x7=I.would.imagine.that.most.people.would.learn.to.use.this.system.very.quickly,
                            x8=I.found.the.system.very.cumbersome.to.use,
                            x9=I.felt.very.confident.using.the.system,
                            x10=I.needed.to.learn.a.lot.of.things.before.i.could.get.going.with.the.system)


a <-Mydata_sampled%>%group_by(x1)%>%summarise(n = n(), percent = (n()/500)*100)
b <-Mydata_sampled%>%group_by(x2)%>%summarise(n = n(),  percent = (n()/500)*100)
c <-Mydata_sampled%>%group_by(x3)%>%summarise(n = n(),percent = (n()/500)*100)
d <-Mydata_sampled%>%group_by(x4)%>%summarise(n = n(), percent = (n()/500)*100)
e <-Mydata_sampled%>%group_by(x5)%>%summarise(n = n(),percent = (n()/500)*100)
f <-Mydata_sampled%>%group_by(x6)%>%summarise(n = n(),percent = (n()/500)*100)
g <-Mydata_sampled%>%group_by(x7)%>%summarise(n = n(),percent = (n()/500)*100)
h <-Mydata_sampled%>%group_by(x8)%>%summarise(n = n(),percent = (n()/500)*100)
i <-Mydata_sampled%>%group_by(x9)%>%summarise(n = n(),percent = (n()/500)*100)
j <-Mydata_sampled%>%group_by(x10)%>%summarise(n = n(),percent = (n()/500)*100)




ggplot(a, aes(x = x1, y = percent))+geom_bar(stat ="identity",fill ="red") +geom_text(aes(label = paste0(percent, "%"), vjust = -0.4),size=7.5)+
  labs(y = " ", x = " ", title = "I think that i would like to use this system frequency ") + 
  theme(plot.title = element_text(size=18),axis.text.y = element_blank(), axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1)) 

ggplot(b, aes(x = x2, y = percent))+geom_bar(stat ="identity",fill ="red") +geom_text(aes(label = paste0(percent, "%"), vjust = -0.4),size=7.5)+
  labs(y = " ", x = " ", title = "I found the system unnecessary complex") + 
  theme(plot.title = element_text(size=18),axis.text.y = element_blank(), axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1)) 

ggplot(c, aes(x = x3, y = percent))+geom_bar(stat ="identity",fill ="red") +geom_text(aes(label = paste0(percent, "%"), vjust = -0.4),size=7.5)+
  labs(y = " ", x = " ", title = "I thought the system was easy to use") + 
  theme(plot.title = element_text(size=18),axis.text.y = element_blank(), axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1)) 

ggplot(d, aes(x = x4, y = percent))+geom_bar(stat ="identity",fill ="red") +geom_text(aes(label = paste0(percent, "%"), vjust = -0.4),size=7.5)+
  labs(y = " ", x = " ", title = "I think that i would need the support of technical person to be able to use this system") + 
  theme(plot.title = element_text(size=18),axis.text.y = element_blank(), axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1)) 

ggplot(e, aes(x = x5, y = percent))+geom_bar(stat ="identity",fill ="red") +geom_text(aes(label = paste0(percent, "%"), vjust = -0.4),size=7.5)+
  labs(y = " ", x = " ", title = "I found the various functions in this system were well integrated") + 
  theme(plot.title = element_text(size=18),axis.text.y = element_blank(), axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1)) 

ggplot(f, aes(x = x6, y = percent))+geom_bar(stat ="identity",fill ="red") +geom_text(aes(label = paste0(percent, "%"), vjust = -0.4),size=7.5)+
  labs(y = " ", x = " ", title = "I thought there was too much inconsistency in this system ") + 
  theme(plot.title = element_text(size=18),axis.text.y = element_blank(), axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1)) 

ggplot(g, aes(x = x7, y = percent))+geom_bar(stat ="identity",fill ="red") +geom_text(aes(label = paste0(percent, "%"), vjust = -0.4),size=7.5)+
  labs(y = " ", x = " ", title = "I would imagine that most people would learn to use this system very quickly ") + 
  theme(plot.title = element_text(size=18),axis.text.y = element_blank(), axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1)) 

ggplot(h, aes(x = x8, y = percent))+geom_bar(stat ="identity",fill ="red") +geom_text(aes(label = paste0(percent, "%"), vjust = -0.4),size=7.5)+
  labs(y = " ", x = " ", title = "I found the system very cumbersome to use") + 
  theme(plot.title = element_text(size=18),axis.text.y = element_blank(), axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1)) 

ggplot(i, aes(x = x9, y = percent))+geom_bar(stat ="identity",fill ="red") +geom_text(aes(label = paste0(percent, "%"), vjust = -0.4),size=7.5)+
  labs(y = " ", x = " ", title = "I felt very confident using the system") + 
  theme(plot.title = element_text(size=18),axis.text.y = element_blank(), axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1)) 

ggplot(j, aes(x = x10, y = percent))+geom_bar(stat ="identity",fill ="red") +geom_text(aes(label = paste0(percent, "%"), vjust = -0.4),size=7.5)+
  labs(y = " ", x = " ", title = " I needed to learn a lot of things \n before i could get going with the \n system ") +
  theme(plot.title = element_text(size=18), axis.text.y = element_blank(), axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1)) 

####Recoding

usb <- Mydata_sampled %>% transmute(x1 = ifelse(x1 == "strongly disagree", 1, ifelse(x1=="disagree", 2,
                  ifelse(x1=="neutral", 3, ifelse(x1=="agree", 4, 5)))),
                  x2 = ifelse(x2 == "strongly disagree", 1, ifelse(x2=="disagree", 2,
                                                                   ifelse(x1=="neutral", 3, ifelse(x2=="agree", 4, 5)))),
                  x3 = ifelse(x3 == "strongly disagree", 1, ifelse(x3=="disagree", 2,
                                                                   ifelse(x1=="neutral", 3, ifelse(x3=="agree", 4, 5)))),
                  x4 = ifelse(x4 == "strongly disagree", 1, ifelse(x4=="disagree", 2,
                                                                   ifelse(x1=="neutral", 3, ifelse(x4=="agree", 4, 5)))),
                  x5 = ifelse(x5 == "strongly disagree", 1, ifelse(x5=="disagree", 2,
                                                                   ifelse(x1=="neutral", 3, ifelse(x5=="agree", 4, 5)))),
                  x6 = ifelse(x6 == "strongly disagree", 1, ifelse(x6=="disagree", 2,
                                                                   ifelse(x1=="neutral", 3, ifelse(x6=="agree", 4, 5)))),
                  x7 = ifelse(x7 == "strongly disagree", 1, ifelse(x7=="disagree", 2,
                                                                   ifelse(x1=="neutral", 3, ifelse(x7=="agree", 4, 5)))),
                  x8 = ifelse(x8 == "strongly disagree", 1, ifelse(x8=="disagree", 2,
                                                                   ifelse(x1=="neutral", 3, ifelse(x8=="agree", 4, 5)))),
                  x9 = ifelse(x9 == "strongly disagree", 1, ifelse(x9=="disagree", 2,
                                                                   ifelse(x1=="neutral", 3, ifelse(x9=="agree", 4, 5)))),
                  x10 = ifelse(x10 == "strongly disagree", 1, ifelse(x10=="disagree", 2,
                                                                   ifelse(x1=="neutral", 3, ifelse(x10=="agree", 4, 5))))
                  )
str(usb)
## 'data.frame':    500 obs. of  10 variables:
##  $ x1 : num  5 5 5 5 3 2 3 3 5 4 ...
##  $ x2 : num  4 2 5 5 4 4 5 5 5 2 ...
##  $ x3 : num  5 5 5 4 5 5 4 4 2 4 ...
##  $ x4 : num  2 5 4 5 5 4 5 5 5 2 ...
##  $ x5 : num  5 5 4 5 5 5 2 2 2 4 ...
##  $ x6 : num  4 4 5 4 5 5 4 4 2 2 ...
##  $ x7 : num  4 5 4 4 5 5 2 2 5 4 ...
##  $ x8 : num  4 2 5 4 4 5 5 5 5 2 ...
##  $ x9 : num  4 5 4 4 5 5 2 2 5 5 ...
##  $ x10: num  5 5 4 5 5 2 4 4 4 2 ...
fun <- function(x) {
  value <- ifelse(colnames(x) %in% c("x1", "x3", "x5", "x7", "x9"), x - 1, 5 - x)
  return(value)
}

code <-as.data.frame(fun(usb))

rn <- code %>% select( x1= c.4..4..4..4..2..1..2..2..4..3..4..4..4..2..3..3..3..4..4..3..,
                       x2= c.1..3..0..0..1..1..0..0..0..3..0..0..0..1..0..0..0..4..1..1..,
                       x3= c.4..4..4..3..4..4..3..3..1..3..4..4..4..4..4..4..3..0..4..4..,
                       x4= c.3..0..1..0..0..1..0..0..0..3..0..1..1..0..1..0..3..4..3..3..,
                       x5= c.4..4..3..4..4..4..1..1..1..3..4..4..4..4..3..4..1..4..4..4..,
                       x6= c.1..1..0..1..0..0..1..1..3..3..0..0..0..0..0..0..1..4..1..0..,
                       x7= c.3..4..3..3..4..4..1..1..4..3..1..4..4..4..4..1..4..4..3..1..,
                       x8= c.1..3..0..1..1..0..0..0..0..3..0..0..0..1..3..0..1..4..1..1..,
                       x9= c.3..4..3..3..4..4..1..1..4..4..1..4..4..4..1..4..3..4..3..4..,
                       x10= c.0..0..1..0..0..3..1..1..1..3..3..0..0..0..3..0..4..4..0..0..
                      )

k <- as.data.frame(rowSums(rn)*2.5)
colSums(k)/500
## rowSums(rn) * 2.5 
##             56.48
num_rows <- nrow(k)

# Create user names using paste0 and the sequence of index positions
user_names <- paste0("user", 1:num_rows)

# Assign user names to the dataset
k$user <- user_names

dd <- k %>%
mutate(Grade = case_when(
rowSums(rn) * 2.5 >= 84 ~ "A+",
rowSums(rn) * 2.5 >= 80 & rowSums(rn) * 2.5 < 84 ~ "A",
  rowSums(rn) * 2.5 >= 78.9 & rowSums(rn) * 2.5 < 80 ~ "A-",
  rowSums(rn) * 2.5 >= 77.2 & rowSums(rn) * 2.5 < 78.9 ~ "B+",
  rowSums(rn) * 2.5 >= 74.1 & rowSums(rn) * 2.5 < 77.2 ~ "B",
  rowSums(rn) * 2.5 >= 72.6 & rowSums(rn) * 2.5 < 74.1 ~ "B-",
  rowSums(rn) * 2.5 >= 71.1 & rowSums(rn) * 2.5 < 72.6 ~ "C+",
  rowSums(rn) * 2.5 >= 65.0 & rowSums(rn) * 2.5 < 71.1 ~ "C",
  rowSums(rn) * 2.5 >= 62.7 & rowSums(rn) * 2.5 < 65.0 ~ "C-",
  rowSums(rn) * 2.5 < 62.7 ~ "D"
                    )) 
Ad <- k %>%
  mutate(Adjective = case_when(
    rowSums(rn) * 2.5 >= 84.1 ~ "Best Imaginable",
    rowSums(rn) * 2.5 >= 80.8 & rowSums(rn) * 2.5 < 84.0 ~ "Excellent",
    rowSums(rn) * 2.5 >= 71.1 & rowSums(rn) * 2.5 < 80.7 ~ "Good",
    rowSums(rn) * 2.5 < 71 ~ "Ok"
  ))
                  
Ac <- k %>%
  mutate(Acceptable = case_when(
    rowSums(rn) * 2.5 >= 71.1 ~ "Acceptable",
    rowSums(rn) * 2.5 < 71.0 ~ "Marginal"
  ))

NP <- Grade <- k %>%
  mutate(NPS = case_when(
    rowSums(rn) * 2.5 >= 79.9 ~ "Promoter",
    rowSums(rn) * 2.5 >= 62 & rowSums(rn) * 2.5 < 78.8 ~ "Passive",
    rowSums(rn) * 2.5 < 62.6 ~ "Detractor"
  ))

dd %>% group_by(Grade) %>% summarise(sum=n()) %>% ggplot(aes(x = Grade, y = sum))+
  geom_bar(stat="identity", fill="red")+geom_text(aes(label = paste0(sum), vjust = -0.4),size=5)+
  labs(y = " ", x = " ", title = "Average SUS scores associated with different \n Grade classes from 500 respondents. ") + 
  theme(plot.title = element_text(size=18),axis.text.y = element_blank(), 
        axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1)) 

Ad %>% group_by(Adjective) %>% summarise(sum = n()) %>% ggplot(aes(x = Adjective, y = sum))+
  geom_bar(stat="identity", fill="red")+geom_text(aes(label = paste0(sum), vjust = -0.4),size=5)+
  labs(y = " ", x = " ", title = "Average SUS scores associated with different \n Adjective classes from 500 respondents. ") + 
  theme(plot.title = element_text(size=18),axis.text.y = element_blank(), 
        axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1)) 

Ac %>% group_by(Acceptable) %>% summarise(sum = n()) %>% ggplot(aes(x = Acceptable, y = sum))+
  geom_bar(stat="identity", fill="red")+geom_text(aes(label = paste0(sum), vjust = -0.4),size=5)+
  labs(y = " ", x = " ", title = "Average SUS scores associated with different \n Acceptable classes from 500 respondents. ") + 
  theme(plot.title = element_text(size=18),axis.text.y = element_blank(), 
        axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1)) 

NP %>% group_by(NPS) %>% summarise(sum = n()) %>% ggplot(aes(x = NPS, y = sum))+
  geom_bar(stat="identity", fill="red")+geom_text(aes(label = paste0(sum), vjust = -0.4),size=5)+
  labs(y = " ", x = " ", title = "Average SUS scores associated with different \n NPS classes from 500 respondents. ") + 
  theme(plot.title = element_text(size=18),axis.text.y = element_blank(), 
        axis.text.x = element_text(face="bold", color='black', size = 18, angle = 45, hjust=1))