Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename() and error - attempt to use zero-length variable name #817

Closed
tyokota opened this issue Nov 29, 2014 · 9 comments
Closed

rename() and error - attempt to use zero-length variable name #817

tyokota opened this issue Nov 29, 2014 · 9 comments

Comments

@tyokota
Copy link

tyokota commented Nov 29, 2014

I think this issue is solved when using select(); however, it requires that I cbind() the mutated string data (date character string converted to numeric using as.Date() in this case). Can rename() function like select() so that I do not have to cbind() later?

@hadley
Copy link
Member

hadley commented Nov 29, 2014

Can you please provide a reproducible example?

@tyokota
Copy link
Author

tyokota commented Nov 30, 2014

Hi Dr. Wickham,

Here is a sample of what I am working on. I've updated the code to be minimal as possible.

library(XML)
library(httr)
library(stringr)
library(lubridate)
library(plyr)
library(dplyr)

fe.url <- "https://docs.google.com/spreadsheets/d/0Aul9Ys3cd80fdHNuRG5VeWpfbnU4eVdIWTU3Q0xwSEE/pubhtml"
raw.data <- content(GET(fe.url))
sheets <- readHTMLTable(raw.data, header = T, stringsAsFactors = F, trim = T)

head <- data.frame(sheets[[2]])
data <- data.frame(sheets[[3]])

head[34] <- NULL
names(data) <- head[1, ]
names(data) <- toupper(names(data))
names(data) <- str_replace_all(names(data), "[[:punct:]]", "")
names(data) <- str_replace_all(names(data)," ", "_")
data[1] <- NULL

data <- data[!sapply(data, function(x) all(x == ""))]

date <- data %>%
  rename(DATE = DATE_OF_INJURY_RESULTING_IN_DEATH_MONTHDAYYEAR) %>%
  mutate(DATE = as.Date(DATE, "%B %d, %Y"),
         DATE = ymd(DATE),
         MONTH = as.integer(month(DATE, label = F)),
         DAY = as.integer(day(DATE)),
         YEAR = as.integer(year(DATE)))

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

@hadley
Copy link
Member

hadley commented Nov 30, 2014

Please make a minimal example that just illustrates the bug

@romainfrancois
Copy link
Member

Yep. I get that:

> sheets <- readHTMLTable(raw.data, header = T, stringsAsFactors = F, trim = T)
Erreur dans (function (classes, fdef, mtable)  :
  unable to find an inherited method for function ‘readHTMLTable’ for signature ‘"XMLDocumentContent"’

With a fresh install of XML.

Can you come up with some data without all the web scraping business.

Also, was this with the development version. My guess is that this is related to #705, as:

> nchar("DATE_OF_INJURY_RESULTING_IN_DEATH_MONTHDAYYEAR" )
[1] 46

@tyokota
Copy link
Author

tyokota commented Dec 1, 2014

I think I found the issue after creating toy examples in my attempt to replicate the error.

Here are what I believe to be the problematic variables (last 3):
[21] "DATEDESCRIPTION" ""
[23] ".1" ".2"

When I used rename() on the date variables, it worked fine. The error shows up when trying to run mutate() on the data. I guess select() worked in this case because the problematic variables were removed prior to mutate(). So I guess there's a lot more running under the hood than a transformation on a single column?

@romainfrancois
Copy link
Member

would you share this toy example of yours please ?

@hadley
Copy link
Member

hadley commented Dec 2, 2014

Can you please read http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and then create a issue?

@hadley hadley closed this as completed Dec 2, 2014
@bramtayl
Copy link

I got this same error in a very different context. It seems dplyr doesn't like variables with empty names. This seems reasonable.

data = data_frame(a = 2, b = 3)
names(data) = c("", "b")
data$c = data$b
#works
data %>% mutate(c = b)
#Error in mutate_impl(.data, dots) : attempt to use zero-length variable name

@sfirke
Copy link
Contributor

sfirke commented Jul 28, 2015

I just ran into the same issue as @bramtayl, calling mutate on a data.frame with an empty-named variable. I negative-selected that variable and that allowed the mutate call to run.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants