You are on page 1of 4

Week 4 Exercises

DrAPS
Dataframe
• 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.

ID Items Store Price


10 book TRUE 2.5
20 pen FALSE 8.0
30 textbook TRUE 10.0
40 pencil_case FALSE 7.0
Week 4 Exercises
b) Implement R Script to extract the data from dataframes
Output
# Creating a dataframe
df = data.frame( X.Student_Name Language Age
1 Narendra R 22
" Student_Name" = c("Narendra", "Raj", "Jyoti"),
2 Raj Python 25
"Language" = c("R", "Python", "Java"), 3 Jyoti Java 45
"Age" = c(22, 25, 45)
)
print(df)
X.Student_Name Language Age
cat("Accessing first and second row\n") 1 Narendra R 22
4 Raj Python 25
print(df[1:2, ])

# Accessing first and second column X.Student_Name Language


cat("Accessing first and second column\n") 1 Narendra R
2 Raj Python
print(df[, 1:2]) 3 Jyoti Java
Week 4 Exercises
# Selecting the subset of the data frame
Output
# age is greater than 30
newDf = subset(df, Age>30) X.Student_Name Language Age
cat("After Selecting the subset of the data frame\n") 3 Jyoti Java 45
print(newDf)

# Selecting the subset of the data frame


# where Name is equal to Amiya
# OR age is greater than 30

newDf = subset(df, Student_Name =="Raj"|Age>30) Student_Name Language Age


2 Raj Python 25
3 Jyoti Java 45
cat("After Selecting the subset of the data frame\n")
print(newDf)

You might also like