Chapter 2 Names and values
Learning objectives:
- To be able to understand distinction between an object and its name
- With this knowledge, to be able write faster code using less memory
- To better understand R’s functional programming tools
Using lobstr package here.
Quiz
1. How do I create a new column called 3
that contains the sum of 1
and 2
?
df <- data.frame(runif(3), runif(3))
names(df) <- c(1, 2)
df
#> 1 2
#> 1 0.9036272 0.1449256
#> 2 0.5308647 0.2990537
#> 3 0.1661038 0.3955052
df$`3` <- df$`1` + df$`2`
df
#> 1 2 3
#> 1 0.9036272 0.1449256 1.0485527
#> 2 0.5308647 0.2990537 0.8299183
#> 3 0.1661038 0.3955052 0.5616090
What makes these names challenging?
You need to use backticks (`) when the name of an object doesn’t start with a a character or ‘.’ [or . followed by a number] (non-syntactic names).