c("region", "value") %in% colnames(user.df) 不都是 TRUE

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

在使用 choroplethrZip包。

我正在尝试使用上述包根据该邮政编码中的标签值绘制美国地图的邮政编码。我正在尝试使用以下代码但不起作用

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

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

#install_github('arilamstein/choroplethrZip@v1.5.0')
library(choroplethrZip)

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

data.txt 长得像

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

函数 zip_choropleth 期望 df

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

但是,你读取数据的方式,df没有这个属性:

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

这就是错误消息告诉您的内容:

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

这只是一种复杂的说法,即 df 的列名不是预期的 regionvalue

这里的问题是文件中的列 headers 被读取时就好像它们是数据的一部分一样。但这种行为很容易改变:

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