Convert data.frame column format from character to factor

181    Asked by Aalapprabhakaran in Python , Asked on Apr 10, 2021

description: I would like to change the format (class) of some columns of my data.frame object (mydf) from character to factor.

I don't want to do this when I'm reading the text file by read.table() function.

How can I do this ?.

Answered by Bruce Benedict

To convert character to factor r, you can use the following functions:

df <- data.frame(Name = c("James","Frank","Jean","Steve","John","Tim"),
                       Position = c("Goalkeeper","Goalkeeper","Defense","Defense","Defense","Striker"), stringsAsFactors = FALSE)
 head(df)
   Name Position
1 James Goalkeeper
2 Frank Goalkeeper
3 Jean Defense
4 Steve Defense
5 John Defense
6 Tim Striker
str(df) #to see the structure of data frame ‘df’
'data.frame': 6 obs. of 2 variables:
 $ Name : chr "James" "Frank" "Jean" "Steve" ...
 $ Position: chr "Goalkeeper" "Goalkeeper" "Defense" "Defense" ...
To convert the ‘Position’ column from the above data frame containing character values to a factor:
df$Position = as.factor(df$Position)
OR
df[, 'Position'] <- as.factor(df[, 'Position'])
Output:
str(df)
'data.frame': 6 obs. of 2 variables:
 $ Name : chr "James" "Frank" "Jean" "Steve" ...
 $ Position: Factor w/ 3 levels "Defense","Goalkeeper",..: 2 2 1 1 1 3

Your Answer

Interviews

Parent Categories