koteletje
koteletje

Reputation: 679

Converting numeric values of multiple columns to factor levels that are consecutive integers in (descending) order

Consider the data frame c containing numeric values:

a = c(0, 1, 3, 5, 6, 0, 1, 3, 6)
b = c(2, 2, 4, 6, 7, 1, 1, 3, 7)
c = data.frame(var1 = a, var2 = b)

I would like to convert every column to factor levels as follows (it's important that the lowest level is 1 and not 0):

levels(c$var1) = length(unique(c$var1)):1
levels(c$var2) = length(unique(c$var2)):1

The number of columns for which I have to do this can vary so I'd like to further automate this. I can do this with a loop but is there a way of doing this for without a loop? I don't see how I can do this with apply, lapply or even catcolwise (from plyr) because of the function length(unique()) is each time applied to a different column.

Upvotes: 1

Views: 132

Answers (1)

akrun
akrun

Reputation: 886948

We can use lapply to loop over the columns and assign the levels as the reverse sequence of length of unique elements in the column and assign the output back to the dataset

c[] <-lapply(c, function(x) {levels(x) <- length(unique(x)):1; x})

Upvotes: 1

Related Questions