1

I have a dataframe with a column values as - "a, a, a, b, b, b, happy, sad, angry".

I want to convert the column to a factor using as.factor.

However I was wondering, is there possibly that certain values of columns can be grouped together as one level of the factor? Like, 'a, b' as one level of the factor, 'happy' as another level and so on?

How is it possible in code?

EDIT -

I tried to use:

allData$label <- factor(allData$label,
                        levels = c(1,2,3,4),
                        labels = c((c("a","b")),
                                   "happy", "sad", "angry"))

Since I wanted chars 'a' and 'b' as one label so I put a vector inside a vector. But it's giving me errors.

Phil
  • 7,287
  • 3
  • 36
  • 66
Dennis Main
  • 133
  • 6

1 Answers1

3

Yes. Use the labels option:


x <- c("a","a","b","b","happy", "sad", "angry")
levels = c("a", "b", "happy", "sad", "angry")
labels = c("letter", "letter", "happy", "sad", "angry")

y <- factor(x, levels, labels = labels)

y

https://rdrr.io/r/base/factor.html

"Duplicated values in labels can be used to map different values of x to the same factor level."

EDIT: Your mistake in the above code example is the nested vector.

  • Hey @Jonathan Graves, thanks for the answer. I used that and then used str() on my dataframe. On using it, I found this: $ label : Factor w/ 4 levels "letter","sad",..: NA NA NA NA NA NA NA NA NA NA ... Is this normal for NA values to show up? – Dennis Main Apr 27 '22 at 20:32
  • 1
    No, it's not - you need to make sure that the `levels` vector has an _exact_ list of all of the valid levels in your dataset, and that for each value there's a corresponding entry in `labels`. It's likely you didn't exactly specify them, if I had to guess. – Jonathan Graves Apr 27 '22 at 20:36
  • I fixed it, thank you! Didn't specify them all – Dennis Main Apr 27 '22 at 20:46