I. Names and values

Let’s learn about the fundamentals of names and values from Hadley Wickham, https://adv-r.hadley.nz/names-values.html

II. Three quetions

You are allowed to skip the chapter if you can answer the following three quizzes.

Quiz1

Given the following data frame, how do I create a new column called “3” that contains the sum of 1 and 2? You may only use $, not [[. What makes 1, 2, and 3 challenging as variable names?

df <- data.frame(runif(3), runif(3))
names(df) <- c(1, 2)
print(df)
##           1         2
## 1 0.6147790 0.6470389
## 2 0.2850090 0.4927846
## 3 0.4068468 0.4994707

My answer and explanatios

This is easy if the column names are alphabets. You’ll use deplyr in this way.

# import a library
library(dplyr)
# integers instead of doubles
df1 <- data.frame(round(runif(3, 0, 10)), round(runif(3, 0, 10)))
# a and b insted of 1 and 2
names(df1) <- c("a", "b")
df1 %>% mutate(c = a + b)
##   a b  c
## 1 8 2 10
## 2 4 5  9
## 3 3 2  5

However, you’ve got an error when the column names are numbers.

You can get the right table if you handle the column numbers as strings like the following way.

You can use 1

1df3$"1" and df3$"2"

# integers instead of doubles
df3 <- data.frame(round(runif(3, 0, 10)), round(runif(3, 0, 10)))
# 1 and 2 insted of a and b
names(df3) <- c(1, 2)
# add a column with the column name, "3"
df4 <- df3 %>% mutate("3" = df3$"1" + df3$"2")
print(df4)
##   1  2  3
## 1 4  6 10
## 2 5 10 15
## 3 3  5  8
# use ` ` instead of $" "
df5 <- df3 %>% mutate("3" = `1` + `2`)
print(df5)
##   1  2  3
## 1 4  6 10
## 2 5 10 15
## 3 3  5  8

To be continued.