Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Practical 1 (Write a Program to find sum, mean and product of a vector infinte NA or NULL)
- x<-c(1,2,3,4,5)
- x
- sum(x)
- mean(x)
- prod(x)
- cat("Using na.rm Function")
- x1<-c(1,2,NA,4,5,NA)
- x1
- sum(x1,na.rm=TRUE)
- mean(x1,na.rm=TRUE)
- prod(x1,na.rm=TRUE)
- //Practical 2 (Write a program to create a vector using (:) operator & seq() function)
- x<-1:10
- x
- cat("Using seq() by")
- y<-seq(1,3,by=0.3)
- y
- cat("Using seq() length.out")
- z<-seq(1,5,length.out=6)
- z
- cat("Using seq() from")
- a<-seq(15,2,from=6)
- a
- cat("Using seq() to")
- b<-seq(10,2,to=20)
- b
- //Practical 3 (Write a program to list containing string, number, vector, matrix, and a logical value. Perform an 3 operation on list)
- 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"))
- list1
- names(list1)<- c("Colors","Numbers","Vector","Matrix","Logical Values")
- list1
- #Accessing the list elements
- print(list1[3])
- print(list1[5])
- #list Manipulation (Adding,Updating,Deleting elements)
- list1[6]<-"New Element"
- list1
- list1[2]<-"Update Element"
- list1
- list1[6]<-NULL
- list1
- #Merging two list
- list2<-list(1,2,3)
- list3<-list("Sunday","Monday","Tuesday")
- list2
- list3
- mergelist<-c(list2,list3)
- mergelist
- #Converting list to vector
- list4<-list(1:5)
- list5<-list(6:10)
- list4
- list5
- v4<-unlist(list4)
- v5<-unlist(list5)
- v4
- v5
- add<-v4+v5
- add
- mul<-v4*v5
- mul
- sub<-v4-v5
- sub
- div<-v4/v5
- div
- //Practica 4 (Write a program to create a list of dataframe and access each of these dataframe separatly)
- #Creating a list of dataframe
- d1<-data.frame(y1=c(1,2,3),y2=c(4,5,6))
- d2<-data.frame(y1=c(7,8,9),y2=c(1,4,6))
- ListofDataFrame<-list(d1,d2)
- ListofDataFrame
- #Accessing components of dataframe
- ListofDataFrame<-list("DataFrame1"=d1,"DataFrame2"=d2)
- ListofDataFrame
- print(ListofDataFrame$DataFrame2)
- print(ListofDataFrame[1])
- print(ListofDataFrame[[1]][2])
- print(ListofDataFrame[[2]][2,2])
- #Modifying components of list of Dataframe
- ListofDataFrame[[2]][[2]][3]<-c(10)
- ListofDataFrame
- #Concatenation of list of DataFrame
- d3<-data.frame(y1=c(5,5,6),y2=c(9,1,2))
- d3
- new<-list("DataFrame3"=d3)
- new
- ListofDataFrame<-c(new,ListofDataFrame)
- ListofDataFrame
- #Deleting Components from a list of Dataframe
- print(ListofDataFrame[[2]][-1])
- print(ListofDataFrame$DataFrame2[-1]<-NULL)
- ListofDataFrame
- //Practical 6 (Write a program to create a data frame from four given values)
- Name<-c("Rakesh","Pooja","Kamlesh","Paresh","Anjali","Akansha","Mangesh")
- Gender<-c("Male","Female","Male","Male","Female","Female","Male")
- Age<-c(23,56,45,12,47,55,78)
- Marks<-c(66,78,44,89,67,99,56)
- StudentInfo<-data.frame(Name,Gender,Age,Marks)
- class(StudentInfo)
- StudentInfo
- #Concatinating one more column (ID)
- ID<-c(1,2,3,4,5,6,7)
- StudentInfo$ID<-ID
- StudentInfo
- //Practical 7 (Write a program to extract 3rd & 5th rows with 1st & 3rd columns from given DataFrame)
- 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"))
- cat("Original DataFrame")
- data
- #Extracting 3rd & 5th Row with 1st & 3rd Column
- result<-data[c(3,5),c(1,3)]
- result
- //Practical 8 (Write a R program to drop rows/columns by number from given DataFrame)
- 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"))
- data
- cat("Dropping 2nd Column")
- data[,-2]
- cat("Dropping 5th Row")
- data[-5,]
- //Practical 9 (Write R Program to create inner,outer, left, right join(merge) from given Two DataFrames)
- df1<- data.frame(numid=c(12,14,10,11))
- df2<- data.frame(numid=c(13,11,15,12))
- df1
- df2
- cat("Inner Join")
- print(result1<-merge(df1,df2,by='numid'))
- cat("Left Outer Join")
- print(result2<-merge(df1,df2,by='numid',all.x=TRUE))
- cat("Right Outer Join")
- print(result3<-merge(df1,df2,by='numid',all.y=TRUE))
- cat("Outer Join")
- print(result4<-merge(df1,df2,by='numid',all=TRUE))
- cat("Cross Join")
- print(result5<-merge(df1,df2,by=NULL))
- //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)
- Name<-c("Rakesh","Pooja","Kamlesh","Paresh","Anjali","Akansha","Mangesh","Anjali","Tara","Rakesh","Ajay")
- Age<-c(23,56,45,12,47,55,78,47,33,23,49)
- Salary<-c(22000,52000,19000,45000,66000,36000,55000,66000,42000,22000,67000)
- df<-data.frame(Name,Age,Salary)
- df
- cat("Duplicated Data")
- print(duplicated(df))
- cat("Unique Data")
- print(unique(df))
- //Practical 11A (Implement matrices addition,subtraction,multiplication,division)
- A<-matrix(c(1,2,3,4,5,6),nrow=2,ncol=3)
- B<-matrix(c(7,8,9,10,11,12),nrow=2,ncol=3)
- A
- B
- cat("Addtion")
- Sum<-A+B
- Sum
- cat("Subtraction")
- Sub<-A-B
- Sub
- cat("Multiplication")
- Mul<-A*B
- Mul
- cat("Division")
- Div<-A/B
- Div
- //Practical 11B (Write a R program to create a matrix perform operations on a matrix)
- A<- matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3,byrow=TRUE)
- rownames(A)=c("1r","2r","3r") #Giving Names to Rows
- colnames(A)=c("1c","2c","3c") #Giving Names to Columns
- A
- #Accessing
- print(A[1:2,]) #Accessing 1st & 2nd Rows
- print(A[,1:2]) #Accessing 1st & 2nd Columns
- print(A[1,2]) #Accessing 1st row 2nd Element
- print(A[2,3]) #Accessing 2nd row 3rd Element
- #Modifying
- A[3,3]=30
- A
- #Concatenation
- B<-matrix(c(10,11,12),nrow=1,ncol=3)
- B
- C<-rbind(A,B)
- C
- #Deleting rows & columns
- print(A[-1,])
- print(A[,-1])
- //Practical 12(Demonstrate any five visulization tools with help of suitable data)
- Excel Save as CSV(MS-DOS) extension se save krna file
- Location copy kro file ka aur "/" daalo ye "\" nhi
- data<-read.csv("location ya pe paste kro/student.csv")
- data
- cat("Line Plot")
- plot(data$Total,data$Percentage,type="b",main="Marks Distributuion")
- cat("Box Plot")
- cg<-data$Percentage
- boxplot(cg)
- cat("Histogram")
- hist(data$Percentage,breaks=15,col="green")
- cat("Density Plot")
- density_data<-density(data$Percentage)
- plot(density_data,main="Marks Distribution")
- polygon(density_data,main="Marks Distribution",col='cyan',border="black")
- cat("Bar Plot")
- bar<-table(data$Percentage)
- barplot(bar,horiz=TRUE,main="Simple Marks Distribution",xlab="Marks",ylab="Percentage",col=c("pink","red","blue","cyan","orange","green"))
- #3D Graph
- cat("3D Graph")
- x <- -10:10
- y <- -10:10
- z_values <- function(x, y) {
- sqrt(x ^ 2 + y ^ 2)
- }
- z = outer(x, y, z_values)
- persp(x, y, z, xlab='X Variable', ylab='Y Variable', zlab='Z Variable', main='3D Plot', col='pink', shade=.4)
- #Time Series
- rainfall <- c(799,1174.8,865.1,1334.6,635.4,918.5,685.5,998.6,784.2,985,882.8,1071)
- rainfall.timeseries <- ts(rainfall,start = c(2012,1),frequency = 12)
- print(rainfall.timeseries)
- plot(rainfall.timeseries)
- #Linear Model
- x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
- y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
- relation <- lm(y~x)
- print(relation)
- summary(relation)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement