【问题标题】:Failed two methods to subset dataset with R, requesting assistance使用 R 子集数据集的两种方法失败,请求帮助
【发布时间】:2017-09-04 21:25:30
【问题描述】:

我正在尝试用 R(开源统计脚本语言)制作一些数据的子集。我尝试了两种方法,但都没有成功。一个返回没有数据的表格,另一个返回所有“NA”单元格的表格,但尺寸明显正确。

我把代码很清楚地注释了--

  • 首先,我创建邮政编码列表,用于对数据进行子集化。邮政编码列表来自我将使用的数据集。 邮政编码列表称为“zipCodesOfData”

  • 接下来,我下载我将作为子集的犯罪数据。我基本上只是将其子集到我需要的数据集中。

  • 最后一部分,第三部分,显示我尝试了 %in% 和 filter 方法来根据邮政编码数据过滤犯罪数据。

不幸的是,这两种方法都不起作用。我希望有人能指出我的错误或为第三部分推荐不同的子集方法。

(顺便说一句,在第二部分中,我尝试将列表转换为数据框,但它不起作用。我很好奇为什么,如果有人可以为我阐明这一点。)

感谢您的时间和帮助!

####
#### Section zero: references and dependencies
####
# r's "choroplethr" library creator's blog for reference:
# http://www.arilamstein.com/blog/2015/06/25/learn-to-map-census-data-in-r/
# http://stackoverflow.com/questions/30787877/making-a-zip-code-choropleth-in-r-using-ggplot2-and-ggmap
# 
# library(choroplethr)
# library(choroplethrMaps)
# library(ggplot2)
# # use the devtools package from CRAN to install choroplethrZip from github
# # install.packages("devtools")
# library(devtools)
# install_github('arilamstein/choroplethrZip')
# library(choroplethrZip)
# library(data.table)
# 
####
#### Section one: the data set providing the zipcode we'll use to subset the crime set
####
austin2014_data_raw <- fread('https://data.austintexas.gov/resource/hcnj-rei3.csv')
names(austin2014_data_raw)
nrow(austin2014_data_raw)
## clean up: make any blank cells in column ZipCode say "NA" instead -> source:  http://stackoverflow.com/questions/12763890/exclude-blank-and-na-in-r
austin2014_data_raw[austin2014_data_raw$ZipCode==""] <- NA
# keep only rows that do not have "NA"
austin2014_data <- na.omit(austin2014_data_raw)
nrow(austin2014_data) # now there's one less row.

# selecting the first column, which is ZipCode
zipCodesOfData <- austin2014_data[,1]
View(zipCodesOfData)
# Now we have the zipcodes we need: zipCodesOfData

####
#### Section two: Crime data
####
# Crime by zipcode: https://data.austintexas.gov/dataset/Annual-Crime-2014/7g8v-xxja
#   (visualized: https://data.austintexas.gov/dataset/Annual-Crime-2014/8mst-ed5t )
# https://data.austintexas.gov/resource/<insertResourceNameHere>.csv  w/ resource "7g8v-xxja"

austinCrime2014_data_raw <- fread('https://data.austintexas.gov/resource/7g8v-xxja.csv')
View(austinCrime2014_data_raw)
nrow(austinCrime2014_data_raw)

# First, let's remove the data we don't need
names(austinCrime2014_data_raw)
columnSelection_Crime <- c("GO Location Zip", "GO Highest Offense Desc", "Highest NIBRS/UCR Offense Description")
austinCrime2014_data_selected_columns <- subset(austinCrime2014_data_raw, select=columnSelection_Crime)
names(austinCrime2014_data_selected_columns)
nrow(austinCrime2014_data_selected_columns)


####
#### Section Three: The problem: I am unable to make subsets with the two following methods.
####
# Neither of these methods work: 

# Attempt 1:

austinCrime2014_data_selected_columns <- austinCrime2014_data_selected_columns[austinCrime2014_data_selected_columns$`GO Location Zip` %in% zipCodesOfData , ]
View(austinCrime2014_data_selected_columns) # No data in the table

# Attempt 2:

# This initially told me an error:
# Then, I installed dplyr and the error went away.  
library(dplyr)
# However, it still doesn't create anything-- just an empty set w/ headers
austinCrime2014_data_selected_zips <- filter(austinCrime2014_data_selected_columns, `GO Location Zip` %in% zipCodesOfData)
View(austinCrime2014_data_selected_zips)

