2016-04-12 26 views
1

私は、readHTMLTable関数を使用して、 http://yfacts.byu.edu/Article?id=85のBYU授業データをdata.frameに読み込む必要があります。私はまた、データを整理し、3つの変数 "year"、 "lds"、および "nonlds"の名前を付ける必要があります。data.frameへの読み込み

私は次のコードを持っている:

library("XML") 
download.file("http://yfacts.byu.edu/Article?id=85", 
      destfile = "tuitiondata.html") 

BYUtuition <- readHTMLTable("tuitiondata.html", 
      header=T, skip.rows=4, 
      colClasses=c("character","FormattedNumber","FormattedNumber")) 
names(BYUtuition)<-c("year","lds","nonlds") 

をそして、私は以下の結果を得ている:

BYUtuition 
$`NULL` 
V1 
1                                                                                                                     Tuition History 
2                                                                                                                  For Full-time Undergraduate Students 
3                                                                                                                         1960-61 
4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ... 
58                                                                                                                         2015-16 
59                                                                                                                           
60 * A significant portion of the cost of operating the university is paid from the tithes of The Church of Jesus Christ of Latter-day Saints. Therefore, students and families of students who are tithe-paying members of the Church have already made a contribution to the operation of the university. Because others will not have made this contribution, they are charged a higher tuition, a practice similar in principle to that of state universities charging higher tuition to nonresidents. 
    V2 V3 
1 NA NA 
2 NA NA 
3 NA NA 
4 NA NA 
... 
60 NA NA 
> mormons<-mormons[[1]] 
Error: object 'mormons' not found 
> names(BYUtuition)<-c("year","lds","nonlds") 
Error in names(BYUtuition) <- c("year", "lds", "nonlds") : 
    'names' attribute [3] must be the same length as the vector [1] 

誰かが私が間違ってやっていると私は必要なものを見つけ出す助けてください代わりにするには?

ありがとうございました

答えて

2

あなたのBYUtuitionはリストです。 [[1]]を使用して、data.frameを内部で抽出します。 FormattedNumberを使用するのではなく、書式設定を実行できます。

BYUtuition <- readHTMLTable("tuitiondata.html",header=T,skip.rows=4)[[1]] 

#remove rows with any NA 
BYUtuition <- na.omit(BYUtuition) 

#set names 
names(BYUtuition) <- c("year","lds","nonlds") 

#convert course fee into numeric 
BYUtuition$lds <- as.numeric(gsub("[^0-9a-zA-Z]+", "",BYUtuition$lds)) 
BYUtuition$nonlds <- as.numeric(gsub("[^0-9a-zA-Z]+", "",BYUtuition$nonlds)) 

#show final table 
BYUtuition 
関連する問題