Extracting specific columns from a multiple csv files

Hi
I uploaded a folder in R containing about 42 csv files. Each csv file in the folder corresponds to a year. So example csv 1 contains weather data from 2000, csv 2 contains data for 2001 and so on. Each csv file contains 29 variables like longitude, latitude, stats name , city, temperature etc. I want to extract these variables from each csv file and compute the average temperature. How do I go about it. I’m new to R.

This is how I read the folder into R
filepath <- dir_ls (“ data directory “)
filepath %>% map (function (path){
read_csv( path)
}
)

Hi, you could create a function that reads the csv files, then selects the columns you want. Map through them then calculate the average. Hard to do without a reproducible example.

Perhaps have a look at this:

Thank you williaml. I tired it . I keep getting this error

Error: cannot allocate vector of size 27.5 Mb.

This is my code

library(tidyverse)
library(fs)
library(readr)
library(vroom)

file_paths <- dir_ls("path") # Data is in this path. all 42 csv files in a folder

data <- file_paths %>%
map(read_csv) %>% # read in all the files individually, using the function read_csv() from the readr package
reduce(rbind) # reduce with rbind into one dataframe
data

Did you try map_dfr?

library(tidyverse)
library(fs)
library(readr)
library(vroom)
library(dplyr)

file_paths <- dir_ls("path") # Data is in this path. all 42 csv files in a folder
data <- file_paths %>% map_dfr(read_csv)

Thank you StatSteph I did @StatSteph . I got an error.
From error, the variable "State code" is character in one of the csvs and the other is a double.
How do I fixed it? How do I know which csv file has State code as character or double ?

Error in dplyr::bind_rows():
! Can't combine State Code and State Code .
Run rlang::last_error() to see where the error occurred.

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.