0

I get data from an Excel file (two variables, one categorical and another one numeric), then change their types to factor and numeric accordingly:

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

Now I would like to arrange factor levels (for category) so that they appear on the plot (later) in a particular order:

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")

However, when I do so I get an error:

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

If I check levels in db_nouns$category I get one additional level called "category", i.e., R treats the name of the factor as one of levels (see line 5 below). How can I fix this?

> 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" 
divibisan
  • 11,659
  • 11
  • 40
  • 58
Alex M
  • 129
  • 1
  • 7
  • 2
    `read.table("Final_Database.txt", stringsAsFactors = T, header = T)`, does this work? – RLave Aug 29 '18 at 14:52

1 Answers1

1

Use stringsAsFactors=T when you read data and 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)
RLave
  • 8,144
  • 3
  • 21
  • 37
  • Thanks! However I've got another problem after this: when I apply levels function, R changes all original values in the variable to wrong ones (I cannot get the principle of this replacement). E.g., it was at the beginning Food 3.92 Transport 3.22 Transport 2.88 Animal 5.24 Animal 1.71 After the function was implied I've got Clothes 3.92 Space 3.22 Space 2.88 Building 5.24 Building 1.71 I.e., it's not just rearrangment but the replacement of values which destroys links between the two variables (categorical and numeric)... – Alex M Aug 29 '18 at 15:21
  • 1
    Please check my edit, see also here: https://stackoverflow.com/questions/2375587/reorder-levels-of-a-factor-without-changing-order-of-values?rq=1 – RLave Aug 29 '18 at 15:28
  • Thanks, it helps! I just used as.factor instead of factor, that was wrong! – Alex M Aug 29 '18 at 15:38