Type your student name and ID

Name and ID: Vanessa Situ 702746674


Task 1: Calculations in R
Task 1.1: Simple calculations in R
# Your code goes here 
7+4 
## [1] 11
(3*8)/(2*3)
## [1] 4
Task 1.2: Using the print() method
# Your code goes here
print(7+4)
## [1] 11
print((3*8)/(2*3))
## [1] 4
Task 1.3: Using different R built-in methods/functions
# Your code goes here
log(12)
## [1] 2.484907
log(12, base=10)
## [1] 1.079181
sqrt(121)
## [1] 11
pi
## [1] 3.141593
Task 2: Assigning values in R
# Your code goes here
x = 7
y = 4
x+y
## [1] 11
z<- 3
k<-4
print (z+k)
## [1] 7
Task 3: Creating Vectors in R
# Your code goes here
a <- c(1, 2, 5, -4, -6,-7)
b <- c(TRUE,FALSE)
c <- c("one", "two", "three")
d <- c(1 + 2*4, 3-6*7)

a
## [1]  1  2  5 -4 -6 -7
b
## [1]  TRUE FALSE
c
## [1] "one"   "two"   "three"
d
## [1]   9 -39
Task 4: Using the class() method
# Your code goes here
a <- c(1, 2, 5, -4, -6,-7)
b <- c(TRUE,FALSE)
c <- c("one","two","three")

a
## [1]  1  2  5 -4 -6 -7
b
## [1]  TRUE FALSE
c
## [1] "one"   "two"   "three"
class(a)
## [1] "numeric"
class(b)
## [1] "logical"
class(c)
## [1] "character"
Task 5: Using the vector() method
# Your code goes here
Y<-vector("numeric",length = 1)
Y
## [1] 0
class(Y)
## [1] "numeric"
Task 6: Using the as.integer() method
# Your code goes here
d<-as.integer(3)
d
## [1] 3
class(d)
## [1] "integer"
Task 7: Creating a vector of 6 integers
# Your code goes here
a<-0:5
a
## [1] 0 1 2 3 4 5
class(a)
## [1] "integer"
Task 8: Using the as. command to change the vector type
# Your code goes here
a <- c(2/5)
class(a)
## [1] "numeric"
a
## [1] 0.4
b = as.character(a)
class(b)
## [1] "character"
b
## [1] "0.4"
Task 9: Creating a data frame with two columns
# Your code goes here
df <- data.frame(name =c("ash","paul","jane","mark"),grade=c(88,93,78,65))
df
##   name grade
## 1  ash    88
## 2 paul    93
## 3 jane    78
## 4 mark    65
Task 10: Using the str(), ncol() and nrow() for a data frame
# Your code goes here
df <- data.frame(name =c("ash","paul","jane","mark"),grade=c(88,93,78,65))

str(df)
## 'data.frame':    4 obs. of  2 variables:
##  $ name : chr  "ash" "paul" "jane" "mark"
##  $ grade: num  88 93 78 65
ncol(df)
## [1] 2
nrow(df)
## [1] 4
Task 11: Creating a data frame from different vectors
# Your code goes here
ID<-c(1,2,3,4)
month<-c("Jan","Feb","March","Apr")
sales<-c(15000,20000,125000,40000)
region<-c("east","west","south","north")
Sales_Data<-data.frame(ID,month,sales,region)
Sales_Data
##   ID month  sales region
## 1  1   Jan  15000   east
## 2  2   Feb  20000   west
## 3  3 March 125000  south
## 4  4   Apr  40000  north
Task 12: Accessing a specific column in a data frame
# Your code goes here
ID<-c(1,2,3,4)
month<-c("Jan","Feb","March","Apr")
sales<c(1500,20000,125000,40000)
## [1] FALSE FALSE FALSE FALSE
region<-c("east","west","south","north")
Sales_Data<-data.frame(ID,month,sales,region)
Sales_Data[2] 
##   month
## 1   Jan
## 2   Feb
## 3 March
## 4   Apr
Task 13: Accessing specific column values in a data frame
# Your code goes here
ID<-c(1,2,3,4)
month<-c("Jan","Feb","March","Apr")
sales<-c(1500,20000,125000,40000)
region<-c("east","west","south","north")
Sales_Data<-data.frame(ID,month,sales,region)
Sales_Data[,2] 
## [1] "Jan"   "Feb"   "March" "Apr"
Task 14: Accessing specific row in a data frame
# Your code goes here
ID<-c(1,2,3,4)
month<-c("Jan","Feb","March","Apr")
sales<-c(15000,20000,125000,40000)
region<-c("east","west","south","north")
Sales_Data[2,]
##   ID month sales region
## 2  2   Feb 20000   west
Task 15: Accessing specific columns in a data frame
# Your code goes here
ID<-c(1,2,3,4)
month<-c("Jan","Feb","March","Apr")
sales<-c(15000,20000,125000,40000)
region<-c("east","west","south","north")
Sales_Data<-data.frame(ID,month,sales,region)
Sales_Data[c("month","region")]
##   month region
## 1   Jan   east
## 2   Feb   west
## 3 March  south
## 4   Apr  north
Task 16: Accessing specific column values in a data frame using the $
# Your code goes here
ID<-c(1,2,3,4)
month<-c("Jan","Feb","March","Apr")
sales<-c(15000,20000,125000,40000)
region<-c("east","west","south","north")
Sales_Data<-data.frame(ID,month,sales,region)
Sales_Data$sales
## [1]  15000  20000 125000  40000
Task 17: Accessing specific column values in a data frame
# Your code goes here
ID<-c(1,2,3,4)
month<-c("Jan","Feb","March","Apr")
sales<-c(15000,20000,125000,40000)
region<-c("east","west","south","north")
Sales_Data<-data.frame(ID,month,sales,region)
Sales_Data[1:2]
##   ID month
## 1  1   Jan
## 2  2   Feb
## 3  3 March
## 4  4   Apr
Task 18: Accessing specific values in a data frame
# Your code goes here
ID<-c(1,2,3,4)
month<-c("Jan","Feb","March","Apr")
sales<-c(15000,20000,125000,40000)
region<-c("east","west","south","north")
Sales_Data<-data.frame(ID,month,sales,region)
Sales_Data[1,2]
## [1] "Jan"
Sales_Data[2,3]
## [1] 20000
Sales_Data[3,4]
## [1] "south"
Task 19: Adding a factor to a data frame
# Your code goes here
ID<-c(1,2,3,4)
month<-c("Jan","Feb","March","Apr")
sales<-c(15000,20000,125000,40000)
region<-c("east","west","south","north")
performance<-c("Poor","Poor","Excellent","Average")
performance<-factor(performance,order=TRUE)
Sales_Data<-data.frame(ID,month,sales,region,performance)
Sales_Data
##   ID month  sales region performance
## 1  1   Jan  15000   east        Poor
## 2  2   Feb  20000   west        Poor
## 3  3 March 125000  south   Excellent
## 4  4   Apr  40000  north     Average
Task 20: Converting a factor to an integer in a data frame
# Your code goes here
ID<-c(1,2,3,4)
month<-c("Jan","Feb","March","Apr")
sales<-c(15000,20000,125000,40000)
region<-c("east","west","south","north")
performance<-c("Poor","Poor","Excellent","Average")
performance<-factor(performance,order=TRUE)
Sales_Data<-data.frame(ID,month,sales,region,performance)
str(Sales_Data)
## 'data.frame':    4 obs. of  5 variables:
##  $ ID         : num  1 2 3 4
##  $ month      : chr  "Jan" "Feb" "March" "Apr"
##  $ sales      : num  15000 20000 125000 40000
##  $ region     : chr  "east" "west" "south" "north"
##  $ performance: Ord.factor w/ 3 levels "Average"<"Excellent"<..: 3 3 2 1
x=as.integer(performance)
x
## [1] 3 3 2 1