Suppose I have a data.frame that’s completely numeric. If I make one entry of the first column a character (for example), then the entire first column will become character.
Question: How do I reverse this. That is, how do I make it such that any character objects inside the data.frame that are “obviously” numeric objects are forced to be numeric?
MWE:
test <- data.frame(matrix(rnorm(50),10))
is(test[3,1])
test[1,1] <- "TEST"
is(test[3,1])
print(test)
So my goal here would be to go FROM the way that test is now, TO a state of affairs where test[2:10] is numeric. So I guess I’m asking for a function that does this over an entire data.frame.
Short answer is you cannot.
As was mentioned in the comments, in a data frame, all elements of a column must have the same mode.
If you would like to specifically find the values that are “number like” you can use the following (where
vechere would be, say, a data frame column)You can then convert these, but unfortunately you cannot put the converted values back into the same column. As as you do, they will be coerced back to
characterAs for a function that can convert the whole dataframe to numeric (realizing that isolating specific entries as exceptions is not possible), you can use
sapply