So far in this series, we used vectors from built-in datasets (rivers, women and nhtemp), or created them by stringing together several numbers with the c function (e.g. c(1, 2, 3, 4)). R offers an extremely useful shortcut to create vectors of the latter kind, which is the colon : operator. Instead of having to […]
Regular Sequences Solutions
Solution 1 Part a x <- 157:164 x ## [1] 157 158 159 160 161 162 163 164 Part b x <- c(15:18, 20:24) x ## [1] 15 16 17 18 20 21 22 23 24 Part c x <- 10:1 x ## [1] 10 9 8 7 6 5 4 3 2 1 Part […]
Vectors and Functions Solutions
Solution 1 v <- c(length(rivers), sum(rivers), mean(rivers), median(rivers), var(rivers), sd(rivers), min(rivers), max(rivers)) v ## [1] 141.0000 83357.0000 591.1844 425.0000 243908.4086 493.8708 ## [7] 135.0000 3710.0000 Back to exercise Solution 2 The trim=0.25 argument means that R will ignore 25 percent of the 8 observations (i.e., 2 observations) from both the lowest and highest end. So, […]
Vectors and Functions
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 […]
Working With Vectors Solutions
Solution 1 u <- 4 v <- 8 u + v ## [1] 12 u – v ## [1] -4 u * v ## [1] 32 u / v ## [1] 0.5 u^v ## [1] 65536 Back to exercise Solution 2 u <- c(4, 5, 6) v <- c(1, 2, 3) u + v ## […]
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 Solutions
Please find below the solutions for the first 5 exercises of our set Creating vectors. The solutions for exercise 6, 7 and 8 are available in our eBook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences. Solution 1 100 ## [1] 100 Back to exercise Solution 2 c(2, 4, 6, 8, […]
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 […]
Celebrating our 100th R exercise set
Yesterday we published our 100th set of exercises on R-exercises. Kudos and many thanks to Avi, Maria Elisa, Euthymios, Francisco, Imtiaz, John, Karolis, Mary Anne, Matteo, Miodrag, Paritosh, Sammy, Siva, Vasileios, and Walter for contributing so much great material to practice R programming! Even more thanks to Onno, who is working (largely) behind the scenes […]