如何在R中导入ical .ics文件 [英] How to import ical .ics file in R

查看:148
本文介绍了如何在R中导入ical .ics文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 .ics文件导入R,但是,当我尝试这样做时...

I would like to import a .ics file into R, however, when I try to do so like...

sneak_cal <- read.delim("iCal-TribeEvents.ics", sep = ":", header=FALSE, stringsAsFactors = FALSE, strip.white = TRUE, na.strings = "")

...我最终拆分了网站的字符串(属于 X-ORIGINAL-URL UID 字段),也是不可取的

...I end up splitting the character strings of website (belonging to the X-ORIGINAL-URL or the UID field) too, which is undesirable

https // www.kicksonfire.com

最终目的是使数据成为整齐的格式,其中每一行代表一个单个 VEVENT ,我认为它将由唯一的 UID 表示,而不会丢失任何信息(例如URL) )

The ultimate goal is to get the data into a tidy format where each row represents a single VEVENT, which I think would be represented by a unique UID, without any loss of information (such as the URL)

是否建议使用另一种方法,例如预定义作为关键字a的字段nd是否匹配该键的值或空白?由于 .ics 文件每次都具有相同的预期字段,因此似乎可以将这些字段用作模板来读取数据,但是我不能找出方法。

Is there another approach that is recommended, such as pre-defining the fields that are expected as the key and matching the value or empty space to that key? Since the .ics file has the same expected fields each time, it seems like it might make sense to use those fields as a template to read in the data, but I can not figure out how to do it.

推荐答案

下面是一个例子

x <- readLines("https://www.kicksonfire.com/releases/?ical=1&tribe_display=list", warn = FALSE)
stopifnot(!any(grepl("^\\s+", x))) # disregarding value fields that have linefeeds for the sake of simplicity 
keyval <- do.call(rbind, regmatches(x, regexpr(":", x, fixed = TRUE), invert = TRUE))
keyval <- keyval[which.max(keyval[,1]=="BEGIN" & keyval[,2]=="VEVENT"):tail(which(keyval[,1]=="END" & keyval[,2]=="VEVENT"), 1),]
keyval <- cbind.data.frame(keyval, id=cumsum(keyval[,1]=="BEGIN" & keyval[,2]=="VEVENT"))
df <- reshape(keyval, timevar="1", idvar="id", direction = "wide")
head(df[,c(3,4,9)])
#    2.DTSTART;VALUE=DATE 2.DTEND;VALUE=DATE                              2.SUMMARY
# 1              20170422           20170423         Air Jordan 11 Low GS Blue Moon
# 14             20170422           20170423     Air Jordan 5 Premium Pure Platinum
# 27             20170427           20170428              Nike Air VaporMax Asphalt
# 40             20170427           20170428                 Nike Air VaporMax Oreo
# 53             20170427           20170428  Nike WMNS Air VaporMax White Ice Blue
# 66             20170427           20170428 wings+horns x adidas NMD R2 Light Grey

这篇关于如何在R中导入ical .ics文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