2. Assigning data elements to a vector


In R, storing data under a specific name is called assignment. There are at least 4 ways to assign data in R:

a. with the operator =
b. with the operator <-
c. with the operator ->
d. with the function assign("name,...").

Here is a practical example where single values are assigned:

[code language=”r”]
data1 = 42
data2 <- 76
89 -> data3
assign("data4", 23)

data1
data2
data3
data4
[/code]

assignment

 

Here is a second example where the function concatenate, written c(...), is used to assign a series of data elements (here we choose and mix numbers, text and logical values) to a vector:

[code language=”r”]
data1 = c(42,43,45,48)
data2 <- c("aa","ab","bc","rt","ux")
c(89, "test", 9, "data") -> data3
assign("data4", c(TRUE, FALSE, TRUE, TRUE))

data1
data2
data3
data4
[/code]

 

concatenate

Note that in the case of data3, the numbers have been stored as text, due to the presence of text elements in the series of data elements to be concatenated. This tells you (or reminds you) that such vectors must contain data elements of the same type. If a mix is to be stored, some adjustments may be performed by R to maintain some form of “harmony”.

 

  Fant du det du lette etter? Did you find this helpful?
[Average: 5]