Reputation: 1114
I am Working on below code in r to scrape a web page info :
library(rvest)
crickbuzz <- read_html(httr::GET("http://www.cricbuzz.com/cricket -match/live-scores"))
matches_dates <- crickbuzz %>%
html_nodes(".schedule-date:nth-child(1)")%>%
html_attr("timestamp")
matches_dates
[1] "1452268800000" "1452132000000" "1452247200000" "1452242400000" "1452327000000" "1452290400000" "1452310200000" "1452310200000" "1452310200000"
[10] "1452310200000" "1452324600000" "1452324600000" "1452324600000" "1452324600000" "1452324600000" "1452150000000" "1452153600000" "1452153600000"
now i am converting it to proper date and time format
dates <- lapply(X = matches_date , function(timestamp_match){
(as.POSIXct(as.numeric(timestamp_match)/1000, origin="1970-01-01")) })
and now i have dates in the below form :
dates
[[1]]
[1] "2016-01-10 07:30:00 IST"
[[2]]
[1] "2016-01-10 21:30:00 IST"
[[3]]
[1] "2016-01-09 12:00:00 IST"
[[4]]
[1] "2016-01-10 13:55:00 IST"
[[5]]
[1] "2016-01-10 10:50:00 IST"
[[6]]
[1] "2016-01-07 12:30:00 IST"
[[7]]
[1] "2016-01-07 13:30:00 IST"
[[8]]
[1] "2016-01-10 09:00:00 IST"
[[9]]
[1] "2016-01-10 09:00:00 IST"
[[10]]
[1] "2016-01-10 09:00:00 IST"
[[11]]
[1] "2016-01-10 09:00:00 IST"
[[12]]
[1] "2016-01-10 09:00:00 IST"
[[13]]
[1] "2016-01-10 13:00:00 IST"
[[14]]
[1] "2016-01-10 13:00:00 IST"
[[15]]
[1] "2016-01-10 13:00:00 IST"
[[16]]
[1] "2016-01-10 13:00:00 IST"
[[17]]
[1] "2016-01-10 03:30:00 IST"
[[18]]
[1] "2016-01-10 03:30:00 IST"
now i am appending this to one column of data frame :
matches_info[,"Date And Time"] <- dates
but only 1st date is getting copied over whole column and giving below warning.
Warning message:
In `[<-.data.frame`(`*tmp*`, , "Date And Time", value = list(1452391200, :
provided 18 variables to replace 1 variables
and if i will do unlist(dates) it is giving me timestamps again. How can i extrate date and time ??
Upvotes: 0
Views: 197
Reputation: 54287
Try do.call(c, dates)
instead of unlist(dates)
to prevent R from converting the list elements to numeric and keeping them POSIXct:
matches_date <- c("1452268800000", "1452132000000")
dates <- lapply(X = matches_date , function(timestamp_match){
(as.POSIXct(as.numeric(timestamp_match)/1000, origin="1970-01-01")) })
do.call(c, dates)
# [1] "2016-01-08 17:00:00 CET" "2016-01-07 03:00:00 CET"
matches_info[,"Date And Time"] <- do.call(c, dates)
or simply
matches_date <- c("1452268800000", "1452132000000")
matches_info[,"Date And Time"] <- as.POSIXct(as.numeric(matches_date)/1000, origin="1970-01-01")
Upvotes: 1