Convert factor to numeric or character

Original by Stackoverflow.com, 2013 

This summary note was Posted on

Useful when merging/joining tables in order to avoid warnings

Simplest way:

 
Using dplyr

bob <- transform(bob, Var1 = as.character(Var1))

Avoid creating the factors in the first place

bob <- as.data.frame(cbind(Var1,Var2), stringsAsFactors=FALSE)

Other methods

  • using the function map_if from the package Purrr
  • using the lapply function
  • converting the levels directy:  levels(Var1)[as.numeric(Var1)]
  • using the function mutate_if from dplyr  (vesion >= 0.5.0)