c("region", "value") %in% colnames(user.df) are not all TRUE

2.2k views Asked by At

I'm getting "c("region", "value") %in% colnames(user.df) are not all TRUE" error while creating a choropleth of US ZIP codes using choroplethrZip package.

I am trying to plot the zip codes of the US map based on the label value in that zip code, using the above packages. I am trying to use the following code but doesn't work

#install.packages("devtools")
library(devtools)

#install_github("choroplethr", "trulia")
library(choroplethr)

#install_github('arilamstein/[email protected]')
library(choroplethrZip)

temp <- read.table("data.txt")
zip_choropleth(temp)

data.txt looks like

region  value
00601   15
00602   42
00603   97
00604   3
.       .
.       .
1

There are 1 answers

0
Stibu On BEST ANSWER

The function zip_choropleth expects df to be

A data.frame with a column named "region" and a column named "value".

However, the way you read the data, df does not have this property:

temp <- read.table("data.txt")
temp
##       V1    V2
## 1 region value
## 2  00601    15
## 3  00602    42
## 4  00603    97
## 5  00604     3

And this is what the error message tells you:

c(“region”, “value”) %in% colnames(user.df) are not all TRUE

This is just a complicated way of saying that the column names of df are not region and value as they are expected to be.

The issue here is that the column headers in the file are read as if they were part of the data. But this behaviour can be easily changed:

temp <- read.table("data.txt", header = TRUE)
temp
##   region value
## 1    601    15
## 2    602    42
## 3    603    97
## 4    604     3