What is the opposite of the cut() function?

When using the code below, I change a quantitative variable to a categorical variable. BUT, the new variable replaces the old quantitative variable, as I am using the cut() command.

HDI$value <- cut(HDI$value, seq(0, 1, .33), labels = c(1,2,3))

Can someone please tell me how to perform the same task of changing the quantitative variable to a categorical variable but ADD the new variable as a column to the data frame AND keep the existing quantitative variable column.

Many thanks,
Tye

If you converted a numeric variable to a group variable, whatever be its label, you're loosing information. You cannot go back to original information only from the newly available information.

In the first step, you could do something like this:

data_set$grouped_value <- cut(data_set$numeric_value, vector_of_breaks, labels = vector_of_labels)

Hope this helps.


@TyeGalloway, did you try my solution? It'll do what you want.

mutate isn't magic. It won't be able to get information which is lost. But if you are tidyverse only person, here's the equivalent line of my above code with mutate and pipe operator:

data_set %>%
    mutate(grouped_value = cut(numeric_value, vector_of_breaks, labels = vector_of_labels))

And, this is not correct totally.

It depends on the name you use. If you use same name, it'll override. The problem with your code in original problem is that only.

1 Like

Hi @Yarnabrina,
Thank you so much for taking the time to help with my question.
I am really after something like the mutate() function, whereby the original columns remain but a new computed column is added.
Can you help with this?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.