![]() |
VOOZH | about |
A key-value pair can be explained as a set of two linked data items where a key uniquely identifies a value or a set of values in the data. Since a list can hold multiple data-types data, we can have a key-value pair stored in the list
We can assign variables to each key and value. Store each key-value pair after building the list using square brackets.
Output:
21 Pulkit
Another way of doing this without using any additional variables is to specify the key and the value in the list() function while creating the list.
Example
Output:
21 Male
Another approach we can take to add a key-value pair in the list is to setNames() function and inside it, we use as.list(). Basically what we will have here is a syntax like given below, which will create a list and assign all the keys with their respective values.
Syntax:
variable<-setNames(as.list(values), keys)
Example:
Output:
75