Index a vector or lists and convert to a list of objects
Details
This function takes a vector and turns it into a list containing 'key' and 'value' for each vector.
This allows the output to be used in loops such as for loops or lapply or other functions to track
the index of the list content e.g. 1,2,3...
This function also contains a validator to ensure that a vector had not been previously 'keyed', which prevents the user from inadvertently calling the function twice on a vector. Helps especially because the function keys the vector, and sets the new list to the variable name of the original vector.
This function takes a vector and turns it into a list containing 'key' and 'value' for each vector.
This allows the output to be used in loops such as for loops or lapply or other functions to track
the index of the list content e.g. 1,2,3...
This function also contains a validator to ensure that a vector had not been previously 'keyed', which prevents the user from inadvertently calling the function twice on a vector. Helps especially because the function keys the vector, and sets the new list to the variable name of the original vector.
Examples
# EXAMPLES for add_key()
#ex1 simple conversion of a vector
rti2 <- c("rpkg","obinna", "obianom")
add_key(rti2)
rti2
#> [[1]]
#> [[1]]$key
#> [1] 1
#>
#> [[1]]$value
#> [1] "rpkg"
#>
#>
#> [[2]]
#> [[2]]$key
#> [1] 2
#>
#> [[2]]$value
#> [1] "obinna"
#>
#>
#> [[3]]
#> [[3]]$key
#> [1] 3
#>
#> [[3]]$value
#> [1] "obianom"
#>
#>
#> attr(,"class")
#> [1] "klist" "list"
#ex2 add keys to a vector content for use in downstream processes
ver1 <- c("Test 1","Test 2","Test 3")
add_key(ver1)
#ex3 use keyed ver1 in for loop
for(i in ver1){
message(sprintf("%s is the key for this %s", i$key, i$value))
}
#> 1 is the key for this Test 1
#> 2 is the key for this Test 2
#> 3 is the key for this Test 3
#ex4 use keyed ver1 in lapply loop
xl1 <- lapply(ver1,function(i){
message(sprintf("lapply - %s is the key for this %s", i$key, i$value))
})
#> lapply - 1 is the key for this Test 1
#> lapply - 2 is the key for this Test 2
#> lapply - 3 is the key for this Test 3
# EXAMPLES for indexed()
#ex1 simple conversion of a vector
rti2 <- c("rpkg","obinna", "obianom")
indexed(rti2)
#> [[1]]
#> [[1]]$key
#> [1] 1
#>
#> [[1]]$value
#> [1] "rpkg"
#>
#>
#> [[2]]
#> [[2]]$key
#> [1] 2
#>
#> [[2]]$value
#> [1] "obinna"
#>
#>
#> [[3]]
#> [[3]]$key
#> [1] 3
#>
#> [[3]]$value
#> [1] "obianom"
#>
#>
#ex2 add keys to a vector content for use in downstream processes
ver1 <- c("Test 1","Test 2","Test 3")
#ex3 use keyed ver1 in for loop
for(i in indexed(ver1)){
message(sprintf("%s is the key for this %s", i$key, i$value))
}
#> 1 is the key for this Test 1
#> 2 is the key for this Test 2
#> 3 is the key for this Test 3
#ex4 use keyed ver1 in lapply loop
xl1 <- lapply(indexed(ver1),function(i){
message(sprintf("lapply - %s is the key for this %s", i$key, i$value))
})
#> lapply - 1 is the key for this Test 1
#> lapply - 2 is the key for this Test 2
#> lapply - 3 is the key for this Test 3