2016-01-10 48 views
0

我在R工作组下面的代码来凑网页信息的列表:追加在R里面,列表数据帧中单列

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" 

现在我将其转换为正确的日期和时间格式

dates <- lapply(X = matches_date , function(timestamp_match){ 
(as.POSIXct(as.numeric(timestamp_match)/1000, origin="1970-01-01")) }) 

和现在我有以下表格日期:

 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" 

现在我附加到这个数据帧中的一个柱:

matches_info [,“日期和时间”] < - 日期

,但只有第一个日期是越来越复制整列并给予以下警告。

Warning message: 
In `[<-.data.frame`(`*tmp*`, , "Date And Time", value = list(1452391200, : 
provided 18 variables to replace 1 variables 

如果我会做unlist(日期)它再次给我时间戳。我怎么能夸大日期和时间?

+0

最终,你可以做'日期< - sapply(X = matches_date,...)'而不是'lapply' – jogo

+2

这是lapply的需求吗? 'as.POSIXct(as.numeric(matches_date)/ 1000,origin =“1970-01-01”)'是否足够?所有涉及的功能都是矢量化的。 – nicola

回答

1

尝试的do.call(c, dates)代替unlist(dates)防止[R从转换列表中的元素,以数字和保持他们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) 

或者干脆

matches_date <- c("1452268800000", "1452132000000") 
matches_info[,"Date And Time"] <- as.POSIXct(as.numeric(matches_date)/1000, origin="1970-01-01") 
+2

lapply不是必须的,因为它里面的所有函数都已经被矢量化 – hadley