Question

When converting a numeric value to a factor, can I specify the level values at which the conversion occurs? Is it possible to designate a special level as "too large" or say "5+" without defining a(n inline) function?

For example:

c(1,2,20,3,10)

would be converted to:

factor(c("1","2","many","3","many"))
Was it helpful?

Solution

Yes, you can use cut, e.g.,

v = c(1,2,20,3,10)
cut(v, c(0:5, Inf), labels = c(1:5, "many"), right = T, include.lowest = T)

yields

 [1] 1    2    many 3    many
 Levels: 1 2 3 4 5 many

cut has a number of additional parameters to control how values get placed in the bins, so read the help there.

OTHER TIPS

Well, a very simple way would be to do something along the lines of

x = c(1,2,20,3,10)
f = x
f[f>5] = "many"
f = factor(f)

There is probably a more elegant way to do this specifying something in the levels parameter of the factor function. Still playing with this.

I don't think that it is reasonable to require it to be done "without defining a(n inline) function". However,

x<-c(1,2,20,3,10)
factor("[<-"(x, x>9, "many")) 

... does it as there is already such a function ([<-).

[[edit]] The solution with cut above seems more readable.

alternatively:

factor(ifelse(x>9, "many", x))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top