Function returns "NULL". Why is that?

Hello all,

I would like a little help with this code. My problem is the following:
Why is this code returning me "NULL" when I try to print out the yearz object?

the split_low object is in my workspace:

split_low
[[1]]
[1] "gauss" "1777"

[[2]]
[1] "bayes" "1702"

[[3]]
[1] "pascal" "1623"

[[4]]
[1] "pearson" "1857"

This is my code:
yearz <- for (i in split_low) {
print(i[2])
}

When I bring up yearz, it return:
"NULL"

I would like it to show every second element in each element of the split_low list. So basically "1777" "1702" "1623" "1857".

Can anyone help? Thank you so much

The return value of for is NULL. You can do something like this:

split_low = list(c("gauss", "1777"), c("bayes", "1702"), c("pascal", "1623"), c("pearson", "1857"))
split_low
#> [[1]]
#> [1] "gauss" "1777" 
#> 
#> [[2]]
#> [1] "bayes" "1702" 
#> 
#> [[3]]
#> [1] "pascal" "1623"  
#> 
#> [[4]]
#> [1] "pearson" "1857"
ITEMS <- vector(mode = "character", length = length(split_low))
for (i in seq_along(split_low)){
  ITEMS[i] <- split_low[[i]][2]
}
ITEMS
#> [1] "1777" "1702" "1623" "1857"

Created on 2019-11-28 by the reprex package (v0.2.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.