Convert Character to Factor in R (3 Examples) | Vector, Data Column & Variables | as.factor Function

preview_player
Показать описание
R programming code:

##### Example 1
vec <- c("A", "B", "A", "D", "C", "B") # Create example vector

class(vec) # Check class of vector

class(vec_updated)

##### Example 2
x2 = letters[1:5],
x3 = c(4, 1, 5, 3, 1),
x4 = c("Male", "Female", "Male", "Male", "Female"),
stringsAsFactors = FALSE)
data # Print example data frame

data2 <- data # Replicate example data frame

class(data2$x2) # Check class of second column

class(data2$x2) # Check class of second column

##### Example 3
data3 <- data # Replicate example data frame

str(data3) # Check class of all columns

stringsAsFactors = TRUE)

str(data3) # Check class of all columns
Рекомендации по теме
Комментарии
Автор

Thanks for the very clear and complete demonstration.

sksbasnayake
Автор

DANKE - hat mir beim Datentyp konvertieren von Character in Factor geholfen...BG

friedrichadam
Автор

Once more, just the video I was looking for!
Thanks !!

agsoutas
Автор

Good example data, just what I needed, thank you!

shanehixson
Автор

The last piece of info was the one I was looking for, thanks. I would however add that it didn't work for me until I added "stringsAsFactors = TRUE" to the as.data.frame function. Apparently it is not the default (any more?) for it to turn characters into factors.

hoppareiter
Автор

Thanks for this video! I followed along and it worked for me but only for the first 7 variables. I wonder if this also has something to do with the fact that when I used the str() function, I could also only see 7 variables before [list output truncated]. As my dataset has hundreds of variables, is there any workaround for this? Thank you!

scarletts
Автор

Hi there, I followed the entire tutorial. Cannot seem to make it work for my data :(

jetindersingh
Автор

Hi, I could not make it work. It remains as before, i.e int remains int and chr remain chr

tmitra
Автор

Hallo Joachim, I have a small question. I have some variables like sex and working status and they are coded as integer like (1, 2) for male and female and (1, 2, 3, 4) for the observation as employed, retired, studen or unemployed. I tried to use table function and other functions for descriptive statistics and thee give me the head of the results as numbers which is difficult to explain. I wanted to change it to (Male, Female) for this purpose but it doesn't work. I used the following codes to create a new variable but I couldn't make it unfortunately

"NA"] <- NA
"1"] <- Male
"2"] <- Female

Sex is my new nominal variable and 260 is the old one

Thank you soooo much

mahmoudmoustafamohammed