In the previous set we started with arithmetic operations on vectors. We’ll take this a step further now, by practising functions to summarize, sort and round the elements of a vector. Sofar, the functions we have practised (log, sqrt, exp, sin, cos, and acos) always return a vector with the same length as the input […]
vectors
Working With Vectors
In the previous exercise set we practised vectors as a data structure. As I noted at the beginning of that set, perhaps you were already familiar with data in a vector-like structure in other applications such as Microsoft Excel or SPSS. If so, perhaps you also used those data to carry out calculations. In this […]
Creating vectors
A vector is the most elementary way to store and structure data in R. For now, think of it as a list of numbers, which can be as short as a single number, or as long as about 2 billion(!) numbers. Perhaps you were used to working with lists of numbers already in a spreadsheet […]
How to create your first vector in R
Are you an expert R programmer? If so, this is *not* for you. This is a short tutorial for R novices, explaining vectors, a basic R data structure. Here’s an example: 10 150 30 45 20.3 And here’s another one: -5 -4 -3 -2 -1 0 1 2 3 still another one: “Darth Vader” “Luke […]
Vectors Vol. 2 Exercises
[For this exercise, first write down your answer, without using R. Then, check your answer using R.] Answers to the exercises are available here. Exercise 1 Consider two vectors, x, y x=c(4,6,5,7,10,9,4,15) y=c(0,10,1,8,2,3,4,1) What is the value of: x*y Exercise 2 Consider two vectors, a, b a=c(1,2,4,5,6) b=c(3,2,4,1,9) What is the value of: cbind(a,b) Exercise […]
Vector exercises
A vector is a simple data structure in R. You will use it frequently, often as a building block of more complex data structures and operations on those structures. Before proceeding, please follow our short tutorial and review Chapter 2 of An Introduction to R. First, write down your answer, without using R and without […]