将列表列表附加到R中单列中的数据框
Appending list of lists to data frame in single column in 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["Date And Time"] <- 日期
但只有第一个日期被复制到整个列并发出以下警告。
Warning message:
In `[<-.data.frame`(`*tmp*`, , "Date And Time", value = list(1452391200, :
provided 18 variables to replace 1 variables
如果我要执行 unlist(dates) 它会再次给我时间戳。我怎样才能提取日期和时间??
尝试 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")
我正在使用 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["Date And Time"] <- 日期
但只有第一个日期被复制到整个列并发出以下警告。
Warning message:
In `[<-.data.frame`(`*tmp*`, , "Date And Time", value = list(1452391200, :
provided 18 variables to replace 1 variables
如果我要执行 unlist(dates) 它会再次给我时间戳。我怎样才能提取日期和时间??
尝试 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")