7 Useful functions
Here we will learn a few more useful functions in R that will useful to us further along.
7.1 Sequences
```
# seq() function generates a sequence of numbers.
seq(from=1, to=9)
# We can ommit the from and to
seq(1,9)
# We can define the step, increment of sequences(Default is 1)
seq(1,9, by=2)
# Steps can by any amount
seq(0,9, by=1.5)
# We can also use length parameter instead, which will equally split our sequence.
seq(from=1, to=10, length.out=19)
# We can achieve a simpler version of seq with the operator ':'
0:9
```
# seq() function generates a sequence of numbers.
seq(from=1, to=9)
# We can ommit the from and to
seq(1,9)
# We can define the step, increment of sequences(Default is 1)
seq(1,9, by=2)
# Steps can by any amount
seq(0,9, by=1.5)
# We can also use length parameter instead, which will equally split our sequence.
seq(from=1, to=10, length.out=19)
# We can achieve a simpler version of seq with the operator ':'
0:9
7.2 Repetitions
R also allows you to easily create vectors containing repetitions with the rep() function.
```
# rep() function replicates the values in x.
rep(c("Isn't it time for a break?"), times=2)
# We can use to create repetitions of numbers as well
rep(5, times=3)
# Or of sequences of numbers
rep(1:5, times=2)
# We can also use "each" to ask that each element is repeated "each" times.
rep(1:5, each=2)
# What would this give?
rep(rep(4:-5, 5), 2)
# Or this? Try to press "Enter" after each parenthesis and comma so you can isolate each part of the code and better understand it
rep(rep(seq(1, 10, length.out=25), 2), 3)
```
# rep() function replicates the values in x.
rep(c("Isn't it time for a break?"), times=2)
# We can use to create repetitions of numbers as well
rep(5, times=3)
# Or of sequences of numbers
rep(1:5, times=2)
# We can also use "each" to ask that each element is repeated "each" times.
rep(1:5, each=2)
# What would this give?
rep(rep(4:-5, 5), 2)
# Or this? Try to press "Enter" after each parenthesis and comma so you can isolate each part of the code and better understand it
rep(rep(seq(1, 10, length.out=25), 2), 3)
7.3 Factors
Factors are variables in R which take on a limited number of different values, such variables are often referred to as categorical variables. For example in cross-national research, “Gender” is usually a variable that can either take “Male” or “Female” values. In R terms, the factor would be called Gender, and it would have two levels, “Male” or “Female”.
Above we learned we can do this by rep(c("Male", "Female"), each=5)
. Now we must save into an above called Gender
:
## [1] Male Male Male Male Male Female Female Female Female Female
## Levels: Female Male
Alternatively, we can use the gl() function, which generates factors by specifying the pattern of their levels. Where:
gl(n, k, length = n*k, labels = 1:n, ordered = FALSE)
- n: number of levels
- k: number of replications
- length: length of the result. By default: length = n*k
- labels: labels for the resulting factor levels
- ordered: whether the result should be ordered or not
## [1] Male Male Male Male Male Female Female Female Female Female
## Levels: Male Female
Let’s try creating a factor of 3 colors
```
gl(n=3, k=1, labels = c("Brown", "Red", "Green"))
# Now let's use the "k" argument within the gl function
# to specify the number of replications of each factor level
gl(n=3, k=2, length=9, labels = c("Brown", "Red", "Green"))
# Here we see Recycling because we used the "length" argument
# which gives the length of the wanted result
gl(n=3, k=1, length=12, labels = c("Brown", "Red", "Green"))
# We can use rep() and gl() together to achieve a repetion
# of all factors levels at once, instead of repeating each level
rep(gl(n=4, k=1, labels = c("Brown", "Red", "Green", "Yellow")), 3)
```
gl(n=3, k=1, labels = c("Brown", "Red", "Green"))
# Now let's use the "k" argument within the gl function
# to specify the number of replications of each factor level
gl(n=3, k=2, length=9, labels = c("Brown", "Red", "Green"))
# Here we see Recycling because we used the "length" argument
# which gives the length of the wanted result
gl(n=3, k=1, length=12, labels = c("Brown", "Red", "Green"))
# We can use rep() and gl() together to achieve a repetion
# of all factors levels at once, instead of repeating each level
rep(gl(n=4, k=1, labels = c("Brown", "Red", "Green", "Yellow")), 3)