Back
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.
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 Position1 James Goalkeeper2 Frank Goalkeeper3 Jean Defense4 Steve Defense5 John Defense6 Tim Strikerstr(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" ...
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)ORdf[, 'Position'] <- as.factor(df[, 'Position'])
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
str(df)
$ Position: Factor w/ 3 levels "Defense","Goalkeeper",..: 2 2 1 1 1 3
31k questions
32.8k answers
501 comments
693 users