Overview

The main purpose of this task is to check that you can get it to load and run and knit it to html. Once that’s working, have a go at the embedded exercises. Exercise 7 is just bonus. At the end, knit it to html and upload both files (Rmd and html) on blackboard (not zipped).

R and the RStudio IDE

R is a free software environment for statistical computing and graphics. We will use the RStudio IDE (Integrated Development Environment). This has a number of nice features which we’ll cover as we go through - identify the following elements:

Exercise 1a

Create variables for the following information: name (character): Your name age (numeric): Your age is_local (logical): Do you live in Leicester? (TRUE/FALSE)

name <- "Vamshi"
age <- 22
is_local <- TRUE

Exercise 1b

Is it possible to create a vector containing the values for your three variables? If not, create a named list containing the values for your three variables and print it.

 v <- c(name, age, is_local)
 x <- list(name, age, is_local)
 x
## [[1]]
## [1] "Vamshi"
## 
## [[2]]
## [1] 22
## 
## [[3]]
## [1] TRUE

Exercise 2

Create a data frame called “students” with the following columns: Name (character): Names of students (include your name and at least three more names) Age (numeric): Ages of students Grade (factor): Grades of students (with levels: A, B, C) Is_Local (logical): Whether each student lives in Leicester (TRUE/FALSE)

 students <- data.frame(
 Name = c("Vamshi","Niheel","Chandrika", "Harini"),
 Age = c(22, 24, 20, 21),
 Grade = factor(c("A", "B", "C", "A"), levels = c("A", "B", "C")),
 Is_Local = c(TRUE, FALSE, FALSE, TRUE)
 )
 students
##        Name Age Grade Is_Local
## 1    Vamshi  22     A     TRUE
## 2    Niheel  24     B    FALSE
## 3 Chandrika  20     C    FALSE
## 4    Harini  21     A     TRUE

Exercise 3

Let R show how many times the different levels appear in the grade-column

 table(students$Grade)
## 
## A B C 
## 2 1 1

Exercise 4

Extract and print the Age vector from the students data frame.

students$Age
## [1] 22 24 20 21

Exercise 5

Look up the function “mean()” with the help function. Calculate the average age of the students from your dataframe and print it out. Use the pipe operator to do this calculation in one line of code.

 students$Age |> mean() |> round(digits = 2)
## [1] 21.75

Exercise 6

Why does the following code not run? Fix the Code (in the code chunk below named r “Exercise 6, solution”). Note, before you run the code, you need to get rid of the “#”s. You can do this by selecting everything and using the shortcut ‘Ctrl Shift C’.” (Commenting it out again works the same way). # Create the data frame correctly df <- data.frame( Name = c(“Sai”, “Mani”, “Venkat”), Age = c(24, 31, 29) # added 3rd age for Venkat )

Add 10 years to each student’s age

age_in_10_years <- df$Age + 10

Calculate the mean age in 10 years

mean(age_in_10_years)