[R] Loop for converting character columns to Numeric

arun smartpink111 at yahoo.com
Mon Aug 26 21:06:13 CEST 2013



Hi,
Suppose you created a dataframe like this:
set.seed(28)
 dat1<-as.data.frame(simplify2array(list(letters[1:5],sample(1:20,5,replace=TRUE),6:10)),stringsAsFactors=FALSE)
 str(dat1)
#'data.frame':    5 obs. of  3 variables:
# $ V1: chr  "a" "b" "c" "d" ...
# $ V2: chr  "1" "2" "10" "18" ...
# $ V3: chr  "6" "7" "8" "9" ...

dat1[,2:3]<- lapply(dat1[,2:3],as.numeric)
 str(dat1)
#'data.frame':    5 obs. of  3 variables:
# $ V1: chr  "a" "b" "c" "d" ...
# $ V2: num  1 2 10 18 2
# $ V3: num  6 7 8 9 10

A.K.
________________________________
From: farnoosh sheikhi <farnoosh_81 at yahoo.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> 
Sent: Monday, August 26, 2013 1:48 PM
Subject: Loop for converting character columns to Numeric





Hi there,

I want to change the columns of my data from Character to numeric.
Since the number of predictors are more than 100, I need to write a loop for it.
Would you please help me to do that?
Thanks tons.

Best,Farnoosh Sheikhi 



More information about the R-help mailing list