I would like to read a csv file and to use the column names from variables. Of Course the strings in the variables are equal to the column names of the csv file.
The name in the column in the csv are e.g. "name" , "ps" and "year". When I just would like to read the csv and to assign column types (without any variables) I use:
library(readr)
CarTest <- read_csv("~/_file.csv",
col_types = cols(
name = col_character(),
year_a = col_character(),
ps =col_double()))
The idea is to assign the name of the columns in a variable to ensure that if the column names in the csv file are changed, just the strings of variables have to be changed, too. So, I would like to assign the column names in variables before (still the column names in the csv_file and the strings in the variables are the same). I tried different approaches: This example (hopefully) shows, that I try to paste the string of the variable car_names in the read_csv function. But obviously get is the wrong approach:
library(readr)
car_names <- "name"
engine_power <- "ps"
year_a <-"year"
CarTest <- read_csv("~/_file.csv",
col_types = cols(
get("car_names") = col_character(),
get("year_a") = col_character(),
get("engine_power") =col_double()))
Thank you for your help ;)
read_csv()is not part of base R.