notuo notuo - 1 year ago 62
R Question and cbind results in factor columns

I have a big data.frame with a mix of integer, character and strings columns. I'll need to order the data.frame by a numeric column.

When I combine the original columns into a data.frame all the columns change to factor, including the column I need for the sort. So the sort gives something like

1, 10, 100...
instead of
1, 2, 3...

Here is an example of my problem.

a <- 1:10
b <- c(1,3,5,6,2,10,100,110,7,4)
c <- LETTERS[1:10]
d <-, b, c)) # I am using this construction
e <- d[with(d, order(b)), ]

How can I fix this?

Answer Source

Actually you need to do:

d <- data.frame(a, b, c, stringsAsFactors=FALSE)

The last part stringsAsFactors=FALSE prevents column d$c from being converted to factors. Include it, and your strings will stay as strings.

Don't forget stringsAsFactors=FALSE - it will save you untold misery, trust me!