I'm attempting to simulate a hash map with the following code and get an index out of bounds in the if statement?
I'm not sure what is causing this since printing the initial null values from a loop works fine.
strings <- c("string1","string2","string3")
test <- NULL
for (i in 1:length(strings)) {
print(is.null(test[[strings[i]]]))
}
for (i in 1:length(strings)) {
if (is.null(test[[strings[i]]])) {
test[[strings[i]]] <- 1
}
}
Instead of test <- NULL you can initialize test as a list:
test <- list()
This should remove the error message and give the desired result:
> test
#$string1
#[1] 1
#
#$string2
#[1] 1
#
#$string3
#[1] 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With