Copy and paste the following code to your R Studio platform or R version:

# Creating a list

name = “Harvey”
student.id = “1992”
subjects = c(“English”, “Law”, “Mathematics”, “Computer Science”)
marks = c(70, 95, 85, 90)

name
student.id
subjects
marks

student.info <- list(name, student.id, subjects, marks)
student.info

# Creating a smaller list
student.info2 <- list(c(“Alan”, “Denny”), 28)
student.info2

# It is a good habit to name the elements of a list
names(student.info) <- c(“Name”, “StudentID”, “Subjects”, “Marks”)
student.info

# Accessing the elements of a list
student.info[3]
student.info$Subjects
student.info$Name

student.info[[3]][2]
student.info[[3]][4]

# Creating a new element in the list
student.info[5] <- “83.75%”
names(student.info)[5] <- “Percentage”
student.info

# Removing an element
student.info$Percentage <- NULL
student.info

# Merging lists
print(student.info)
print(student.info2)

merged.list <- c(student.info, student.info2)
merged.list

names(merged.list)[5:6] <- c(“OldNames”, “OldMark”)
merged.list

# Data Frames

Names <- c(“Harvey”, “Alan”, “James”)
Age <- c(22, 23, 24)
Height <- c(1.70, 1.75, 1.80)

D.frame <- data.frame(Names, Age, Height)
D.frame

D.frame$Salary <- c(5000, 6000, 8000)
D.frame
D.frame$Names

# Loading the library to obtain the Boston data set
library(MASS)
head(Boston)
dim(Boston)
names(Boston)
str(Boston)

head(Boston[[1]])
head(Boston[[“crim”]])

# Observe the difference in the output

head(Boston[1])
head(Boston[“crim”])

Posted by Dimitris

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s