How to Use stringsAsFactors in R

stringsAsFactors decides the strings will be considered as factors or just strings or characters. The following uses two examples to explains the difference.

Example 1: stringsAsFactors= FALSE

The following sets stringsAsFactors= FALSE. We can see that the data type of the column of Names is characters. (chr represents characters.)

# Create a dataframe in R, setting stringsAsFactors= FALSE
df <- data.frame(Names = c('Jack','Jess','Jacob'),   
                 Ages = c(32,22,44),
                 stringsAsFactors = FALSE)

# print out the dataframe
print(df)

# check the datatype in the dataframe
str(df)    

Output:

  Names Ages
1  Jack   32
2  Jess   22
3 Jacob   44

'data.frame':	3 obs. of  2 variables:
 $ Names: chr  "Jack" "Jess" "Jacob"
 $ Ages : num  32 22 44

Example 2: stringsAsFactors= TRUE

The following sets stringsAsFactors= TRUE. We can see that the data type of the column of Names is Factor now.


# Create a dataframe in R, setting stringsAsFactors= TRUE
df <- data.frame(Names = c('Jack','Jess','Jacob'),   
                 Ages = c(32,22,44),
                 stringsAsFactors = TRUE)

# print out the dataframe
print(df)

# check the datatype in the dataframe
str(df)   

Output:

  Names Ages
1  Jack   32
2  Jess   22
3 Jacob   44

'data.frame':	3 obs. of  2 variables:
 $ Names: Factor w/ 3 levels "Jack","Jacob",..: 1 3 2
 $ Ages : num  32 22 44

Further Reading