R - 使用另一个向量替换因子的级别

时间:2016-02-14 19:30:53

标签: r r-factor

我有一个名为" nationality"这表明受访者'我的数据框(df)中的国籍。然而,问题在于它当前是一个从1到193的整数向量。我有另一个行向量叫做"标签"每个国籍的标签(即第一栏说"阿富汗",第二栏"阿尔巴尼亚"等)。我想做的是改变国籍"矢量到一个因子并用标签替换其数值。我试过这个:

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

但它不起作用:(

请帮忙。提前谢谢!

1 个答案:

答案 0 :(得分:0)

我做到了!但是我必须采取中间步骤并将手动保存193个国籍标签的文件作为xlsx文件。这是我的解决方案:

## 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])