Gov 1005 pset 4 - subscript out of of bounds

Hey! Trying to extract the state name from a list of "city, state" variables for Problem Set 4. The weird thing is that this exact code chunk works when I run it through Reprex, but it gives the " Error in FUN(X[[i]], ...) : subscript out of bounds " message when I run it in the Rmd file or the Console.

Any help is appreciated, and I apologize for messing up this reprex format when posting the first time.

library(tidyverse)
library(magrittr)
#> 
#> Attaching package: 'magrittr'
#> The following object is masked from 'package:purrr':
#> 
#>     set_names
#> The following object is masked from 'package:tidyr':
#> 
#>     extract
library(reprex)

get_state <- function(countystate){
  countystate %>% 
  str_split_fixed(.,", ", 2) %>% 
  pluck(2) %>% 
  return()
 }

get_state("Treasure County, Montana")
#> [1] "Montana"

Please see the FAQ: What's a reproducible example (`reprex`) and how do I create one? Using a reprex, complete with representative data will attract quicker and more answers.

The first thing to look at is at the end

could not find function "%>%

which means magrittr isn't in namespace

Yep! You were totally right about magrittr.

I fixed it now, but even though the code runs in Reprex, the same error message keeps appearing in my Rmd and Console

Have you also included the library call in your Rmd file? have in mind that code run through reprex, runs on a separate clean environment, it doesn't leave anything on your current working environment.

Also, I recommend you take out the " Gov 1005 pset 4" part in your topic title since it seems like you are posting for this topic and those posts are not meant to be solved.

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