Convert data.frame column format from character to factor

convert character to factor in r dplyr
r convert multiple columns to factor
convert character columns to factors
convert column to factor in r
tidyverse convert character to factor
convert numeric to factor in r
convert character columns to factors r
r convert all character to factor

I would like to change the format (class) of some columns of my data.frame object (mydf) from charactor 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.

Hi welcome to the world of R.

mtcars  #look at this built in data set
str(mtcars) #allows you to see the classes of the variables (all numeric)

#one approach it to index with the $ sign and the as.factor function
mtcars$am <- as.factor(mtcars$am)
#another approach
mtcars[, 'cyl'] <- as.factor(mtcars[, 'cyl'])
str(mtcars)  # now look at the classes

This also works for character, dates, integers and other classes

Since you're new to R I'd suggest you have a look at these two websites:

R reference manuals:

R Reference card:

Convert data.frame column format from , How to change characters to factors in R - 3 example codes - Convert vector, one column & all variables of a data frame - R programming data type conversion. 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)

# To do it for all names
df[] <- lapply( df, factor) # the "[]" keeps the dataframe structure
 col_names <- names(df)
# do do it for some names in a vector named 'col_names'
df[col_names] <- lapply(df[col_names] , factor)

Explanation. All dataframes are lists and the results of [ used with multiple valued arguments are likewise lists, so looping over lists is the task of lapply. The above assignment will create a set of lists that the function data.frame.[<- should successfully stick back into into the dataframe, df

Another strategy would be to convert only those columns where the number of unique items is less than some criterion, let's say fewer than the log of the number of rows as an example: <- sapply( df, function(col) length(unique(col)) < log10(length(col)) )
df[] <- lapply(df[] , factor)

Convert Character to Factor in R, How to convert character vectors, data frame columns and variables to the factor class in the Duration: 7:47 Posted: Dec 16, 2019 In above data frame, both diagnosis and param_d are character vectors. One could quickly check classes of all columns using the following command: sapply(df, class) Convert Single Column to Factor. Following is demonstrated the code samples along with help text. Pay attention that one could use lapply method to change the single column to factor.

You could use dplyr::mutate_if() to convert all character columns or dplyr::mutate_at() for select named character columns to factors:


# all character columns to factor:
df <- mutate_if(df, is.character, as.factor)

# select character columns 'char1', 'char2', etc. to factor:
df <- mutate_at(df, vars(char1, char2), as.factor)

Convert Character to Factor in R (3 Examples), How to convert a data frame variable to numeric in the R programming language. Your Duration: 4:03 Posted: Jan 6, 2020 If you want a new data frame bobc where every factor vector in bobf is converted to a character vector, try this: bobc <- rapply(bobf, as.character, classes="factor", how="replace") If you then want to convert it back, you can create a logical vector of which columns are factors, and use that to selectively apply factor

If you want to change all character variables in your data.frame to factors after you've already loaded your data, you can do it like this, to a data.frame called dat:

character_vars <- lapply(dat, class) == "character"
dat[, character_vars] <- lapply(dat[, character_vars], as.factor)

This creates a vector identifying which columns are of class character, then applies as.factor to those columns.

Sample data:

dat <- data.frame(var1 = c("a", "b"),
                  var2 = c("hi", "low"),
                  var3 = c(0, 0.1),
                  stringsAsFactors = FALSE

Convert Data Frame Column to Numeric in R, Using Column Index Numbers. In this case, we are converting first, second, third and fifth numeric variables to factor variables. mydata is a data frame. mutate_all and re-casting the data.frame after lapply will cause the data.frame's attributes to change. If you need to retain row.names, labels or other attributes associated with the underlying data.frame, try: x[, ] <- lapply(x[, ], as.character) This converts the columns to character class in place, retaining the data.frame's attributes.

Another short way you could use is a pipe (%<>%) from the magrittr package. It converts the character column mycolumn to a factor.


mydf$mycolumn %<>% factor

R : Converting Multiple Numeric Variables to Factor, The following R codes show how to convert data frame columns to numeric in the R my_data$fact <- as.numeric(as.character(my_data$fact)) # Factor column to Your browser does not currently recognize any of the video formats available. I have a data frame with factor columns, now I what convert it to numeric. So I used the How to convert the Factor column to the numeric column?

Convert Data Frame Column to Numeric in R, Description. Convert a data object to logical, integer, numeric, complex, character or factor as appropriate. S3 method for class 'data.frame' type.convert(x, . Convert Data Frame Column to Numeric in R (2 Examples) | Change Factor, Character & Integer . In this R tutorial, I’ll explain how to convert a data frame column to numeric in R. No matter if you need to change the class of factors, characters, or integers, this tutorial will show you how to do it. The article is structured as follows:

Convert Data to Appropriate Type, I have considered a data frame named 'zz'. head(zz) phenotype exclusion GSM399350 3- 4- 8- desired result. So any suggestions that can� zz <- data.frame(lapply(zz, as.character), stringsAsFactors=FALSE) You can use the following code to convert only the factor variables to character i <- sapply(zz, is.factor) zz[i] <- lapply(zz[i], as.character) You can also use the dplyr package

How to convert dataframe columns from factors to characters , To convert the columns of a data frame to the character class and then concatenate the rows of the data frame, use the following function: bob <- data.frame (lapply (bob, as.character), stringsAsFactors=FALSE) This recreates the data frame with the lapply () function with character class for all variables.

  • mydf$myfavoritecolumn <- as.factor(mydf$myfavoritecolumn)
  • Thanks! but I have another problem. I have the name of each column in an array of characters col_names[]. How can I use the above command (mydf$col_names[i]) doesn't work.
  • Any way to do this automatically for all character variables, as data.frame does it with stringsAsFactors?
  • @EtienneLow-Décarie: just unclass and use data.frame on the result,.
  • Thanks! but I have another problem. I have the name of each column in an array of characters col_names[]. How can I use the above command (neither mydf$col_names[i] nor mydf[,col_names[i]] doesn't work.)
  • @Rasoul, mydf[, col_names] will do this
  • +1 for the refs. This is basic stuff, which is OK to ask, but it's also fine to be aware of the extensive work that has been put into these (and similar) works.
  • This is a very nice solution! It can also work with column numbers which might be especially useful if you wanted to change many but not all. E.g., col_nums <- c(1, 6, 7:9, 21:23, 27:28, 30:31, 39, 49:55, 57) then df[,col_nums] <- lapply(df[,col_nums] , factor).
  • Caveat: the first solution doesn't work if length(col_names)==1. In that case, df[,col_names] is automatically demoted to a vector instead of a list of length 1, and then lapply tries to operate over each entry rather than the column as a whole. This can be prevented by using df[,col_names,drop=FALSE].
  • That's a a good point. The other invocation that would retain the list status is to use df[col_names].
  • The complete conversion of every character variable to factor usually happens when reading in data, e.g., with stringsAsFactors = TRUE, but this is useful when say, you've read data in with read_excel() from the readxl package and want to train a random forest model that doesn't accept character variables.
  • Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge.
  • pls if I want t use it for all columns of my df ?
  • I believe you need double brackets to actually extract the column and change it to a factor, e.g. [[i]]