I am trying to reorder a factor variable that has embedded escape
characters. The data begins as a csv file with a factor that includes
embedded new line characters. By the time read.table has rendered it
into a data frame, the variable now has an extra backslash.
e.g.
"This\nLabel" in the csv becomes "This\\nLabel" in the data
frame.
So, I am trying to reorder the factor and deal with the introduction of
a secondary \ . Here's a small example:
A = c("A\\nB", "C\\nD")
test <-data.frame(A)
str(test)
test$reorderA <-factor(test$A, c("C\\nD", "A\\nB"))
str(test)
test$reorderB <-sub("\\\\n", "\n", test$reorderA)
str(test)
When sub is applied to the now-correctly ordered factor, it returns to
the default ordering.
Suggestions?
Does the following help?
A = c("A\\nB", "C\\nD")
test <-data.frame(A)
#access levels directly to change names
levels(test$A) <- sub("\\\\n", "\n", levels(test$A))
#re-order levels of the factor
test$A <- relevel(test$A, "C\nD")
Rob James wrote:> I am trying to reorder a factor variable that has embedded escape
> characters. The data begins as a csv file with a factor that includes
> embedded new line characters. By the time read.table has rendered it
> into a data frame, the variable now has an extra backslash.
>
> e.g.
> "This\nLabel" in the csv becomes "This\\nLabel" in the
data frame.
>
> So, I am trying to reorder the factor and deal with the introduction of
> a secondary \ . Here's a small example:
>
>
> A = c("A\\nB", "C\\nD")
> test <-data.frame(A)
> str(test)
> test$reorderA <-factor(test$A, c("C\\nD", "A\\nB"))
> str(test)
> test$reorderB <-sub("\\\\n", "\n", test$reorderA)
> str(test)
>
> When sub is applied to the now-correctly ordered factor, it returns to
> the default ordering.
>
> Suggestions?
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
On Dec 10, 2010, at 3:14 PM, Rob James wrote:> I am trying to reorder a factor variable that has embedded escape > characters. The data begins as a csv file with a factor that > includes embedded new line characters. By the time read.table has > rendered it into a data frame, the variable now has an extra > backslash. > > e.g. > "This\nLabel" in the csv becomes "This\\nLabel" in the data frame. > > So, I am trying to reorder the factor and deal with the introduction > of a secondary \ . Here's a small example: > > > A = c("A\\nB", "C\\nD") > test <-data.frame(A) > str(test) > test$reorderA <-factor(test$A, c("C\\nD", "A\\nB")) > str(test) > test$reorderB <-sub("\\\\n", "\n", test$reorderA) > str(test) > > When sub is applied to the now-correctly ordered factor, it returns > to the default ordering.Actually it returns a character vector rather than a factor.> > Suggestions?Perhaps you should ask a question. -- David Winsemius, MD West Hartford, CT