-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sf issue with vec_rbind()
and empty data.frame()
#1891
Comments
Could you please turn this into a self-contained reprex (short for minimal reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff. If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. You can install reprex by running (you may already have it, though, if you have the tidyverse package installed): install.packages("reprex") Thanks |
# Load required packages
library(rjson)
library(tidyverse)
library(tidycensus)
library(dplyr)
library(reprex)
# Set the API key
#census_api_key(api_key_data$apiKey, install = TRUE)z
# Define the variables
variables = c('DP03_0018', #Commuting to Work Estimate
'DP03_0018P', #Commuting to Work Percent
'DP03_0019', #Commuting to Work Drive Alone Estimate
'DP03_0019P', #Commuting to Work Drive Alone Percent
'DP03_0021', #Commuting to Work Public Transportation Estimate
'DP03_0021P', #Commuting to Work Public Transportation Percent
'DP05_0001', #Total Population Estimate
'DP05_0001P', #Total Population Percent
'DP05_0002', #Total Population Male Estimate
'DP05_0002P', #Total Population Male Percent
'DP05_0003', #Total Population Female Estimate
'DP05_0003P', #Total Population Female Percent
'DP05_0008P', #Age 15-19 Percent of Pop
'DP05_0009P', #Age 20-24 Percent of Pop
'DP05_0010P', #Age 25-34 Percent of Pop
'DP05_0011P', #Age 35-44 Percent of Pop
'DP05_0012P', #Age 45-54 Percent of Pop
'DP05_0013P', #Age 55-59 Percent of Pop
'DP05_0014P', #Age 60-64 Percent of Pop
'DP05_0015P', #Age 65-74 Percent of Pop
'DP05_0016P', #Age 75-84 Percent of Pop
'DP05_0017P', #Age 85 and older Percent of Pop
'DP03_0076P', #Family Income Less Than 10k
'DP03_0077P', #Family Income 10k-14999
'DP03_0078P', #Family Income 15k-24999
'DP03_0079P', #Family Income 25k-34999
'DP03_0080P', #Family Income 35k-49999
'DP03_0081P', #Family Income 50k-74999
'DP03_0082P', #Family Income 75k-99999
'DP03_0083P', #Family Income 100k-149999
'DP03_0084P', #Family Income 150k-199999
'DP03_0085P' #Family Income Greater Than 200k
)
# Define the years you want to loop through
years <- 2010:2021
survey <- ifelse(years < 2013, "acs1", "acs5")
# Create an empty data frame to store the results
population_data <- data.frame()
# Loop through the years
for (i in 1:length(years)) {
year <- years[i]
current_survey <- survey[i]
# Use a tryCatch block to handle potential errors
tryCatch({
# Retrieve data for the current year and survey type
acs_data <- get_acs(
geography = "county",
variables = variables,
year = year,
survey = current_survey,
output = "wide",
geometry = TRUE
)
# Add a "year" column
acs_data$year <- year
# Append the data for the current year to the results data frame
population_data <- bind_rows(population_data, acs_data)
}, error = function(e) {
cat("Error for year", year, ":", conditionMessage(e), "\n")
# You can handle the error as needed, e.g., continue the loop or take other actions
})
} |
#> Getting data from the 2010 1-year ACS |
#> Getting data from the 2011-2015 5-year ACS Print or save the results data frameView(population_data) You can save the results as a CSV file if needed#write.csv(population_data, "acs_data.csv", row.names = FALSE)
|
Sorry, I have no idea what I'm doing. It's my first issue. |
The code in #1891 (comment) was great, thanks so much! I was able to extract a minimal reprex of: library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
pt1 = st_point(c(0,1))
pt2 = st_point(c(1,1))
d = data.frame(a = 1:2)
d$geom = st_sfc(pt1, pt2)
df = st_as_sf(d)
df
#> Simple feature collection with 2 features and 1 field
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: 0 ymin: 1 xmax: 1 ymax: 1
#> CRS: NA
#> a geom
#> 1 1 POINT (0 1)
#> 2 2 POINT (1 1)
vctrs::vec_rbind(
data.frame(),
df
)
#> Error in `vctrs::vec_rbind()`:
#> ! `c()` method returned a vector of unexpected size 4 instead of 2.
#> ℹ In file 'c.c' at line 414.
#> ℹ This is an internal error that was detected in the vctrs package.
#> Please report it at <https://github.com/r-lib/vctrs/issues> with a reprex (<https://tidyverse.org/help/>) and the full backtrace.
#> Backtrace:
#> ▆
#> 1. ├─vctrs::vec_rbind(data.frame(), df)
#> 2. └─rlang:::stop_internal_c_lib(...)
#> 3. └─rlang::abort(message, call = call, .internal = TRUE, .frame = frame) |
I think this may be the same as #1748, but I'm not sure. I'll leave both open |
vec_rbind()
and empty data.frame()
Using R to download ACS profiles for a range of years. Code ran fine until I set the geometry to TRUE, then I received this error.
Error for year 2021 :
c()method returned a vector of unexpected size 3223 instead of 3221. ℹ In file c.c at line 414. ℹ Install the winch package to get additional debugging info the next time you get this error. ℹ This is an internal error that was detected in the vctrs package. Please report it at <https://github.com/r-lib/vctrs/issues> with a reprex and the full backtrace.
I am performing the Census API calls using the tidycensus library.
Here is the for loop I am running:
`# Loop through the years
for (i in 1:length(years)) {
year <- years[i]
current_survey <- survey[i]
Use a tryCatch block to handle potential errors
tryCatch({
# Retrieve data for the current year and survey type
acs_data <- get_acs(
geography = "county",
variables = variables,
year = year,
survey = current_survey,
geometry = TRUE,
output = "wide"
)
}, error = function(e) {
cat("Error for year", year, ":", conditionMessage(e), "\n")
# You can handle the error as needed, e.g., continue the loop or take other actions
})
}`
The text was updated successfully, but these errors were encountered: