TODO: Naming a good variable allows the person to read through your code easily.
We’ll use the “palmerpenguins” packages (https://allisonhorst.github.io/palmerpenguins/) to address this question. You’ll need to install the package with install.packages(“palmerpenguins”) if you have not done so before, call library(““palmerpenguins”), and load the data with data(penguins)
#install.packages("palmerpenguins")
library(palmerpenguins)
## Warning: package 'palmerpenguins' was built under R version 4.1.2
data(penguins)
Using the variable data in “penguins”, we can take a look at good variable names. By using names() we can pull up all the variables in the data set and in this case, “penguins”.
names(penguins)
## [1] "species" "island" "bill_length_mm"
## [4] "bill_depth_mm" "flipper_length_mm" "body_mass_g"
## [7] "sex" "year"
As we can see, the variables are label specifically to understand the contents of their data. If we were to alter the title of “year” to “time” then people could question the specific time frame the data is set at. Other errors could be produced by not understanding the syntax behind R. When coding, there is specific rules which need to be followed or the program will not understand.
names(penguins_raw)
## [1] "studyName" "Sample Number" "Species"
## [4] "Region" "Island" "Stage"
## [7] "Individual ID" "Clutch Completion" "Date Egg"
## [10] "Culmen Length (mm)" "Culmen Depth (mm)" "Flipper Length (mm)"
## [13] "Body Mass (g)" "Sex" "Delta 15 N (o/oo)"
## [16] "Delta 13 C (o/oo)" "Comments"
When taking a look at “penguins_raw” we can see there are some errors. There contains spaces, or gaps, between words. Other unacceptable variable names would consist of starting variable names with punctuation, numbers, or specifically variables containing “@”.
For more information on this topic, see https://www.datamentor.io/r-programming/variable-constant/