Created
April 11, 2014 04:36
-
-
Save jennybc/10441042 to your computer and use it in GitHub Desktop.
Explore unusual behaviour of a factor returned by melt.data.frame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(testthat) | |
library(reshape2) # installed from github | |
# SHA 096afb80192f163ced5d001cc416acc1d9a85d12 | |
## working with example from test-melt.r line 132 --> | |
df <- data.frame( | |
id=1:2, | |
f1=factor(c("a", "b")), | |
f2=factor(c("b", "a")) | |
) | |
## the levels, 'a' and 'b', appear in the Console | |
df | |
## now melt and preserve the factor status and levels | |
m2 <- melt(df, 1, factorsAsStrings=FALSE) | |
## the __integers__ 1 and 2 appear in the Console | |
m2 | |
## yes value IS a factor | |
expect_identical( class(m2$value), "factor" ) | |
## yes value has the right levels | |
expect_identical( levels(m2$value), c("a", "b") ) | |
## so why do the factor levels not display normally? | |
## they aren't written to file in the usual way either ... | |
write.table(m2, "yo.txt") | |
cat(readLines("yo.txt"), sep = "\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment