Factors not numbered correctly

I wrote these lines of code, but the console outcome is incorrect:

brazil <- data.frame(name = "Brazil", continent = "South-America")
countries <- data.frame(name = "Spain", continent = "Europe")

str(rbind(brazil, countries))
'data.frame': 2 obs. of 2 variables:

$ name : Factor w/ 2 levels "Brazil","Spain": 1 2

$ continent: Factor w/ 2 levels "South-America",..: 1 2

Shouldn't the console display the elements in a factor ordered alphabetically, and display the integer values following the order of the elements as they appear in the factor? So, the structure of the continent factor should be, in my opinion:
$ continent: Factor w/ 2 levels "Europe",..: 2 1

Should it not??? If not, why is the console outputting as such?

It seems that when you combine data frames with rbind() the factors from the second data frame get appended to the end of the levels in the first data frame. The whole set of values is not reevaluated.

brazil <- data.frame(name = "Brazil", continent = "South-America", stringsAsFactors = TRUE)
countries <- data.frame(name = "Spain", continent = "Europe", stringsAsFactors = TRUE)

AllDat <- rbind(brazil, countries)
#> [1] "South-America" "Europe"

AllDat2 <- rbind(countries, brazil)
#> [1] "Europe"        "South-America"

DF1 <- data.frame(name = c("A","C","E"), stringsAsFactors = TRUE)
DF2 <- data.frame(name = c("B","D","F"), stringsAsFactors = TRUE)
AllDat3 <- rbind(DF1, DF2)
#> [1] "A" "C" "E" "B" "D" "F"

AllDat4 <- rbind(DF2, DF1)
#> [1] "B" "D" "F" "A" "C" "E"

Created on 2024-02-28 with reprex v2.0.2

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.