[R] Char to Numeric --- Type Conversion

arun smartpink111 at yahoo.com
Tue Apr 1 21:18:13 CEST 2014


Hi,
May be this helps:
set.seed(445) 

dat1 <- as.data.frame(matrix(sample(seq(2,4,by=0.5),80,replace=TRUE),ncol=20),stringsAsFactors=FALSE) dat1[dat1==2] <- ""
dat1[,sapply(dat1,is.character)] <- lapply(dat1[,sapply(dat1,is.character)] ,as.numeric)
identical(sum(sapply(dat1,is.numeric)), ncol(dat1))
#[1] TRUE 
A.K.


Hi All, I have a dataframe with 100 columns. In some of the columns, the values are of type CHAR.
I wanted to do type conversion for the whole table, wherever I find the values to be char into Num. Regards,
Praveen 





More information about the R-help mailing list