Go back to the main page
Go back to the R overview page


This file can be downloaded here

R Basics

  1. Create a vector with elements 1, 2, 3. Assign to variable vec1.
#<YOUR CODE HERE>
  1. Calculate the length of the vector.
#<YOUR CODE HERE>
  1. What class type is the vector?
#<YOUR CODE HERE>
  1. Calculate the mean.
#<YOUR CODE HERE>
  1. Calculate the median.
#<YOUR CODE HERE>
  1. Calculate the standard deviation.
#<YOUR CODE HERE>
  1. Create a vector with elements 1 to 10. Use the : notation. Assign to variable vec2.
#<YOUR CODE HERE>
  1. Create a sequence of 1 to 10. Only odd numbers. Assign to variable vec3.
#<YOUR CODE HERE>
  1. create a vector from vector a repeated 10 times. Assign to vec4
#<YOUR CODE HERE>
  1. Use the paste function to concatenate strings hello and world.
#<YOUR CODE HERE>
  1. Use the paste0 function to concatenate strings hello and world.
#<YOUR CODE HERE>
  1. Print all letters of the alphabet.
#<YOUR CODE HERE>
  1. Print all letters of the alphabet in capitals.
#<YOUR CODE HERE>
  1. Assign all letters of the alphabet to vec5.
#<YOUR CODE HERE>
  1. Use toupper function to convert letters to capitals.
#<YOUR CODE HERE>
  1. Select first element of vec5.
#<YOUR CODE HERE>
  1. Select all but the first element of vec5.
#<YOUR CODE HERE>
  1. Print last 6 elements of vec5.
#<YOUR CODE HERE>
  1. Print last element of vec5.
#<YOUR CODE HERE>
  1. Create a factor “G”, “V”, “O”, “V”, “G”. Add levels and order. Assign to fac1.
#<YOUR CODE HERE>
  1. Print all elements larger than “V” from fac1.
#<YOUR CODE HERE>
  1. Create vector 1, 2, 3. Assign to vec6. Add 1 to each element.
#<YOUR CODE HERE>
  1. Create vector one, 2, 3. Assign to vec7. What class is this vector?
#<YOUR CODE HERE>
  1. Create a data frame df1 of two columns: 1, 2, 3 and 4, 5, 6.
#<YOUR CODE HERE>
  1. Add column names “col1” and “col2”.
#<YOUR CODE HERE>
  1. Add column col3 that consist of vector 7, 8, 9.
#<YOUR CODE HERE>
  1. Select the second column by index.
#<YOUR CODE HERE>
  1. Select the second column by name.
#<YOUR CODE HERE>
  1. Select the first column by index in [, ] notation.
#<YOUR CODE HERE>
  1. Select the third column by index in [, ] notation.
#<YOUR CODE HERE>
  1. Create df2 with columns col4, col5 and col6. Assign to vectors c(10, 11, 12), c(13, 14, 15), c(16, 17, 18).
#<YOUR CODE HERE>
  1. Sum the data frames df1 and df2.
#<YOUR CODE HERE>
  1. Add 1 to all values of col3.
#<YOUR CODE HERE>
  1. Print col3.
#<YOUR CODE HERE>
  1. Add 1 to all values of col3 and save to df3.
#<YOUR CODE HERE>
  1. Print the build in data frame iris.
#<YOUR CODE HERE>
  1. Get help on data frame iris.
#<YOUR CODE HERE>
  1. Print the Petal Length by index.
#<YOUR CODE HERE>
  1. Print the second row from the third column.
#<YOUR CODE HERE>
  1. Print the Petal Length by column name.
#<YOUR CODE HERE>
  1. Print the second item from the column Petal Length.
#<YOUR CODE HERE>
  1. Print the max Petal Length.
#<YOUR CODE HERE>
  1. Print a logical vector for all the values in Petal Length equal to the max value.
#<YOUR CODE HERE>
  1. Print the row in iris corresponding to the max Petal Length.
#<YOUR CODE HERE>
  1. Print summary statistics of iris.
#<YOUR CODE HERE>
  1. Print summary statistics of the Petal Length column in iris.
#<YOUR CODE HERE>
  1. Print all the rows equal to species virginica in iris.
#<YOUR CODE HERE>
  1. Order the data frame based on Petal Length. Order ascending.
#<YOUR CODE HERE>
  1. Order the data frame based on Petal Length. Order descending.
#<YOUR CODE HERE>
  1. Create XY-scatter of vectors: x = c(1, 2, 3), y = c(4, 5, 6)
#<YOUR CODE HERE>
  1. Create XY-scatter of iris Sepal Length against Petal With.
#<YOUR CODE HERE>
  1. Again but with title and axis labels. Use whatever text you like.Blue dots.
#<YOUR CODE HERE>
  1. Create histogram of iris Sepal Lenght.
#<YOUR CODE HERE>
  1. Again but with breaks of 0.25
#<YOUR CODE HERE>
  1. Create boxplot of iris Sepal Length and iris Sepal Width. Give labels (names) “sepal length” and “sepal width”.
#<YOUR CODE HERE>
  1. Create XY-scatter with y = c(1, 2, 3) and x = c(4, 5, 6) using the formula notation.
#<YOUR CODE HERE>
  1. Create a barplot of vector c(1, 2, 3).
#<YOUR CODE HERE>
  1. Create a barplot of vector c(1, 2, 3). Labels (names) = a, b, c.
#<YOUR CODE HERE>
  1. Print the build in data frame ChickWeight.
#<YOUR CODE HERE>
  1. Create XY-scatter of weight plotted against Time from data frame ChickWeight. Use formula notation.
#<YOUR CODE HERE>
  1. Create a linair regression model. Print model to screen.
#<YOUR CODE HERE>
  1. Create XY-scatter of weight plotted against Time from data frame ChickWeight. Use formula notation. Display the regression line.
#<YOUR CODE HERE>
  1. Plot the iris Sepal Length against Sepal Width in blue. Also add a second series to the plot.this should be Petal Length against Petal Width in red.
#<YOUR CODE HERE>
  1. Create a line plot of x = c(1, 2, 3) and y = c(4, 5, 6) in blue.Add second series x = c(1, 2, 3) and y = c(5, 5.5, 6) in red.
#<YOUR CODE HERE>

The end…


Go back to the main page
Go back to the R overview page
⬆️ Back to Top


This web page is distributed under the terms of the Creative Commons Attribution License which permits unrestricted use, distribution, and reproduction in any medium, provided the original author and source are credited. Creative Commons License: CC BY-SA 4.0.