I am trying to read multiple csv. files and join them into one.
So I originally have:
d <- list.files(path="data/", full.names = TRUE, pattern = "\\.csv$") %>%
lapply(read_csv) %>%
bind_rows
I get the following error:
Error: Can't combine rt
character and rt
double.
I imagine this must be because rt is a variable which sometimes is a number and otherwise is null.
I tried to change rt into double with mutate, like this:
d <- list.files(path="data/", full.names = TRUE, pattern = "\\.csv$") %>%
lapply(read_csv) %>%as.data.table()%>%mutate(across(rt, as.double))%>%
bind_rows
However when I do that I run into a more complicated problem. I get the following message:
Item 1 has 541 rows but longest item has 583; recycled with remainder.Item 3 has 561 rows but longest item has 583; recycled with remainder.Item 4 has 473 rows but longest item has 583; recycled with remainder.Item 5 has 575 rows but longest item has 583; recycled with remainder.Item 6 has 4 rows but longest item has 583; recycled with remainder.Problem with mutate()
input ..1
.
i NAs introduced by coercion
i Input ..1
is across(rt, as.double)
.NAs introduced by coercion
it seems (from my understanding) that one of the csv files is longer than the other and for this reason only one of them is mutated and only one of them is binded.
You’ve diagnosed the problem correctly. Your fix fails to work because you forgot that you are working on a list of data.frame
s. 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With