3.2 Adding new data to a data frame

The first approach is very straightforward. We will add the name of the new variable (age) after the data frame (e.g., affective.dis) followed by the $ symbol. Then, we will assign the set of values of the newly-created variable to the data frame.


age <- sample(18:30, 36, replace = T)
affective.dis$age <- age
head(affective.dis)
##   id treatment year    sex depression life.satis alone age
## 1  1         1 2020   male         22   53.50930     0  23
## 2  2         1 2020 female         19   42.10586     0  19
## 3  3         1 2020   male         14   22.73856     1  24
## 4  4         1 2020 female         23   18.42421     0  21
## 5  5         1 2020   male         16   68.70935     1  20
## 6  6         1 2020 female         11   40.22030     1  21

With the second approach, we can merge variables (i.e., columns) and observations (i.e., rows) using the functions cbind() and rbind(). The function cbind() binds columns (i.e., variables), whereas rbind() binds rows (i.e., observations, cases, participants).


y <- sample(0:20, 36, replace = T)
affective.dis <- cbind(affective.dis, y)
head(affective.dis)
##   id treatment year    sex depression life.satis alone age  y
## 1  1         1 2020   male         22   53.50930     0  23  0
## 2  2         1 2020 female         19   42.10586     0  19  4
## 3  3         1 2020   male         14   22.73856     1  24  2
## 4  4         1 2020 female         23   18.42421     0  21 11
## 5  5         1 2020   male         16   68.70935     1  20  2
## 6  6         1 2020 female         11   40.22030     1  21  0

To rearrange the order of the variables after adding new ones (e.g., placing age after sex), we can use indexing approaches to vectors.


affective.dis <- affective.dis[c(1:4, 8, 5:7, 9)]
head(affective.dis)
##   id treatment year    sex age depression life.satis alone  y
## 1  1         1 2020   male  23         22   53.50930     0  0
## 2  2         1 2020 female  19         19   42.10586     0  4
## 3  3         1 2020   male  24         14   22.73856     1  2
## 4  4         1 2020 female  21         23   18.42421     0 11
## 5  5         1 2020   male  20         16   68.70935     1  2
## 6  6         1 2020 female  21         11   40.22030     1  0