-3
library(tidyverse)
library(readr)
library(countrycode)
library(stringr)

B07 <- read_csv("Downloads/BorderData/BorderApprehension2007.csv")
B08 <- read_csv("Downloads/BorderData/BorderApprehension2008.csv")

B07[is.na(B07)] = 0
B08[is.na(B08)] = 0
# and so on...
B07$CITIZENSHIP <- str_to_title(B07$CITIZENSHIP)

B07$continent <- countrycode(sourcevar = B07[,"CITIZENSHIP"], origin = "country.name", destination = "continent")

I want to assign a continent instead of the names of countries, but I keep running into this error...

Error in countrycode(sourcevar = B07[, "CITIZENSHIP"], origin = "country.name", : sourcevar must be a character or numeric vector. This error often arises when users pass a tibble (e.g., from dplyr) instead of a column vector from a data.frame (i.e., my_tbl[, 2] vs. my_df[, 2] vs. my_tbl[2])

Here is an image of my data:

2

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
josephtg1
  • 1
  • 1
  • 2
    Welcome to SO! In future, please help those who want to help you by providing a minimum reproducible example. [This post](https://stackoverflow.com/help/minimal-reproducible-example) may help. – Limey Jul 07 '21 at 07:16

1 Answers1

1

As the error says: "sourcevar must be a character or numeric vector. This error often arises when users pass a tibble (e.g., from dplyr) instead of a column vector from a data.frame (i.e., my_tbl[, 2] vs. my_df[, 2] vs. my_tbl[[2]])"

Look at the difference between the results of B07[,"CITIZENSHIP"] versus B07[["CITIZENSHIP"]] or B07$CITIZENSHIP

library(tibble)
library(countrycode)

B07 <- tibble(CITIZENSHIP = c("United States", "Canada", "Mexico"))

countrycode(sourcevar = B07[,"CITIZENSHIP"], origin = "country.name", destination = "continent")
#> Error in countrycode(sourcevar = B07[, "CITIZENSHIP"], origin = "country.name", : sourcevar must be a character or numeric vector. This error often
#>              arises when users pass a tibble (e.g., from dplyr) instead of a
#>              column vector from a data.frame (i.e., my_tbl[, 2] vs. my_df[, 2]
#>                                               vs. my_tbl[[2]])

countrycode(sourcevar = B07[["CITIZENSHIP"]], origin = "country.name", destination = "continent")
#> [1] "Americas" "Americas" "Americas"

countrycode(sourcevar = B07$CITIZENSHIP, origin = "country.name", destination = "continent")
#> [1] "Americas" "Americas" "Americas"


B07[,"CITIZENSHIP"]
#> # A tibble: 3 x 1
#>   CITIZENSHIP  
#>   <chr>        
#> 1 United States
#> 2 Canada       
#> 3 Mexico

B07[["CITIZENSHIP"]]
#> [1] "United States" "Canada"        "Mexico"

B07$CITIZENSHIP
#> [1] "United States" "Canada"        "Mexico"
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56