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

ldply fails if .fun gives NA as a column name #66

Closed
scottkosty opened this issue Nov 7, 2011 · 2 comments
Closed

ldply fails if .fun gives NA as a column name #66

scottkosty opened this issue Nov 7, 2011 · 2 comments

Comments

@scottkosty
Copy link
Contributor

ldply has trouble when .fun passes a return with NA as a column name. The following shows a fix for the make_names function in helper-quick-df.r. I did not test, but I do not think there will be any difference in computation time, especially since the modification is executed on a primitive function, sum.

Example data

test_df <-
structure(list(health = structure(c(3L, 2L, 3L, 2L, 3L, 1L, 2L,
2L, 2L, 1L, NA, NA, 2L, 2L, 2L, 3L), .Label = c("very good",
"good", "fair", "poor", "very poor"), class = "factor"), age = c(30,
30, 30, 30, 30, 30, 30, 30, 30, 40, 40, 40, 40, 40, 40, 40)), .Names = c("health",
"age"), row.names = c("760", "761", "762", "763", "764", "765",
"766", "767", "768", "769", "770", "771", "772", "773", "774",
"775"), class = "data.frame")

before patch:

> ddply(test_df, "age", .fun= function(x) table(x$health, useNA="always"))
Error in seq_len(sum(nm == "")) :
  argument must be coercible to non-negative integer

after patch:

> ddply(test_df, "age", .fun= function(x) table(x$health, useNA="always"))
age very good good fair poor very poor NA
1  30         1    5    3    0         0  0
2  40         1    3    1    0         0  2

patch:

--- helper-quick-df.r   2011-10-31 01:40:53.000000000 -0400
+++ helper-quick-df.r.sk        2011-11-06 20:11:04.074707068 -0500
@@ -24,6 +24,6 @@
     nm <- rep("", length = length(x))
   }

-  nm[nm == ""] <- paste(prefix, seq_len(sum(nm == "")), sep = "")
+  nm[nm == ""] <- paste(prefix, seq_len(sum(nm == "", na.rm = TRUE)), sep = "")
   nm
-}
\ No newline at end of file
+}

Scott

@hadley
Copy link
Owner

hadley commented Nov 7, 2011

Thanks Scott - would mind having a go at creating a formal pull request? That way you could also include a test and a comment in the NEWS file describing the improvement.

@scottkosty
Copy link
Contributor Author

Sure, Hadley -- I'll do that within a week.

@hadley hadley closed this as completed in bac3768 Oct 8, 2012
hadley added a commit that referenced this issue Mar 30, 2015
jjallaire/rcpp-gallery/#66
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants