Search code examples
rr-factor

R - Replacing levels of a factor using another vector


I have a vector called "nationality" that indicates respondents' nationality in my data frame (df). The problem, however, is that it is currently an integer vector that goes from 1 to 193. I have another row vector called "labels" with the labels of each nationality (i.e. the first column says "Afghan", the second "Albanian", etc.). What I want to do is transforming "nationality" vector into a factor and replacing its numeric values with labels. I tried this:

df$nationality <- as.factor(df$nationality)
labels2 <- names(labels)
levels(df$nationality) <- labels2

But it does not work :(

Help, please. Thanks in advance!


Solution

  • I did it! But I had to take an intermediate step and save the file with the 193 nationality labels manually as a xlsx file. Here is my solution:

    ## Creating data frame with 5 respondents and its corresponding nationalities (dim 5 x 2):
    
    df <- data.frame(respondentId = c(1, 2, 3, 4, 5), nationality = c(166, 91, 4, 49, 128))
    
    ## Downloading nationality labels from guavastudios.com:
    
    fileUrl <- "http://www.guavastudios.com/downloads/nationalities/nationalities.txt"
    download.file(fileUrl, destfile= "./nationalities.txt", method = "curl")
    
    ## Then I copied nationalities.txt to one column in Excel and saved the xlsx file. It
    # contains 193 rows (or labels for 193 different nationalities).
    
    ## Loading xlsx package. If you do not have it installed, first type install.packages("xlsx").
    
    library(xlsx)
    
    ## Reading the xlsx file and saving it as an object in R called "labelsNAtion":
    
    labelsNation <- read.xlsx("./nationalities.xlsx", sheetIndex = 1, header = FALSE)
    
    ## Replacing numbers for nationality labels in the second column of df:
    
    df$nationality <- factor(df$nationality, levels=c(1:193), labels = labelsNation[,1])