SHARE
TWEET

R codes

Ashenk97 Sep 19th, 2019 (edited) 133 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Extra
  2.  
  3. #.xlsx --> .csv
  4.  
  5. #Opening csv file
  6. #data{#variable name} <- read.csv('filename.csv', header=TRUE)
  7.  
  8. #Opening text file
  9. #data{#variable name} <- read.table('filename.txt', header=TRUE,sep=","{#Because data is seperated by ','s})
  10.  
  11. #To clear console
  12. #Ctrl + L
  13.  
  14. #---------------------------------------------------------------------------------------------------
  15.  
  16. #Set Path
  17. setwd("F:\\SLIIT\\Practicals\\Year 02 Semester 02\\PS\\R")
  18. getwd()
  19.  
  20. #Lab7 - 1
  21.  
  22. #read data from the text file
  23. data<-read.table("lab7.txt",header=TRUE,sep=",")
  24. attach(data)
  25. fix(data)
  26.  
  27.  
  28. #Lab7 - 2
  29. #Summary/Strucure of data variable
  30. str(data)
  31.  
  32.  
  33. #Lab7 - 3
  34. #str - Used to count obesrvations
  35. #Observations = 517
  36.  
  37. #Lab7 - 4
  38. min(wind)
  39. max(wind)
  40.  
  41. #Lab7 - 5
  42. summary(temp)
  43.  
  44. #Lab7 - 6
  45. boxplot(wind, horizontal = TRUE, outline = TRUE, pch=16)
  46. #pch = Symbols for representing outliers. ex:-8,16,24
  47.  
  48. #Lab7 - 7(Negative,Positive,Normal)
  49. #Negative
  50.  
  51. #Lab7 - 8
  52. median(temp)
  53.  
  54. #Lab7 - 9
  55. mean(wind)
  56. sd(wind)
  57.  
  58. sapply(data,sd)
  59.  
  60. #Lab7 - 10
  61. IQR(wind)
  62.  
  63. #Lab7 - 11
  64. #Create a frequency table with day &month count
  65. freq<-table(day, month)
  66. freq
  67. #Answer = 21
  68.  
  69. #Lab7 - 12
  70. #Average = Mean
  71. mean(temp[month=='sep'])
  72.  
  73. #Lab7 - 13
  74. barplot(freq,beside = TRUE,xlab = "Month",ylab="Frequency",legend=rownames(freq))
  75. #Beside = TRUE --> Bars in seperate
  76. #Beside = FALSE --> Bars in one
  77.  
  78. #Population Mean
  79. mean(nicotine)
  80.  
  81. #Variance
  82. var(nicotine)
  83.  
  84. #Standard Deviation
  85. sd(nicotine)
  86.  
  87. #Lab6 - 2
  88.  
  89. #Sample values (5 samples)
  90. sam<-sample(nicotine,5)
  91. sam
  92.  
  93. #5 numbers of random values
  94. #i is variable
  95. for(i in 1:30){
  96. s<-sample(nicotine,5)
  97. samples1<-cbind(samples1,s)
  98. n<-c(n,paste('s',i))
  99. }
  100.  
  101. #Calculate the mean.
  102. s.mean<-colMeans(samples1)
  103. s.mean
  104.  
  105. #Calculate variance(2 ways of finding variance.row vise and column vise)
  106.  
  107. #row vise-->
  108. s.vars<-apply(samples1,1,var)
  109. s.vars
  110.  
  111. #column vise
  112. s.vars1<-apply(samples1,2,var)
  113. s.vars1
  114.  
  115. #Comparison of population mean and mean of sample means
  116. msm<-mean(s.mean)
  117. msm
  118.  
  119. #Comparison of population variance and mean of sample variance of sample means
  120. vsm<-var(s.vars)
  121. vsm
  122.  
  123. #Controll Statements
  124. team_a <-3
  125. team_b <-4 #Assigning values
  126.  
  127. #this is a sample if else Statement
  128. if(team_a>team_b){
  129.   print("Team A Wins")
  130. }else{
  131.   print("Team B Wins")
  132. }
  133.  
  134. setwd("F:\\SLIIT\\Practicals\\Year 02 Semester 02\\PS\\R")
  135.  
  136. #Get the keyboard input from the keyboard
  137. #my.name is a variable u can use any word
  138. my.name <- readline(prompt = "Enter Name : ")
  139. my.age <- readline(prompt = "Enter age : ")
  140.  
  141. if(myAge < 18){
  142.   print("You are not major")
  143. }else{
  144.   if(myAge >= 18 & myAge <= 60){
  145.     print("You are eligeble to work")
  146.   }
  147.   else{
  148.     print("Collect your Pension")
  149.   }
  150. }
  151.  
  152. #Frequency Table
  153. gender.freq<-table(Gender)
  154. gender.freq
  155.  
  156. accomadation.freq<-table(Accomadation)
  157. accomadation.freq
  158.  
  159. #Pie chart
  160. pie(gender.freq, main = "Pie chart for gender")
  161. pie(accomadation.freq, main = "Pie chart for accomadation")
  162.  
  163. #Bar chart
  164. barplot(gender.freq,main="bar chart for Gender", ylab = "Frequency", col = "Red")
  165. #To get x axis
  166. abline(h=0)
  167.  
  168. barplot(accomadation.freq,main="bar chart for Accomadation", ylab = "Frequency")
  169. #To get x axis
  170. abline(h=0)
  171.  
  172. #Two way frequency table
  173. gender_accomadation.freq<-table(Accomadation,Gender)
  174. gender_accomadation.freq
  175.  
  176. #Stack bar chart
  177. barplot(gender_accomadation.freq, main = "Gender and Accomadation",legend = rownames(gender_accomadation.freq))
  178. abline(h=0)
  179.  
  180. #Clustered bar chart
  181. barplot(gender_accomadation.freq, beside = TRUE, main = "Gender and Accomadation",legend = rownames(gender_accomadation.freq))
  182. abline(h=0)
  183.  
  184. #Histogram
  185. hist(attendance,main = "Histogram for Attendance",ylab = "Frequency")
  186. hist(salary,main = "Histogram for Salary",ylab = "Frequency")
  187. hist(years,main = "Histogram for Years",ylab = "Frequency")
  188.  
  189. #Stem-Leaf Plot
  190. stem(attendance)
  191. stem(salary)
  192. stem(years)
  193.  
  194. #Function for find the modes of a given set of values.
  195. get.mode<-function(x){
  196.   counts<-table(x)
  197.   names(counts[counts==max(counts)])
  198. }
  199.  
  200. quantile(years)
  201.  
  202. find.outliers<-function(x){
  203.     q1<-quantile(x)[2]
  204.     q3<-quantile(x)[4]
  205.  
  206.     iqr<-q3-q1
  207.  
  208.     ub<-q3+1.5*iqr
  209.     lb<-q3-1.5*iqr
  210.  
  211.     print(paste("Upper Bound = ",ub))
  212.     print(paste("Lower Bound = ",lb))
  213.  
  214.     print(paste("Outliers : ",paste(sort(x[x<lb | x>ub]),collapse = ",")))    
  215.   }
  216.  
  217. find.outliers(years)
  218.  
  219. sheep<-data.frame(height,weight)
  220. fix(sheep)
  221.  
  222. #Exporting Data frames
  223.  
  224. #Export Data to CSV and txt
  225. write.csv(sheep,file = "sheepNew.csv")
  226. write.table(sheep,file = "sheeptab1.txt")
  227.  
  228. histogram<-hist(X2,main="Histogram for number of shareholders",breaks = seq(130,270,length=8))
  229.  
  230. freq<-histogram$counts
  231. freq
  232.  
  233. classes<-c()
  234. for(i in 1:length(breaks)-1){
  235.   classes[i]<-paste("[", breaks[i],",", breaks[i+1],")")
  236. }
  237.  
  238. #column bind - can be used two combine 2 data frames with the same values
  239. cbind(Classes = classes, Frequency = freq)
  240.  
  241. #Draw frequency polygon in a new plot.(type = p for ponts and type = l for lines)
  242. plot(mids, freq, type = 'l', main = "Frequency Polygon for shareholders",xlab="shareholders",ylab="frequency",ylim = c(0,max(freq)))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top