在意识到没有必要之后,我将这一部分删掉了。

####
####  Bad section
####
nrow(austinCrime2014_data_selected_columns)

# Then, let's keep only the zipcodes we need
# doesnt work: austinCrime2014_data_selected_columns_df <- data.frame(austinCrime2014_data_selected_columns)
# typeof(austinCrime2014_data_selected_columns_df)

austinCrime<-do.call("rbind", austinCrime2014_data_selected_columns)
austinCrime_needsTranspose <-as.data.frame(austinCrime)
austinCrime <- t(austinCrime_needsTranspose)
typeof(austinCrime)
View(austinCrime)
names(austinCrime)

####
####  Bad section
####

【问题讨论】:

  • austinCrime_df 是一个矩阵

标签: r dataframe subset data-cleaning


【解决方案1】:

我认为readrdplyr 可以解决您的问题。很简单:

library(readr)
library(dplyr)

### SECTION 1

# Import data
austin2014_data_raw <- read_csv('https://data.austintexas.gov/resource/hcnj-rei3.csv', na = '')
glimpse(austin2014_data_raw)
nrow(austin2014_data_raw)

# Remove NAs
austin2014_data <- na.omit(austin2014_data_raw)
nrow(austin2014_data) # now there's one less row.

# Get zip codes
zipCodesOfData <- austin2014_data$`Zip Code`

### SECTION 2

# Import data
austinCrime2014_data_raw <- read_csv('https://data.austintexas.gov/resource/7g8v-xxja.csv', na = '')
glimpse(austinCrime2014_data_raw)
nrow(austinCrime2014_data_raw)

# Select and rename required columns
columnSelection_Crime <- c("GO Location Zip", "GO Highest Offense Desc", "Highest NIBRS/UCR Offense Description")
austinCrime_df <- select(austinCrime2014_data_raw, one_of(columnSelection_Crime))
names(austinCrime_df) <- c("zipcode", "highestOffenseDesc", "NIBRS_OffenseDesc")
glimpse(austinCrime_df)
nrow(austinCrime_df)

### SECTION 3

# Filter by zipcode
austinCrime2014_data_selected_zips <- filter(austinCrime_df, zipcode %in% zipCodesOfData)
glimpse(austinCrime2014_data_selected_zips)
nrow(austinCrime2014_data_selected_zips)

这里我使用readr 包中的read_csv() 导入数据,并使用dplyr 包中的子集方法select()filter() 来获取所需的列和行。

【讨论】:

  • 感谢您保持简单并向我介绍一瞥!
  • 不客气!我是dplyr 和所有其他tidyverse 设施的忠实粉丝。
【解决方案2】:

我不确定您为什么要 do.calling 和 transposing 您的数据。您可以使用类似dplyrsemi_join 来获取您想要的邮政编码:


library(data.table)
library(dplyr)
#> -------------------------------------------------------------------------
#> data.table + dplyr code now lives in dtplyr.
#> Please library(dtplyr)!
#> -------------------------------------------------------------------------
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:data.table':
#> 
#>     between, first, last
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
zipCodesOfData <- fread('https://data.austintexas.gov/resource/hcnj-rei3.csv') %>%
  mutate(`Zip Code` = ifelse(`Zip Code` == "", NA, `Zip Code`)) %>%
  na.omit() %>% 
  select(`Zip Code`)

austinCrime2014_data_raw <- fread('https://data.austintexas.gov/resource/7g8v-xxja.csv') %>%
  select(`GO Location Zip`, `GO Highest Offense Desc`, `Highest NIBRS/UCR Offense Description`) %>%
  semi_join(zipCodesOfData, by = c("GO Location Zip" = "Zip Code")) %>%
  rename(zipcode = `GO Location Zip`, 
         highestOffenseDesc = `GO Highest Offense Desc`, 
         NIBRS_OffenseDesc = `Highest NIBRS/UCR Offense Description`)

【讨论】:

  • 谢谢!在进行文件下载调用时,我没有意识到我可以做到这一点!我将不得不调查dplyr!是的,发布后我最终删除了您引用的部分。但是,我重新添加了它们,以免任何人对您在我的原始帖子中引用的内容感到困惑。
猜你喜欢
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 2013-07-24
  • 1970-01-01
  • 2010-11-22
相关资源
最近更新 更多