【问题标题】:R considers factor name as a levelR 将因子名称视为一个级别
【发布时间】:2018-08-29 14:50:39
【问题描述】:

我从 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 中的级别,我会得到一个额外的级别,称为“类别”,即 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" 

【问题讨论】:

  • read.table("Final_Database.txt", stringsAsFactors = T, header = T),这行得通吗?

标签: r r-factor


【解决方案1】:

读取数据时使用stringsAsFactors=Theader = 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)

【讨论】:

  • 谢谢!但是,在此之后我遇到了另一个问题:当我应用级别函数时,R 将变量中的所有原始值更改为错误值(我无法获得这种替换的原理)。例如,一开始是 Food 3.92 Transport 3.22 Transport 2.88 Animal 5.24 Animal 1.71 在函数被暗示之后,我得到了 Clothes 3.92 Space 3.22 Space 2.88 Building 5.24 Building 1.71 即,它不仅仅是重新排列,而是破坏链接的值的替换在两个变量(分类和数字)之间...
  • 请检查我的编辑,也可以在这里查看:stackoverflow.com/questions/2375587/…
  • 谢谢,有帮助!我只是用 as.factor 而不是 factor,这是错误的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 2014-04-12
  • 1970-01-01
  • 2018-07-08
  • 2015-06-25
  • 1970-01-01
  • 2017-08-04
相关资源
最近更新 更多