3. Diverse functions


head(test) returns by default the first sixth entries in the vector “test”; head(test, 3) returns only the first three entries in the vector “test”. Check the example below:

[code language=”r”]
test <- c(4,7,9,6,89,45,3,5,78,23,45,0,2,12)
test

head(test)

head(test,  3)
[/code]

head()

 

tail(test) returns the last sixth entries in the vector “test”; tail(test, 3) returns only the last three entries in the vector “test”. Check the example below:

[code language=”r”]
test

tail(test)

tail(test, 3)
[/code]

tail()

length(test) return the number of entries in the vector “test”.

[code language=”r”]
test

length(test)
[/code]

length()

 

names(dataset) adds the label of your choice to each of the entries of the vector “dataset”, in the order mentioned in the command. Here, the vector “dataset” contains 4 entries (numbers) and we want to give them each their own label (or name); obviously lacking a sense of creativity, the labels that we choose here are “label A”, “label B”, “label C” and “label D”:

 

[code language=”r”]
dataset <- c(25,43,65,80)
dataset

names(dataset) <- c("label A", "label B", "label C", "label D")
dataset
[/code]

 

names()

An interesting point with this type of named vectors is that is become easy to retrieve any value in the vector by calling its name (or label). Use dataset["label C"] for example to retrieve the value(s) under “label C”:

[code language=”r”]
dataset["label C"]
[/code]

 

 

dataset[]

 

 

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