Say I have a data file in transposed form (i.e. rows are variables, columns are observations), like this:
name A B C
gender M F M
age 25 26 27
I read the file into R: dat <- read.table(datafile, row.names=1, as.is=TRUE)
. Since data.frame needs values of homogeneous type in each column, the "age" row is coerced into characters.
Then I would transpose dat
back to "normal" form: dat_t <- t(dat)
. Now "age" is a column but the values are still characters.
Now my source data is large with many rows that should be numeric interspersed among many character rows. So after the transposition how can I easily convert the types of all the columns to what they should be?
Thank you. This is my first question so I'm not very good at searching for previous answers or asking proper questions. I apologize in advance if the question is trivial or duplicated.