Advertisement
MK7265

Untitled

Mar 7th, 2023 (edited)
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 7.06 KB | None | 0 0
  1. //Practical 1 (Write a Program to find sum, mean and product of a vector infinte NA or NULL)
  2.  
  3. x<-c(1,2,3,4,5)
  4. x
  5. sum(x)
  6. mean(x)
  7. prod(x)
  8. cat("Using na.rm Function")
  9. x1<-c(1,2,NA,4,5,NA)
  10. x1
  11. sum(x1,na.rm=TRUE)
  12. mean(x1,na.rm=TRUE)
  13. prod(x1,na.rm=TRUE)
  14.  
  15. //Practical 2 (Write a program to create a vector using (:) operator & seq() function)
  16.  
  17. x<-1:10
  18. x
  19. cat("Using seq() by")
  20. y<-seq(1,3,by=0.3)
  21. y
  22. cat("Using seq() length.out")
  23. z<-seq(1,5,length.out=6)
  24. z
  25. cat("Using seq() from")
  26. a<-seq(15,2,from=6)
  27. a
  28. cat("Using seq() to")
  29. b<-seq(10,2,to=20)
  30. b
  31.  
  32. //Practical 3 (Write a program to list containing string, number, vector, matrix, and a logical value. Perform an 3 operation on list)
  33.  
  34. list1<- list(c("Red","Blue","Green"),c(1,2,3,4,5),c(6,7,8,9,10),matrix(c(2,5,8,9,11,12),nrow=2),c("TRUE","FALSE","NA"))
  35. list1
  36. names(list1)<- c("Colors","Numbers","Vector","Matrix","Logical Values")
  37. list1
  38.  
  39. #Accessing the list elements
  40. print(list1[3])
  41. print(list1[5])
  42.  
  43. #list Manipulation (Adding,Updating,Deleting elements)
  44. list1[6]<-"New Element"
  45. list1
  46.  
  47. list1[2]<-"Update Element"
  48. list1
  49.  
  50. list1[6]<-NULL
  51. list1
  52.  
  53. #Merging two list
  54. list2<-list(1,2,3)
  55. list3<-list("Sunday","Monday","Tuesday")
  56. list2
  57. list3
  58. mergelist<-c(list2,list3)
  59. mergelist
  60.  
  61. #Converting list to vector
  62. list4<-list(1:5)
  63. list5<-list(6:10)
  64. list4
  65. list5
  66. v4<-unlist(list4)
  67. v5<-unlist(list5)
  68. v4
  69. v5
  70. add<-v4+v5
  71. add
  72. mul<-v4*v5
  73. mul
  74. sub<-v4-v5
  75. sub
  76. div<-v4/v5
  77. div
  78.  
  79. //Practica 4 (Write a program to create a list of dataframe and access each of these dataframe separatly)
  80.  
  81. #Creating a list of dataframe
  82.  
  83. d1<-data.frame(y1=c(1,2,3),y2=c(4,5,6))
  84. d2<-data.frame(y1=c(7,8,9),y2=c(1,4,6))
  85. ListofDataFrame<-list(d1,d2)
  86. ListofDataFrame
  87.  
  88. #Accessing components of dataframe
  89.  
  90. ListofDataFrame<-list("DataFrame1"=d1,"DataFrame2"=d2)
  91. ListofDataFrame
  92. print(ListofDataFrame$DataFrame2)
  93. print(ListofDataFrame[1])
  94. print(ListofDataFrame[[1]][2])
  95. print(ListofDataFrame[[2]][2,2])
  96.  
  97. #Modifying components of list of Dataframe
  98.  
  99. ListofDataFrame[[2]][[2]][3]<-c(10)
  100. ListofDataFrame
  101.  
  102. #Concatenation of list of DataFrame
  103.  
  104. d3<-data.frame(y1=c(5,5,6),y2=c(9,1,2))
  105. d3
  106. new<-list("DataFrame3"=d3)
  107. new
  108. ListofDataFrame<-c(new,ListofDataFrame)
  109. ListofDataFrame
  110.  
  111. #Deleting Components from a list of Dataframe
  112.  
  113. print(ListofDataFrame[[2]][-1])
  114. print(ListofDataFrame$DataFrame2[-1]<-NULL)
  115. ListofDataFrame
  116.  
  117. //Practical 6 (Write a program to create a data frame from four given values)
  118.  
  119. Name<-c("Rakesh","Pooja","Kamlesh","Paresh","Anjali","Akansha","Mangesh")
  120. Gender<-c("Male","Female","Male","Male","Female","Female","Male")
  121. Age<-c(23,56,45,12,47,55,78)
  122. Marks<-c(66,78,44,89,67,99,56)
  123.  
  124. StudentInfo<-data.frame(Name,Gender,Age,Marks)
  125. class(StudentInfo)
  126. StudentInfo
  127.  
  128. #Concatinating one more column (ID)
  129. ID<-c(1,2,3,4,5,6,7)
  130. StudentInfo$ID<-ID
  131. StudentInfo
  132.  
  133. //Practical 7 (Write a program to extract 3rd & 5th rows with 1st & 3rd columns from given DataFrame)
  134.  
  135. data<-data.frame(Name=c("Rakesh","Pooja","Kamlesh","Paresh","Anjali","Akansha","Mangesh"),Marks=c(66,78,44,89,67,99,56),Age=c(23,56,45,12,47,55,78),Gender=c("Male","Female","Male","Male","Female","Female","Male"))
  136. cat("Original DataFrame")
  137. data
  138.  
  139. #Extracting 3rd & 5th Row with 1st & 3rd Column
  140. result<-data[c(3,5),c(1,3)]
  141. result
  142.  
  143. //Practical 8 (Write a R program to drop rows/columns by number from given DataFrame)
  144.  
  145. data<-data.frame(Name=c("Rakesh","Pooja","Kamlesh","Paresh","Anjali","Akansha","Mangesh"),Marks=c(66,78,44,89,67,99,56),Age=c(23,56,45,12,47,55,78),Gender=c("Male","Female","Male","Male","Female","Female","Male"))
  146. data
  147. cat("Dropping 2nd Column")
  148. data[,-2]
  149. cat("Dropping 5th Row")
  150. data[-5,]
  151.  
  152.  
  153. //Practical 9 (Write  R Program to create inner,outer, left, right join(merge) from given Two DataFrames)
  154.  
  155. df1<- data.frame(numid=c(12,14,10,11))
  156. df2<- data.frame(numid=c(13,11,15,12))
  157. df1
  158. df2
  159. cat("Inner Join")
  160. print(result1<-merge(df1,df2,by='numid'))
  161. cat("Left Outer Join")
  162. print(result2<-merge(df1,df2,by='numid',all.x=TRUE))
  163. cat("Right Outer Join")
  164. print(result3<-merge(df1,df2,by='numid',all.y=TRUE))
  165. cat("Outer Join")
  166. print(result4<-merge(df1,df2,by='numid',all=TRUE))
  167. cat("Cross Join")
  168. print(result5<-merge(df1,df2,by=NULL))
  169.  
  170.  
  171. //Practical 10 (Write an R program to create a dataframe using two gvien vectors and disply the duplicated elements and unique rows of the said DataFrame)
  172.  
  173.  
  174. Name<-c("Rakesh","Pooja","Kamlesh","Paresh","Anjali","Akansha","Mangesh","Anjali","Tara","Rakesh","Ajay")
  175. Age<-c(23,56,45,12,47,55,78,47,33,23,49)
  176. Salary<-c(22000,52000,19000,45000,66000,36000,55000,66000,42000,22000,67000)
  177. df<-data.frame(Name,Age,Salary)
  178. df
  179. cat("Duplicated Data")
  180. print(duplicated(df))
  181. cat("Unique Data")
  182. print(unique(df))
  183.  
  184. //Practical 11A (Implement matrices addition,subtraction,multiplication,division)
  185.  
  186. A<-matrix(c(1,2,3,4,5,6),nrow=2,ncol=3)
  187. B<-matrix(c(7,8,9,10,11,12),nrow=2,ncol=3)
  188. A
  189. B
  190. cat("Addtion")
  191. Sum<-A+B
  192. Sum
  193.  
  194. cat("Subtraction")
  195. Sub<-A-B
  196. Sub
  197.  
  198. cat("Multiplication")
  199. Mul<-A*B
  200. Mul
  201.  
  202. cat("Division")
  203. Div<-A/B
  204. Div
  205.  
  206. //Practical 11B (Write a R program to create a matrix perform operations on a matrix)
  207.  
  208. A<- matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3,byrow=TRUE)
  209. rownames(A)=c("1r","2r","3r")  #Giving Names to Rows
  210. colnames(A)=c("1c","2c","3c")  #Giving Names to Columns
  211. A
  212.  
  213. #Accessing
  214. print(A[1:2,])  #Accessing 1st & 2nd Rows
  215. print(A[,1:2])  #Accessing 1st & 2nd Columns
  216. print(A[1,2])   #Accessing 1st row 2nd Element
  217. print(A[2,3])   #Accessing 2nd row 3rd Element
  218.  
  219. #Modifying
  220. A[3,3]=30
  221. A
  222.  
  223.  
  224. #Concatenation
  225. B<-matrix(c(10,11,12),nrow=1,ncol=3)
  226. B
  227. C<-rbind(A,B)
  228. C
  229.  
  230. #Deleting rows & columns
  231. print(A[-1,])
  232. print(A[,-1])
  233.  
  234.  
  235. //Practical 12(Demonstrate any five visulization tools with help of suitable data)
  236.  
  237. Excel Save as CSV(MS-DOS) extension se save krna file
  238. Location copy kro file ka aur "/" daalo ye "\" nhi
  239.  
  240. data<-read.csv("location ya pe paste kro/student.csv")  
  241. data
  242.  
  243. cat("Line Plot")
  244. plot(data$Total,data$Percentage,type="b",main="Marks Distributuion")
  245.  
  246. cat("Box Plot")
  247. cg<-data$Percentage
  248. boxplot(cg)
  249.  
  250.  
  251. cat("Histogram")
  252. hist(data$Percentage,breaks=15,col="green")
  253.  
  254. cat("Density Plot")
  255. density_data<-density(data$Percentage)
  256. plot(density_data,main="Marks Distribution")
  257. polygon(density_data,main="Marks Distribution",col='cyan',border="black")
  258.  
  259. cat("Bar Plot")
  260. bar<-table(data$Percentage)
  261. barplot(bar,horiz=TRUE,main="Simple Marks Distribution",xlab="Marks",ylab="Percentage",col=c("pink","red","blue","cyan","orange","green"))
  262.  
  263.  
  264.  
  265. #3D Graph
  266. cat("3D Graph")
  267. x <- -10:10
  268. y <- -10:10
  269. z_values <- function(x, y) {
  270. sqrt(x ^ 2 + y ^ 2)
  271. }
  272. z = outer(x, y, z_values)
  273. persp(x, y, z, xlab='X Variable', ylab='Y Variable', zlab='Z Variable', main='3D Plot', col='pink', shade=.4)
  274.  
  275.  
  276. #Time Series
  277. rainfall <- c(799,1174.8,865.1,1334.6,635.4,918.5,685.5,998.6,784.2,985,882.8,1071)
  278. rainfall.timeseries <- ts(rainfall,start = c(2012,1),frequency = 12)
  279. print(rainfall.timeseries)
  280. plot(rainfall.timeseries)
  281.  
  282.  
  283. #Linear Model
  284. x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
  285. y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
  286. relation <- lm(y~x)
  287. print(relation)
  288. summary(relation)
  289.  
  290.  
  291.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement