Error: attempt to use zero-length variable name

54,890

Solution 1

I've posted an issue on dplyr github page. I can reproduce the results using the code below. It has to do with whether the csv contains a column of rownames without a header. read_csv and read.csv handle this differently, thus producing differing results with filter.

First the case when it works

write_csv to read_csv or read.csv; both work fine with filter

library(readr)
library(dplyr)

mtcars %>% write_csv("~/Desktop/test.csv")
test_r <-  read_csv("~/Desktop/test.csv") %>% filter(hp>100)
test.r <-  read.csv("~/Desktop/test.csv") %>% filter(hp>100)

Now for when it fails

When csv is generated through a process like write.csv, unless the person changes the default of row.names to FALSE, it introduces a column of rownames w/o a header. When reading the data back in, read_csv does not fill in the header where the rownames are, but read.csv imputes an X. Thus, when filter works on read.csv imports, it has all headers with filled cells, but filter after read_csv has an empty header cell at least where rownames are.

The following code should error after test1_r %>% filter(hp>100) with the following error

Error in filter_impl(.data, dots) : 
  attempt to use zero-length variable name

Again, the big difference is how write.csv produces the csv.

mtcars %>% write.csv("~/Desktop/test1.csv")


test1_r <- read_csv("~/Desktop/test1.csv")
test1_r %>% str() 
#should fail here
test1_r %>% filter(hp>100)


test1.r <- read.csv("~/Desktop/test1.csv")
test1.r %>% str() 
test1.r %>% filter(hp>100)

To solve the problem, you can use read.csv as mentioned above by @hackR. Or you can subset out the first column when you know the csv behaves like this:

test1_r <- read_csv("~/Desktop/test1.csv")[-1]

Or, if you are in control of the csv-creation step, you can add the option row.names=FALSE to write.csv

mtcars %>% write.csv("~/Desktop/test2.csv", row.names = FALSE)
test2.r <- read_csv("~/Desktop/test2.csv")
test2.r %>% str() 
test2.r%>% filter(hp>100)

or use write_csv as shown above.

Solution 2

I had the same error message and it was related to read_csv() from Hadley's readr package. If I did this:

cyt <- read_csv(fil)
cyt2 <- cyt %>% filter(EPID == 10030001)

then I got the error message "Error in filter_impl(.data, dots) : attempt to use zero-length variable name". But if I used the analogous base read.csv() function then it worked OK.

cyt <- read.csv(fil, stringsAsFactors = FALSE)
cyt2 <- cyt %>% filter(EPID == 10030001)

I think this may be a bug in read_csv(), or perhaps I am misusing read_csv().


In your specific case, perhaps something is running automatically when you are loading RStudio.

Solution 3

Putting this as an answer for visibility: this happens if you try to run by selecting all in the Rmd and pressing enter like you would with a normal R script. RStudio tries to run this all as R code, including the markdown parts, leading to the errors you saw.

You can avoid this by running an individual chunk by clicking the green play button or by selecting one of the run options in the dropdown at the top of the Rmd editor.

Try run all from RUN dropdown

Share:
54,890

Related videos on Youtube

skeletonnoire
Author by

skeletonnoire

Updated on July 21, 2022

Comments

  • skeletonnoire
    skeletonnoire over 1 year

    Without opening any files, I get the following error when opening Rstudio:

    "Error: attempt to use zero-length variable name"

    All my commands are ignored (i.e. nothing happens, no warnings and R becomes unresponsive to anything I do) and I have tried rm and gc options to no availability and also updated to the latest version of Rstudio and R but I still get this error.

    I am running windows 7 (64bit).

  • data_steve
    data_steve almost 8 years
    I have the same exact problem and replacing with read.csv solved it.
  • data_steve
    data_steve almost 8 years
    @skeletonnoire, did this address your problem?
  • BLT
    BLT about 6 years
    This can also just happen if your .csv has a column without a header.
  • Admin
    Admin about 4 years
    I have exact same problem
  • Jolin
    Jolin almost 4 years
    @data_steve Thanks , read_csv works for me as initially i am using read.csv to read my file which cause this error too.
  • Anaphory
    Anaphory almost 2 years
    I should probably turn this into a self-answered question, but I had exactly this happen recently without actually running more than a code chunk. Turns out RStudio has some issues with wide unicode characters, which made it think the code chunk included the ``` at the end.

Related