Combing multiple csv files

Hello,
I am trying to combine multiple csv files with same headers, but after running the below code, i get a response # A tibble: 0 x 0

cyclist_df <- list.files(path='~/cloud/project/capstone', pattern = "*.csv", full.names = TRUE) %>%
lapply(read_csv) %>%
bind_rows

cyclist_df

Can anyone be of help?

1 Like

The code looks correct. What is the result of the list.files(...) alone? What is the result of list.files(...) %>% lapply(...)? You need to narrow things down to find exactly what part is incorrect.

Note: your code works with my fake data, so that really suggests you are looking in the wrong directory:

library(tidyverse)

# Create data
write_csv(data.frame(a = 1:3,
                     b = 4:6),
          "test1.csv")

write_csv(data.frame(a = 1:6,
                     b = 4:9),
          "test2.csv")

# Run
list.files(path='.', pattern = "*.csv", full.names = TRUE) %>%
  lapply(read_csv) %>%
  bind_rows
#> Rows: 3 Columns: 2
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (2): a, b
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> Rows: 6 Columns: 2
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (2): a, b
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> # A tibble: 9 × 2
#>       a     b
#>   <dbl> <dbl>
#> 1     1     4
#> 2     2     5
#> 3     3     6
#> 4     1     4
#> 5     2     5
#> 6     3     6
#> 7     4     7
#> 8     5     8
#> 9     6     9

Created on 2022-07-04 by the reprex package (v2.0.1)

Thank you for the response.
Yes i was looking at the wrong directory, but i have been able to re-direct to the right path using.
getwd()
setwd()

Great!

Personal note, I usually avoid using getwd() and setwd() for exactly this reason: when using them, there is a chance that code that was working stops working for reasons that are not in the code. Since I started using RStudio projects I never had that problem again.

Other people prefer the {here} package, it's the same idea, you define once where on your computer you are, then you can use relative paths, so you never change the working directory.

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.