1

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!

smci
  • 32,567
  • 20
  • 113
  • 146
Antonio Serrano
  • 882
  • 2
  • 14
  • 27
  • You could try something along the lines of `factor(labels[nationality])` – talat Feb 14 '16 at 19:33
  • I tried but it did not work. Any other idea?? – Antonio Serrano Feb 15 '16 at 07:06
  • Provide a small reproducible example of your problem and desired output. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to create it – talat Feb 15 '16 at 08:20
  • `set.seed(1); a <- sample(1:10); b <- letters[1:10]; factor(a,labels=b)` gives `[1] c d e g b h i f j a`. Are you looking for something like this? – slamballais Feb 15 '16 at 13:10

1 Answers1

0

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])
Antonio Serrano
  • 882
  • 2
  • 14
  • 27