如何使用数字和字符列创建数据框?

时间:2014-07-23 03:13:01

标签: r dataframe numeric cbind

我正在尝试构建一个由三个字符变量和一个数字变量组成的数据框。当我运行以下代码时,我得到一个四列矩阵,但得分变量不再是数字,并且分数被视为因子。

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)

如果我跑

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

我得到一个数据框,score仍然是一个因素。如何构建此数据框以使score为数字?

2 个答案:

答案 0 :(得分:4)

要构建由三个字符变量和一个数字变量组成的数据框,您需要在stringsAsFactors=FALSE函数中指定:data.frame()

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)

结果如下:

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  

答案 1 :(得分:3)

 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

我想这应该有用!!!