Search code examples
rcsvreadr

Unused argument (sep=";") error happens with all CSV files


I'd like to open a random csv-file like New_York_City_Leading_Causes_of_Death.csv (just a dummy, from a tutorial but happens with my own data the same way; replaced , with ;) https://www.dropbox.com/s/3usf8y75vz2cpj4/New_York_City_Leading_Causes_of_Death.csv?dl=0

The outline says: Error in read_csv("New_York_City_Leading_Causes_of_Death.csv", sep = ";") : unused argument (sep = ";")

I created several csv-files to debug I tried readr::read_csv as well as utils::read_csv -> both same problem I made sure that csv-file and .r are in the same folder I am using a pc by the university (maybe there's the bottleneck?)

Thank you in advance :)

library(utils)
library(utf8)

df <- read_csv("New_York_City_Leading_Causes_of_Death.csv", sep = ";" )
head(df)

That's the error message I get:

Error in read_csv("New_York_City_Leading_Causes_of_Death.csv", sep = ";") : unused argument (sep = ";")

I expect the output to be a data-frame.


Solution

  • The utils package only has the read.csv() function--notice the . instead of a _. As for readr's read_csv() function, it does not have a parameter called sep. This is why an error message about having an unused argument sep appears every time you run your code.

    I tried executing just read_csv('New_York_City_Leading_Causes_of_Death.csv') without any arguments and was successfully able to read in the following tibble:

    # A tibble: 1,094 x 7
        Year `Leading Cause`                                 Sex   `Race Ethnicity`    Deaths `Death Rate` `Age Adjusted Dea~
       <dbl> <chr>                                           <chr> <chr>               <chr>  <chr>        <chr>             
     1  2010 Assault (Homicide: Y87.1, X85-Y09)              M     Black Non-Hispanic  299    35.1         35.5              
     2  2011 Mental and Behavioral Disorders due to Acciden~ M     Not Stated/Unknown  5      .            .                 
     3  2011 Diseases of Heart (I00-I09, I11, I13, I20-I51)  M     Black Non-Hispanic  1840   215.7        268.3             
     4  2008 Certain Conditions originating in the Perinata~ F     Other Race/ Ethnic~ .      .            .                 
     5  2014 Accidents Except Drug Posioning (V01-X39, X43,~ F     Hispanic            64     5.1          5.4               
     6  2007 Intentional Self-Harm (Suicide: X60-X84, Y87.0) M     Not Stated/Unknown  5      .            .                 
     7  2012 Accidents Except Drug Posioning (V01-X39, X43,~ M     Black Non-Hispanic  152    17.8         18.6              
     8  2009 All Other Causes                                M     Asian and Pacific ~ 220    43.1         56.1              
     9  2013 Diseases of Heart (I00-I09, I11, I13, I20-I51)  F     Asian and Pacific ~ 437    72.8         81.8              
    10  2014 Accidents Except Drug Posioning (V01-X39, X43,~ M     Other Race/ Ethnic~ 12     .            .                 
    # ... with 1,084 more rows
    

    Alternatively, for faster performance on larger datasets, consider using data.table::fread(). The fread() function from the data.table package allows you to specify the delimiter to be ; using the argument sep = ';',. Do note that fread() can only return a data.table or a data.frame. By default, fread() returns a data.table, but can return a data.frame if the argument data.table = FALSE is passed in. However, if you would prefer working with a tibble, you can simply wrap the fread() call in as_tibble().