Explore Courses Blog Tutorials Interview Questions
0 votes
in R Programming by (5.3k points)

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.

Any help would be appreciated.

1 Answer

0 votes

To convert data frame columns from character to factor, 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)


   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)


df[, 'Position'] <- as.factor(df[, 'Position'])



'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

Browse Categories