Skip to content

Commit

Permalink
Fix bug in nchar when dealing with factors
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Nov 11, 2009
1 parent 673c9f3 commit 799f6e3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
stringr 0.2
===========

* str_length now returns correct results when used with factors
1 change: 1 addition & 0 deletions R/basics.r
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' str_length(letters)
#' str_length(c("i", "like", "programming", NA))
str_length <- function(string) {
string <- as.character(string)
nc <- nchar(string)
is.na(nc) <- is.na(string)
nc
Expand Down
4 changes: 4 additions & 0 deletions tests/test-length.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ context("String length")
test_that("length of missing string is missing", {
expect_that(str_length(NA), equals(NA_integer_))
expect_that(str_length("NA"), equals(2))
})

test_that("works with factors", {
expect_that(str_length(factor("abc")), equals(3))
})

0 comments on commit 799f6e3

Please sign in to comment.