Dambo
Dambo

Reputation: 3486

How to refactor a vector?

I have this vector

v <- c("firstOne","firstTwo","secondOne")

I would like to factor the vector assigning c("firstOne","firstTwo) to the same level (i.e., firstOne). I have tried this:

> factor(v, labels = c("firstOne", "firstOne", "secondOne"))
[1] firstOne  firstOne  secondOne
Levels: firstOne firstOne secondOne

But I get a duplicate factor (and a warning message advising not to use it). Instead, I would like the output to look like:

[1] firstOne  firstOne      secondOne
Levels: firstOne secondOne

Is there any way to get this output without brutally substituting the character strings?

Upvotes: 2

Views: 8971

Answers (4)

Dambo
Dambo

Reputation: 3486

Eventually I also found a solution which looks somehow sloppy but I don't see major issues (looking forward to listen which might be possible problems with this tho):

v <- c("firstOne","firstTwo","secondOne")
factor(v)
factor(factor(v,labels = c("firstOne","firstOne","secondOne")))

Upvotes: 0

Daniel
Daniel

Reputation: 7832

You could use the rec-function of the sjmisc-package:

rec(v, "firstTwo=firstOne;else=copy", as.fac = T)

> [1] firstOne  firstOne  secondOne
> Levels: firstOne secondOne

(the output is shortened; note that the sjmisc-package supports labelled data and thus adds label attributes to the vector, which you'll see in the console output as well)

Upvotes: 0

Ernest A
Ernest A

Reputation: 7839

A factor is just a numeric (integer) vector with labels, and so manipulating a factor is equivalent to manipulating integers, rather than character strings. Therefore performance-wise is perfectly OK to do

f <- as.factor(v)
f[f %in% c('firstOne', 'firstTwo')] <- 'firstOne'
f <- droplevels(f)

Upvotes: 2

alexwhitworth
alexwhitworth

Reputation: 4907

Here are a couple of options:

v <- factor(ifelse(v %in% c("firstOne", "firstTwo"), "firstOne", "secondOne"))
v <- factor(v,levels = c("firstOne","secondOne")); f[is.na(f)] <- 'firstOne'

Upvotes: 2

Related Questions