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?
farnoosh sheikhi
2013-Aug-26 19:24 UTC
[R] Loop for converting character columns to Numeric
Thanks a lot. That works great. I have another question, I will send you another email. Best,Farnoosh Sheikhi ________________________________ Cc: R help <r-help@r-project.org> Sent: Monday, August 26, 2013 12:06 PM Subject: Re: Loop for converting character columns to Numeric 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. ________________________________ 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 [[alternative HTML version deleted]]