You are on page 1of 3

EXPERIMENT – 6

DATE: 28.2.2020
DATA ANALYTICS LAB
NAME : DATA TYPES and DATA FRAMES and READING FILES

Q1: Creating a vector using seq() function


Sol:
seq(1, 3, by=0.2)

Q2: Creating a vector using seq() function


Sol:
seq(1, 5, length.out=4)

Q3:Creating a sequence from 5 to 13 and store in a variable v. print the


contents of v
Sol:
v <- 5:13
print(v)

Q4: Create two vectors. and perform multiplication of the two vectors and
store in a variable and then print

the contents of it

Sol:
v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11,0,8,1,2)
result <- v1*v2
print(result)

Q5: Sort the vector created in the Q4 and store the sorted vector in a new
vector and print
Sol:
sorted <- sort(result)
print(sorted)

Q6: Write a R program to create a matrix taking a given vector of numbers as


input. Display the matrix.
Sol:
M = matrix(c(1:16), nrow = 4, byrow = TRUE)
print("Original Matrix:")
print(M)
Q7: Write a R program to create a matrix taking a given vector of numbers as
input and define the column and row names. Display the matrix.
Sol:
row_names = c("row1", "row2", "row3", "row4")
col_names = c("col1", "col2", "col3", "col4")
M = matrix(c(1:16), nrow = 4, byrow = TRUE, dimnames = list(row_names,
col_names))
print("Original Matrix:")
print(M)

Q8: A data frame is a table or a two-dimensional array-like structure in


which each column contains values of one variable and each row contains one
set of values from each column.

Following are the characteristics of a data frame.

The column names should be non-empty.


The row names should be unique.
The data stored in a data frame can be of numeric, factor or character
type.
Each column should contain same number of data items.

Sol:
# Create the data frame.
emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),

start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-


11",
"2015-03-27")),
stringsAsFactors = FALSE
)
# Print the data frame.
print(emp.data)
Q9: Get the structure of the dataframe created in the Question8.

Sol:
str(emp.data)

Q10: Write a R program to call the (built-in) dataset airquality. Remove the
variables 'Solar.R' and 'Wind' and display the data frame.

Sol:
data = airquality
print("Original data: Daily air quality measurements in New York, May to
September 1973.")
print(data)
data[,c("Solar.R")]=NULL
data[,c("Wind")]=NULL
print("data.frame after removing 'Solar.R' and 'Wind' variables:")
print(data)

Q10: Load the dataset mtcars into a variable. see the column names. display
the first 6 rows and last 6 rows of the cars (use head and tail)

Sol:
cars <-mtcars
cars
names(mtcars)
head(cars) # First 6 rows of dataset
tail(cars) # last 6 rows
head(cars, n=10)# First 10 rows of dataset cars
# check the following commans and see the output and explain
cars[1:10, ] # First 10 rows
cars[1:10,1 ] # First 10 rows
cars[1:10,1:3 ] # First 10 rows
edit(cars)

Q11:load the data set mtcars into a variable usercars and store that info
into a csv file named mba.csv

SOl:
usercars<-mtcars
usercars
write.csv(usercars, "mba.csv")

You might also like