簡體   English   中英

讀入data.frame

[英]Reading into data.frame

我需要使用readHTMLTable函數將http://yfacts.byu.edu/Article?id=85上的BYU學費數據讀入data.frame。 我還需要清理數據並將三個變量命名為“ year”,“ lds”和“ nonlds”。

我有以下代碼:

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]

有人可以幫我弄清楚我做錯了什么,我需要做些什么?

謝謝

您的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM