Re: [R] convert columns of dataframe to same factor levels

2018-12-19 Thread William Dunlap via R-help
You can abuse the S4 class system to do this. setClass("Size") # no representation, no prototype setAs(from="character", to="Size", # nothing but a coercion method function(from){ ret <- factor(from, levels=c("Small","Medium","Large"), ordered=TRUE) class(ret) <- c("Size", class(ret))

Re: [R] convert columns of dataframe to same factor levels

2018-12-19 Thread Duncan Murdoch
On 19/12/2018 6:48 AM, Luigi Marongiu wrote: Thank you, that worked fine for me. Best wishes of merry Christmas and happy new year, Luigi Actually it's wrong! Sorry about that. If you look at my.data.new$column_2, you'll see that the levels have changed: > my.data column_1 column_2

Re: [R] convert columns of dataframe to same factor levels

2018-12-19 Thread Luigi Marongiu
Thank you, that worked fine for me. Best wishes of merry Christmas and happy new year, Luigi On Wed, Dec 19, 2018 at 12:19 PM Duncan Murdoch wrote: > > On 19/12/2018 5:58 AM, Luigi Marongiu wrote: > > Dear all, > > I have a data frame with character values where each character is a > > level;

Re: [R] convert columns of dataframe to same factor levels

2018-12-19 Thread Duncan Murdoch
On 19/12/2018 5:58 AM, Luigi Marongiu wrote: Dear all, I have a data frame with character values where each character is a level; however, not all columns of the data frame have the same characters thus, when generating the data frame with stringsAsFactors = TRUE, the levels are different for

[R] convert columns of dataframe to same factor levels

2018-12-19 Thread Luigi Marongiu
Dear all, I have a data frame with character values where each character is a level; however, not all columns of the data frame have the same characters thus, when generating the data frame with stringsAsFactors = TRUE, the levels are different for each column. Is there a way to provide a single