3

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 ;)

3
  • 2
    Please, specifify the package you are using. read_csv() is not part of base R. Commented Sep 21, 2017 at 13:16
  • I use the library(readr). Commented Sep 21, 2017 at 13:18
  • 3
    Please, edit your question accordingly and make sure it is an minimal reproducible example - Thank you. Commented Sep 21, 2017 at 13:20

3 Answers 3

7

if you just want to impose column names you could do this:

read data with no column names:

data <- read.csv(file="myfile.csv", header=FALSE, sep=",")

impose column names:

names(data) <- c('id','name','type')

view data with column names:

data

id      name    type
37707   name1   t1 
37726   name2   t2 
37737   name3   t3
Sign up to request clarification or add additional context in comments.

Comments

3

You can just specify the column names with the col_names argument:

> library(readr)
> read_csv("example.csv", col_names = c("name", "year_a", "ps"), col_types = cols(col_character(), col_character(), col_double()))
# A tibble: 4 x 3
   name year_a    ps
  <chr>  <chr> <dbl>
1     1      2   3.0
2 test1   2017  35.5
3 test2   2018  44.5
4 test3   2019  22.0

So to use your example variables, you could just do:

library(readr)
read_csv("example.csv", col_names = c(car_names, year_a, engine_power), col_types = cols(col_character(), col_character(), col_double()))

2 Comments

Thank you your answer, do you think there is a solution where the order of the columns is not important? (the original csv has 30 columns)
I'm not really sure I understand your question. The order of the columns in your call to read_csv is important because it has to match the order of the columns in your file. There is no way that the order of the columns can have no meaning, because the csv file format gives the order of the columns meaning. The only way to know which column is which is by their order.
1
data1 <- read.csv("2016.csv",col.names = c("consitituency_name","candidate_surname","candidate_first_name","result","count_number","transfers","votes","total_votes","candidate_id","party"))

This code works fine on my system

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.