Advertisement
Xonx

number 1 backup

Dec 15th, 2022
1,815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.55 KB | Source Code | 0 0
  1. #Create a student file (either .csv/.xlsx) that contains:
  2. #Student Number
  3. #Course (BSCS, BSIS, and BSIT only)
  4. #Year
  5. #Section
  6.  
  7. #Create unsorted 50 records of student.
  8. #Analyze the records according to the courses, year and section. #Present the data in a graph.
  9.  
  10. #Determine how many students per course (pie graph)
  11. #Determine how many students per year level in the course (line graph) #Determine how many students per section in every year level and course (bar graph)
  12.  
  13. library("readxl")
  14. library("writexl")
  15.  
  16. courses <- c("BSCS", "BSIS", "BSIT")
  17. #courses is for reference only. could be used if decided.
  18. stud_data<-function(){
  19. studnum <- c()
  20. studname <- c()
  21. studcourse <- c()
  22. studyr <- c()
  23. section <- c()
  24.  
  25.  
  26. #requirement is up to 50 students only
  27.  
  28. i<-1
  29. max <- 50
  30. cont<-'exit'
  31. roww<-0
  32.  
  33. while(cont!="n"||cont!="N"){
  34.  
  35.   if(file.exists("StudentRecord.xlsx")==TRUE){
  36.     dt<-read_excel("StudentRecord.xlsx")
  37.     items<-nrow(dt)
  38.     roww=items
  39.     print(paste("Record(s) : ",items))
  40.     if(items==50){print("Record Full!");print("Only accepts 50 records");
  41.       print("remove some data to continue...");
  42.       break}
  43.   }
  44.  
  45.   #check the number of students inside file
  46.  
  47.   snum <- readline(prompt=("STUDENT NUMBER > ")); print("________________")
  48.   sname <- readline(prompt=("STUDENT NAME >")); print("________________")
  49.   scourse <- readline(prompt=("[BSCS/BSIS/BSIT] COURSE >")); print("________________")
  50.   syr <- readline(prompt=("YEAR >")); print("________________")
  51.   sect <- readline(prompt=("SECTION >")); print("________________")
  52.  
  53.   studnum[i]=snum
  54.   studname[i]=sname
  55.   studcourse[i]=scourse
  56.   studyr[i]=syr
  57.   section[i]=sect
  58.  
  59.   print("=========================================")
  60.   cont = readline(prompt=("Save Input? [y/n] >"))
  61.  
  62.   if(cont=='y'||cont=='Y'){ i<- i+1
  63.  
  64.  
  65.   stud_data=data.frame(
  66.     StudentNumber=c(studnum),
  67.     StudentName=c(studname),
  68.     StudentCourse=c(studcourse),
  69.     StudentYear=c(studyr),
  70.     Sections=c(section)
  71.   )
  72.   if(file.exists("StudentRecord.xlsx")==FALSE&&roww==0){
  73.     write_xlsx(stud_data,"StudentRecord.xlsx", col_names = TRUE)
  74.   }
  75.   else{
  76.     write_xlsx(stud_data,"Temporary.xlsx", col_names = TRUE)
  77.     dt1<-read_excel("Temporary.xlsx")
  78.     dt2<-read_excel("StudentRecord.xlsx")
  79.     dtm<-merge(dt1,dt2, all.x=TRUE, all.y=TRUE)
  80.     write_xlsx(dtm,"StudentRecord.xlsx")
  81.     file.remove("Temporary.xlsx")
  82.   }
  83.  
  84.   cont=readline(prompt=("Input Again? [y/n] >"))
  85.   if(cont=='n'||cont=='N'){break;}
  86.  
  87.   }
  88.   else if(cont=='N'||cont=='n'){
  89.     cont=readline(prompt=("Input Again? [y/n] >"))
  90.     if(cont=='n'||cont=='N'){break;}
  91.   }
  92.   else if((cont!='y'|cont!='Y')&&(cont!='N'|cont!='n'))
  93.   { print("Invalid Input.");}
  94.  
  95.  
  96. }
  97. }
  98.  
  99. mn<-function(){
  100. #x=stud_data()
  101. #^call only stud_data for inputting records
  102. if(file.exists("StudentRecord.xlsx")==TRUE){
  103.   dt<-read_excel("StudentRecord.xlsx")
  104.   s1<-sum(dt$StudentCourse=="BSIT")
  105.   s2<-sum(dt$StudentCourse=="BSIS")
  106.   s3<-sum(dt$StudentCourse=="BSCS")
  107.   s1<-paste("BSIT = ",s1)
  108.   s2<-paste("BSIS = ",s2)
  109.   s3<-paste("BSCS = ",s3)
  110.   lbl<-c(s1,s2,s3)
  111.   vals<-as.numeric(gsub("[^[:digit:]]","",lbl))
  112.   names(vals)<-seq_along(vals)
  113.   lbl<-lbl[as.numeric(names(sort(vals, decreasing=TRUE)))]
  114.  
  115.   pie(table(dt$StudentCourse), main="Number of Students per Course", col=rainbow(3),clockwise=TRUE)
  116.   legend("bottomright",lbl)
  117.   #displaying students per course pie chart
  118.   graphics.off()
  119.   require(ggplot2)
  120.   library(ggplot2)
  121.  
  122.  ggplot(data=dt,aes(x=StudentYear,y=StudentCourse))+geom_line(group=1)+geom_count()
  123.  
  124.  
  125.  
  126. }
  127. }
  128.  
  129. mn()
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement