I seem to spend a lot of time creating a dataframe from a file, database or something, and then converting each column into the type I wanted it in (numeric, factor, character etc). Is there a way to do this in one step, possibly by giving a vector of types ?
foo<-data.frame(x=c(1:10),
y=c("red", "red", "red", "blue", "blue",
"blue", "yellow", "yellow", "yellow",
"green"),
z=Sys.Date()+c(1:10))
foo$x<-as.character(foo$x)
foo$y<-as.character(foo$y)
foo$z<-as.numeric(foo$z)
instead of the last three commands, I’d like to do something like
foo<-convert.magic(foo, c(character, character, numeric))
Edit See this related question for some simplifications and extensions on this basic idea.
My comment to Brandon’s answer using
switch:For truly large data frames you may want to use
lapplyinstead of theforloop:When doing this, be aware of some of the intricacies of coercing data in R. For example, converting from factor to numeric often involves
as.numeric(as.character(...)). Also, be aware ofdata.frame()andas.data.frame()s default behavior of converting character to factor.