R 将因子名称视为一个级别
R considers factor name as a level
我从 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"
读取数据时使用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)
我从 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"
读取数据时使用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)