Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a data frame with numeric and character columns?

I'm trying to build a data frame consisting of three character variables and one numeric variable. When I run the following code, I get a four-column matrix, but the score variable is no longer numeric, and the scores are treated as factors.

school<-c("NYU", "BYU", "USC", "FIT", "UNH","UCLA","USF","Columbia")
state<-c("NY","UT","CA","NY","NY","CA", "CA","NY")
measure<-c("MSAT","MSAT","GPA","MSAT","MSAT","GPA","GPA","GPA")
score<-c(500, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
data<-cbind(school,state, measure,score)

If I run

data1<-data.frame(cbind(school,state, measure,score))

I get a data frame where score is still a factor. How can I build this data frame so that score is numeric?

like image 410
user3614783 Avatar asked Jul 23 '14 03:07

user3614783


2 Answers

To build a data frame consisting of three character variables and one numeric variable, you need to specify: stringsAsFactors=FALSE in data.frame() function:

school<-c("NYU", "BYU", "USC")
state<-c("NY","UT","CA")
measure<-c("MSAT","MSAT","GPA")
score<-c(500, 490, 2.9)
df<-data.frame(school,state, measure,score,  stringsAsFactors=FALSE)

Here's the result:

summary(df)
school             state             measure              score      
Length:3           Length:3           Length:3           Min.   :  2.9  
Class :character   Class :character   Class :character   1st Qu.:246.4  
Mode  :character   Mode  :character   Mode  :character   Median :490.0  
                                                         Mean   :331.0  
                                                         3rd Qu.:495.0  
                                                         Max.   :500.0  
like image 99
Farid Khafizov Avatar answered Nov 14 '22 07:11

Farid Khafizov


 school<-c("NYU", "BYU", "USC", "FIT", "UNH","UCLA","USF","Columbia")
 state<-c("NY","UT","CA","NY","NY","CA", "CA","NY")
 measure<-c("MSAT","MSAT","GPA","MSAT","MSAT","GPA","GPA","GPA")
 score<-c(500, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
 data<-data.frame(school,state, measure,score)
 data
   school state measure score
     NYU    NY    MSAT 500.0
     BYU    UT    MSAT 490.0
     USC    CA     GPA   2.9
     FIT    NY    MSAT 759.0
     UNH    NY    MSAT 550.0
    UCLA    CA     GPA   1.2
     USF    CA     GPA   3.1
Columbia    NY     GPA   3.2

is.numeric(data$score)
[1] TRUE

I guess This should work !!!

like image 36
nopainnogain Avatar answered Nov 14 '22 08:11

nopainnogain