Intellipaat Back

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

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
by
edited by

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)

 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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...