You are on page 1of 4

PROG: 3 USING DATA FRAMES IN R

AIM:
To write a program to create and perform data frames operations in R.
ALGORITHM:
1. Initialize the variables emp.data and using the function data.frame() input the
values as a list.
2. By using c() input the list of values into the frame.
3. Using the variable final extract the data and print it.
4. Use rbind() to add a row in the data frame
5. Use cbind() to add a column in data frame.
6. The inputs for rbind() and cbind() are given as list.
7. Initialize column to NULL, to delete the column from the data frame.
8. Using print() we can print the output.
SOURCE CODE:

# Creating the data frame.


emp.data<- data.frame(
employee_id = c (1:5),
employee_name = c("Shubham","Arpita","Nishka","Gunjan","Sumit"),
sal = c(623.3,915.2,611.0,729.0,843.25), starting_date = as.Date(c("2012-01-
01", "2013-09-23", "2014-11-15", "2014-05-11", "2015-03-27")),
stringsAsFactors = FALSE
)
# Printing the data frame.
print(emp.data)

# Extracting specific columns from a data frame


final <- data.frame(emp.data$employee_id,emp.data$sal)
print(final)

# Extracting first row from a data frame


final <- emp.data[1,]
print(final)

# Extracting last two row from a data frame


final <- emp.data[4:5,]
print(final)
# Extracting 2nd and 3rd row corresponding to the 1st and 4th column
final <- emp.data[c(2,3),c(1,4)]
print(final)

#Adding row in the data frame


x <- list(6,"Vaishali",547,"2015-09-01")
rbind(emp.data,x)

#Adding column in the data frame


y <- c("Moradabad","Lucknow","Etah","Sambhal","Khurja")
cbind(emp.data,Address=y)

#Delete column from the data frame


emp.data$starting_date<-NULL
print(emp.data)

#Printing the summary


print(summary(emp.data))

OUTPUT:

Create a data frame

Structure of data frame


Extract specific columns

Extracting first row

Extracting last two rows

Extracting 2nd and 3rd row corresponding to 1st and 4th column

Adding row in data frame


Adding column in data frame

Delete rows from data frame

Delete columns from data frame

Summary of data frame

RESULT:
Thus, the program is completed and the output is verified successfully.

You might also like