Think of an integer, double it, add six, divide it in half, subtract the number you started with, and then square it. If your code is correct, the answer should be nine.
Object names have rules
Names can be a combination of letters, digits, periods . and underscores _.
Names can not include white spaces.
If a name starts with a period ., it can not be followed by a digit.
Names can not start with a number or an underscore _.
Object names have rules
Names are case-sensitive (age, Age and AGE are three different objects).
Reserved words (TRUE, FALSE, NULL, if, …) can not be used as names
Tips for naming objects
Avoid giving your object the same name as a built-in function.
To separate words, use an underscore (my_object) or a dot (my.object), or capitalize the different words (MyObject). Choose your favorite way, but be consistent with it.
Use names that illustrate what you want to do with the objects.
Exercise
Write a function that can simulate the roll of two six-sided dice, one red and one blue, an arbitrary number of times. This function should return a vector with the values of the red die that were strictly larger than the corresponding values of the blue die.
Exercise step by step (part 1)
Step 1: define a function that takes one argument, num_rolls, representing the number of times to roll the dice.
Step 2: create two objects called red and blue to store the results from the dice rolls.
Step 3: simulate the dice rolls using function sample() (read its help page if you need to).
Exercise step by step (part 2)
Step 4: create a vector of indices that identifies the values in the red die that were larger than the values in the blue die.
Step 5: use this vector of indices to extract the values from the red die.
Step 6: make sure that your function returns the values you extracted in step 5.
Coercion
When adding different data types to the same atomic vector, R follows specific rules to coerce everything to be of the same type.
If a character string is present in an atomic vector, R will convert all other values to character strings.
If a vector only contains logicals and numbers, R will convert the logicals to numbers; every TRUE becomes a 1, and every FALSE becomes a 0.
NAs are never coerced automatically.
Make a histogram
Use the example I showed to you to make a histogram for variable height. Bonus: can you color the bars?
Use the example I showed to you to make a histogram for variable leaf_area. Bonus: can you color the box?
Here is the code for the boxplot.
boxplot( flower_df$leaf_area, ylab ="Leaf area", col ="blue",main ="Most leaf areas are between 11 and 18")
Make a scatterplot
Use the example I showed to you to make a scatter plot with height and weight, coloring by nitrogen level. Remember to add a legend to the plot. Your scatter plot should resemble this one:
Here is the code for the scatter plot.
plot(x = flower_df$weight,y = flower_df$height, col = flower_df$nitrogen,main ="No clear association between height and weight",xlab ="Weight",ylab ="Height")# Add a legend to the plotlegend(x ="bottomright", legend =levels(flower_df$nitrogen), col =1:length(levels(flower_df$nitrogen)), pch =16)
Make a mosaic plot
Use the example I showed to you to make a mosaic plot to visualize how frequently the values of nitrogen and treat combine with each other, but only for flowers with a weight below 10. Your plot should resemble this:
Here is the code for the mosaic plot
nitrogen_by_treat_table =xtabs(formula =~ nitrogen + treat,data = flower_df[which(flower_df$weight <10),])mosaicplot(nitrogen_by_treat_table, main ="Nitrogen by treat, weight below 10")