Reputation: 287
I need to Read into a data.frame the BYU tuition data at http://yfacts.byu.edu/Article?id=85 using the readHTMLTable function. I also need to clean up the data and name the three variables "year", "lds", and "nonlds".
I have the following code:
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")
And I'm getting the following results:
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]
Can someone please help me figure out what I'm doing wrong and what I need to do instead?
Thanks
Upvotes: 1
Views: 51
Reputation: 25225
your BYUtuition is a list. Use [[1]] to extract the data.frame within. then you can perform your formatting rather than using 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
Upvotes: 2