How to classify a "labelled" variable and assign new labels?

Asked

Viewed 227 times

0

My question is very basic and is this: I have a variable "labelled" with ten levels and different Labels and I would like to aggregate two to two their levels and assign new labels to each of the levels. how can I do this?

  • 2

    Check out this package: https://github.com/tidyverse/forcats To convert from labelled for factor you will need the function as_factor package haven.

1 answer

2

It may not be the best code, but do the task:

niveis <- data.frame(Antigo = letters[1:10])
a <- data.frame(Niveis = niveis,
              Novo = paste0(rep("NV", 10), rep(1:5,each = 2)))
merge(niveis, a, by = "Antigo", all.x = T)

   Antigo  Novo
        a  NV1
        b  NV1
        c  NV2
        d  NV2
        e  NV3
        f  NV3
        g  NV4
        h  NV4
        i  NV5
        j  NV5

The best code will depend on whether your variable is in a data frame or not

Browser other questions tagged

You are not signed in. Login or sign up in order to post.