Data Visualization Lab Digital Assignment 1
Data Visualization Lab Digital Assignment 1
Q1: Write a R program to take input from the user (name and age) and display the
values. Also print the version of R installation.
Code and Output:
Q4: Write a R program to create a vector which contains 10 random integer values
between -50 and +50
Code and Output:
Q5: Write a R program to get the first 10 Fibonacci numbers.
Code and Output:
Q6: Write a R program to extract first 10 English letter in lower case and last 10 letters
in upper case and extract letters between 22nd to 24th letters in upper case.
Code and Output:
Q7: Write a R program to find the factors of a given number.
Code and Output:
Q8: Write a R program to find the maximum and the minimum value of a given vector.
Code and Output:
Q9: Write a R program to get the unique elements of a given string and unique
numbers of vector.
Code and Output:
Q10: Write a R program to create three vectors a, b ,c with 3 integers. Combine the
three vectors to become a 3×3 matrix where each column represents a vector. Print the
content of the matrix.
Code and Output:
Q11: Write a R program to create a list of random numbers in normal distribution and
count occurrences of each value.
Code and Output:
Q12: Write a R program to read the .csv file and display the content.
Code and Output:
CSV File:
Q13: Write a R program to create three vectors numeric data, character data and
logical data. Display the content of the vectors and their type.
Code and Output:
Q14: Write a R program to create a 5 x 4 matrix, 3 x 3 matrix with labels and fill the
matrix by rows and 2 × 2 matrix with labels and fill the matrix by columns.
Code and Output:
Q15: Write a R program to create an array, passing in a vector of values and a vector of
dimensions. Also provide names for each dimension.
Code and Output:
Complete Code:
#Question 3
print("Sequence of numbers from 20 to 50: ")
print(seq(20,50))
print("Mean of numbers from 20 to 60: ")
print(mean(20:60))
print("Sum of numbers from 51 to 91: ")
print(sum(51:91))
#Question 4
v=sample(-50:50, 10, replace=TRUE)
print("Contents of the vector: ")
print("10 random integer values between -50 and 50: ")
print(v)
#Question 5
Fibonacci <- numeric(10)
Fibonacci[1] <- Fibonacci[2] <- 1
for(i in 3:10) Fibonacci[i] <- Fibonacci[i-2] + Fibonacci[i-1]
print("First 10 Fibonacci numbers are: ")
print(Fibonacci)
#Question 6
print("First 10 letters of the alphabet in lower case: ")
t=head(letters, 10)
print(t)
print("Last 10 letters of the alphabet in upper case are: ")
t=tail(LETTERS, 10)
print(t)
print("Letters between the 22nd and 24th letters of the alphabet: ")
e=tail(LETTERS[22:24])
print(e)
#Question 7
print_factors <- function(x) {
print(paste("The factors of",x,"are:"))
for(i in 1:x) {
if((x %% i) == 0) {
print(i)
}
}
}
print_factors(140)
#Question 8
nums=c(14, 1, 2000, 22, 67, 31)
print("Original Vector: ")
print(nums)
print(paste("Maximum Value of the said vector: ", max(nums)))
print(paste("Minimum value of the said Vector: ", min(nums)))
#Question 9
string1= "To be or not to be, that is the question"
print("Original vector(string):")
print(string1)
print("Unique elements of the said vector: ")
print(unique(tolower(string1)))
numbers= c(1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0)
print("Original vector(number): ")
print(numbers)
print("Unique elements in the above vector: ")
print(unique(numbers))
#Question 10
a<-c(1,2,3)
b<-c(4,5,6)
c<-c(7,8,9)
matrix<-cbind(a,b,c)
print("Content of the combined matrix: ")
print(matrix)
#Question 11
n = floor(rnorm(500, 50, 100))
print("List of random numbers in normal distribution: ")
print(n)
t=table(n)
print("Count occurrences of each value: ")
print(t)
#Question 12
data<- read.csv(file="C:/Users/samar/OneDrive/Desktop/DV Lab/input.csv")
print("Content of the .csv file: ")
print(data)
#Question 13
a=c(1,2,3,4,5,6,7,8,9)
b=c("Samar","Abbas","Naqvi")
c=c(TRUE,TRUE, FALSE, FALSE, TRUE)
print(a)
print(typeof(a))
print(b)
print(typeof(b))
print(c)
print(typeof(c))
#Question 14
m1= matrix(1:20, nrow=5, ncol=4)
print("5x4 matrix: ")
print(m1)
cells= c(1,3,5,7,8,8,9,11,12,14)
rownames=c("Row1","Row2","Row3")
colnames=c("Col1","Col2","Col3")
m2= matrix(cells, nrow=3, ncol=3, byrow=TRUE, dimnames=list(rownames, colnames))
print("3x3 matrix with labels, filled by rows: ")
print(m2)
rownames2=c("Row1","Row2")
colnames2=c("Col1","Col2")
cells2= c(0,4,5,6)
m3=matrix(cells2, nrow=2, ncol=2, byrow=FALSE, dimnames=list(rownames2, colnames2))
print("2x2 matrix with labesl, filled by columns: ")
print(m3)
#Question 15
a = array(6:30,
dim=c(4,3,2),
dimnames=list(
c("Col1","Col2","Col3","Col4"),
c("Row1","Row2","Row3"),
c("Part1","Part2")
))
print(a)