R将因子名称视为一个级别

问题描述 投票:0回答:1

我从Excel文件中获取数据(两个变量,一个是分类的,另一个是数字),然后相应地将它们的类型更改为因子和数字:

setwd("D:/Desktop/")
db_nouns = read.table ("Final_Database.txt")
db_nouns = db_nouns [2:507,]
colnames (db_nouns) = c ("category", "space")
db_nouns$category = as.factor (db_nouns$category)
db_nouns$space = as.numeric(as.character(db_nouns$space))

现在我想安排因子水平(对于类别),以便它们以特定顺序出现在图(稍后)上:

levels (db_nouns$category) = c( "Ground", "Building", "Tool_precise_grip", "Tool_power_grip", "Food", "Clothes", "Animal", "Object", "Transport", "Action", "Body_Part", "Sense_Phys", "Sound", "Sense_Emotion", "Intelligence", "Space")

但是,当我这样做时,我收到一个错误:

*Error in `levels<-.factor`(`*tmp*`, value = c("Ground", "Building", "Tool_precise_grip",  : 
  number of levels differs*

如果我检查db_nouns $ category中的级别,我会得到一个名为“category”的附加级别,即R将该因子的名称视为一个级别(参见下面的第5行)。我怎样才能解决这个问题?

> levels (db_nouns$category)
 [1] "Action"            "Animal"            "Body_Part"         "Building"         
 [5] "Category"          "Clothes"           "Food"              "Ground"           
 [9] "Intelligence"      "Object"            "Sense_Emotion"     "Sense_Phys"       
[13] "Sound"             "Space"             "Tool_power_grip"   "Tool_precise_grip"
[17] "Transport" 
r r-factor
1个回答
1
投票

阅读数据和stringsAsFactors=T时使用header = T

db_nouns <- read.table("Final_Database.txt", stringsAsFactors = T, header = T)

colnames(db_nouns) <- c ("category", "space")

new_order <- c( "Ground", "Building", "Tool_precise_grip", "Tool_power_grip", "Food", "Clothes", "Animal", "Object", "Transport", "Action", "Body_Part", "Sense_Phys", "Sound", "Sense_Emotion", "Intelligence", "Space")

db_nouns$category <- factor(db_nouns$category, levels = new_order)
© www.soinside.com 2019 - 2024. All rights reserved.