Introduction to R

6. Creating new variables

Variables inside a dataframe are accessed in the format <dataframe>$<variable>.  If we wished to calculate the BMI for all 205 subjects in the dataframe, we can follow the same procedure as above, but by creating a new column in the data frame, rather than a new object:

data$bmi <- data$weight/(data$height/100)^2   #  note correct height to m from cm

head(data)

 

Note that here I used the # symbol to insert a comment clarifying what was done, and that importantly I did correct the height to metres from centimetres.