Harold Nelson
2023-09-04
A data frame is a list of vectors of equal length. Each vector becomes a column, and each element within the vector is a row entry for that column.
You can create a data frame using the data.frame() function.
## name age
## 1 Alice 25
## 2 Bob 30
## name age
## 1 Alice 25
## 2 Bob 30
## c..Alice....Bob.. c.25..30.
## 1 Alice 25
## 2 Bob 30
## name age
## 1 Alice 25
## 2 Bob 30
Create a data frame named students that contains the following columns: ID, Name, and Age. Fill it with the following data:
ID = c(101, 102, 103)
Name = c("John", "Jane", "Jack")
Age = c(21, 23, 20)
students = data.frame(ID,Name,Age)
students
## ID Name Age
## 1 101 John 21
## 2 102 Jane 23
## 3 103 Jack 20
Use the str() function to see the structure of your dataframe.
## 'data.frame': 3 obs. of 3 variables:
## $ ID : num 101 102 103
## $ Name: chr "John" "Jane" "Jack"
## $ Age : num 21 23 20
Use the $ and [[ ]] operators to obtain the students’ names as a separate vector. Verify that these produce the same result.
## [1] "John" "Jane" "Jack"
## [1] "John" "Jane" "Jack"
Change the first name in names1 to Joe. Verify that the names in the dataframe and names2 have not been changed.
## [1] "Joe" "Jane" "Jack"
## [1] "John" "Jane" "Jack"
## [1] "John" "Jane" "Jack"
Create a new datframe students2 in the same format. The ID values are 201, 202, and 203. The names are Tom, Dick, and Harry. The ages are 21, 22, and 23. Then use the rbind() function to add students2 to students.
ID = c(201,202,203)
Name = c("Tom","Dick","Harry")
Age = c(21,22,23)
students2 = data.frame(ID,Name,Age)
students2
## ID Name Age
## 1 201 Tom 21
## 2 202 Dick 22
## 3 203 Harry 23
## ID Name Age
## 1 101 John 21
## 2 102 Jane 23
## 3 103 Jack 20
## 4 201 Tom 21
## 5 202 Dick 22
## 6 203 Harry 23
Create the vector Major with values CS,IT,ME,CE,ME,CS. Add it to the datframe using the cbind() function making students3. Add it using the $ operator making students4. Verify that students3 and students4 are identical.
## ID Name Age Major
## 1 101 John 21 CS
## 2 102 Jane 23 IT
## 3 103 Jack 20 ME
## 4 201 Tom 21 CE
## 5 202 Dick 22 ME
## 6 203 Harry 23 CS
## ID Name Age Major
## 1 101 John 21 CS
## 2 102 Jane 23 IT
## 3 103 Jack 20 ME
## 4 201 Tom 21 CE
## 5 202 Dick 22 ME
## 6 203 Harry 23 CS
Create a new dataframe Mechanical_Engineers as a subset of students3 where the value of Major is “ME”. Use the [] operator.