# 1
<- read.csv("my_data_file.csv")
d
# 2
<- d %>% filter(id != "241269-1212")
d
# 3
<- d %>%
d mutate(s=factor(c("F", "M"))[as.numeric(substr(id,nchar(id),nchar(id))) %% 2 + 1 ])
Comments - what to
1 Meaningful comments
What comments would be relevant here?
Suggest comments for this code… think ‘what’ and ‘why’
Suggestions for meaningful comments
# ?? comments necessary ??
<- read.csv("my_data_file.csv")
d
# excluded as participant entered an invalid CPR number
<- d %>% filter(id != "2321369-1212")
d
# set 's' to F(emale) or M(ale) depending on odd/even id number in CPR
<- d %>%
d mutate(s=factor(c("M", "F"))[as.numeric(substr(id,nchar(id),nchar(id))) %% 2])
In relation to each of the three lines of code in the exercise above:
- it is unnecessary to comment this line – the code is self-explanatory
- it’s a good idea to comment line two to explain why the CPR number in question was deleted.
- it’s a good idea to comment line three to explain what the code does, as it is not self-explanatory!
The code in line 3 is a little complicated (on purpose), but basically, it creates a new variable s
which is either ‘M’ or ‘F’ for odd and even CPR numbers. If you want to understand the code:
If
id
is a text string representation of a CPR number, the code evaluates the last character in the string (substr(id,nchar(id),nchar(id))
), converts it into a numerical value (as.numeric
) and calculates the modulus 2 (%% 2
), i.e. the integer remainder of division by 2 (which becomes0
for even numbers and1
for odd numbers), it then adds 1 (+1
) and uses that numbers (which is now1
for even CPR numbers and2
for odd CPR numbers), and setss
to be index 1 or 2 of the factorfactor(c("F", "M"))
– this works just fine, but it is not self-explanatory, is it?
1.1 Commenting your code
Main points
- Always comment your code
- Comments should explain your thinking : ‘why’
- With good code the ‘how’ is self-evident
- ..but if your code can not be self-evident: explain in comments