Error in Bind_Rows_(X, .Id):Column Can't Be Converted from Factor to Numeric

Error in bind_rows_(x, .id) : Column can't be converted from factor to numeric

I think that this should work:

library(plyr)
all_data <- rbind.fill(data1,data2)

bind_rows(), column can't be converted from integer to character error

As pointed out by @akrun in the comments, bind_rows is type sensitive. Therefore, I would first use lapply within dplyr to mutate_if over the list and then bind_rows of the character data frames, setNames to be able to call the variable in filtering by Area_Name in the final step:

fips <- read_html("https://www.census.gov/geo/reference/ansi_statetables.html") %>% 
html_nodes("table") %>%
html_table() %>%
lapply(., mutate_if, is.integer, as.character) %>%
bind_rows() %>%
setNames(gsub(" ", "_", names(.))) %>%
filter(!grepl("Status", Area_Name)) %>%
mutate(Name = ifelse(is.na(Name), Area_Name, Name)) %>%
select(Name, FIPS_State_Numeric_Code, Official_USPS_Code)

bind_rows in dplyr throwing unusual error

The error messages says that: "in one data.frame, 'rmnumber' in of class integer and in the other data.frame, 'rmnumber' is of class factor. I cannot bind different classes together".

Let's use your example

x.df <- data.frame(first_name = c("abc"), last_name = c("def"), rmnum = (1:15), addy = ("some_address"))
y.df <- data.frame(first_name = c("abc"), last_name = c("def"), rmnum = (1:15), addy = ("some_address"))

We check the class for each column of "x.df" and "y.df":

sapply(x.df, class)
# first_name last_name rmnum addy
# "factor" "factor" "integer" "factor"

sapply(y.df, class)
# first_name last_name rmnum addy
# "factor" "factor" "integer" "factor"

All is fine, the classes between data.frames are consistent. Now, let's turn "y.df$rmnum" into factor:

y.df$rmnum <- factor(y.df$rmnum)
class(y.df$rmnum)
# [1] "factor"

Let's try to bind now:

bind_rows(x.df, y.df)

Error: incompatible type (data index: 2, column: 'rmnum', was collecting: integer (dplyr::Collecter_Impl<13>), incompatible with data of type: factor

Same error message. So, in one of your data.frame, 'rmnumber' is integer and in the other one, 'rmnumber' is a factor. You have to turn the factored 'rmnumber' into integer, or the opposite.

Error: Can't combine character and double using bind_rows

You’ve diagnosed the problem correctly. Your fix fails to work because you forgot that you are working on a list of data.frames. You are converting them to one single data.table via as.data.table, but this won’t do what you want it to do. Instead, you need to use lapply (or similar) to perform the fix (i.e. the column conversion) on all of your tables in the list:

d <- list.files(path = "data", full.names = TRUE, pattern = "\\.csv$") %>%
lapply(read_csv) %>%
lapply(\(x) mutate(x, across(rt, as.double))) %>%
bind_rows()

bind_rows is throwing an error - can't combine 1.activity.id and `..2$activity ID` factor2585

While inflexible, i went ahead with rbind which solved the problem for now. Still not able to figure out what caused Bind_rows to fail.

purrr mapping not producing tidy data

A simple way to do this is to reshape your data to long form, which lets you aggregate with ordinary dplyr:

library(tidyverse)

mpg_means <- mtcars %>%
gather(variable, value, -mpg) %>%
group_by(variable, value) %>%
summarise(mean_mpg = mean(mpg))

mpg_means
#> # A tibble: 146 x 3
#> # Groups: variable [?]
#> variable value mean_mpg
#> <chr> <dbl> <dbl>
#> 1 am 0. 17.1
#> 2 am 1. 24.4
#> 3 carb 1. 25.3
#> 4 carb 2. 22.4
#> 5 carb 3. 16.3
#> 6 carb 4. 15.8
#> 7 carb 6. 19.7
#> 8 carb 8. 15.0
#> 9 cyl 4. 26.7
#> 10 cyl 6. 19.7
#> # ... with 136 more rows

Note that while mtcars is entirely numeric, if you have different types, converting to long form will coerce variable types. The calculations will be the same, but it may cause issues later. To resolve it, use an output format that can handle diverse types, e.g.

mpg_means_in_list_cols <- mtcars %>% 
as_tibble() %>% # compact printing for list columns
summarise_all(list) %>% # collapse each column into a list of itself
gather(group, group_values, -mpg) %>%
mutate(mpg_means = map2(mpg, group_values, # for each mpg/value pair, ...
~tibble(mpg = .x, group_value = .y) %>% # ...reconstruct a data frame...
group_by(group_value) %>%
summarise(mean_mpg = mean(mpg)))) # ...and aggregate

mpg_means_in_list_cols
#> # A tibble: 10 x 4
#> mpg group group_values mpg_means
#> <list> <chr> <list> <list>
#> 1 <dbl [32]> cyl <dbl [32]> <tibble [3 × 2]>
#> 2 <dbl [32]> disp <dbl [32]> <tibble [27 × 2]>
#> 3 <dbl [32]> hp <dbl [32]> <tibble [22 × 2]>
#> 4 <dbl [32]> drat <dbl [32]> <tibble [22 × 2]>
#> 5 <dbl [32]> wt <dbl [32]> <tibble [29 × 2]>
#> 6 <dbl [32]> qsec <dbl [32]> <tibble [30 × 2]>
#> 7 <dbl [32]> vs <dbl [32]> <tibble [2 × 2]>
#> 8 <dbl [32]> am <dbl [32]> <tibble [2 × 2]>
#> 9 <dbl [32]> gear <dbl [32]> <tibble [3 × 2]>
#> 10 <dbl [32]> carb <dbl [32]> <tibble [6 × 2]>

While this is decidedly not as pretty, it's capable of holding many types tidily. To extract the result above, just add %>% unnest(mpg_means). As-is, grouping variables are each held in a list element of group_values and in aggregated form in the first column of each mpg_means tibble.



Related Topics



Leave a reply



Submit