#Assigning the function data.frame to the object called mydata.
mydata <- data.frame("ID" = c(1, 2, 3, 4, 5, 6),
"Age" = c(20, 25, 21, 18, 24, 23),
"Gender" = c(1, 2, 2, 1, 1, 2))
print(mydata) #Showing the data set
## ID Age Gender
## 1 1 20 1
## 2 2 25 2
## 3 3 21 2
## 4 4 18 1
## 5 5 24 1
## 6 6 23 2
mydata2 <- mydata[-4 , c(1, 2)] #Excluding the 4th row and selecting only the 1st and the 2nd column.
print(mydata2)
## ID Age
## 1 1 20
## 2 2 25
## 3 3 21
## 5 5 24
## 6 6 23
mydata[1, 2] <- 22
print(mydata)
## ID Age Gender
## 1 1 22 1
## 2 2 25 2
## 3 3 21 2
## 4 4 18 1
## 5 5 24 1
## 6 6 23 2
mydata$Height <- c(180, 172, 172, 178, 182, 169) #Adding a new variable Height to exisiting data frame, named mydata.
print(mydata)
## ID Age Gender Height
## 1 1 22 1 180
## 2 2 25 2 172
## 3 3 21 2 172
## 4 4 18 1 178
## 5 5 24 1 182
## 6 6 23 2 169
#Creating new variable, named GenderF, which is factor (categorical variables) with categories 1 and 2 where 1 means Male and 2 Female.
mydata$GenderF <- factor(mydata$Gender,
levels = c(1, 2),
labels = c("M", "F"))
print(mydata)
## ID Age Gender Height GenderF
## 1 1 22 1 180 M
## 2 2 25 2 172 F
## 3 3 21 2 172 F
## 4 4 18 1 178 M
## 5 5 24 1 182 M
## 6 6 23 2 169 F