R 手动拆分,然后使用子字符串拆分一个字符钉住,准备好函数来完成吗?
这是我要拆分的字符串:R 手动拆分,然后使用子字符串拆分一个字符钉住,准备好函数来完成吗?,r,R,这是我要拆分的字符串: b[1] [1] “县2016年1月2016年2月2016年3月2016年4月2016年5月2016年6月2016年7月2016年8月2016年9月2016年10月2016年11月2016年12月\r” 从这篇文章中,我收集到没有现成的函数可以这样做,我只是想确认一下 这是我的密码 split.pos <- gregexpr("County|([aA-zZ]{1,} [0-9]{4,})", b[1], perl = FALSE) split.length <
b[1]
[1] “县2016年1月2016年2月2016年3月2016年4月2016年5月2016年6月2016年7月2016年8月2016年9月2016年10月2016年11月2016年12月\r”
从这篇文章中,我收集到没有现成的函数可以这样做,我只是想确认一下
这是我的密码
split.pos <- gregexpr("County|([aA-zZ]{1,} [0-9]{4,})", b[1], perl = FALSE)
split.length <- attr(split.pos[[1]], "match.length")
split.start <- split.pos[[1]][1:length(split.pos[[1]])]
substring(b[1], split.start, split.start+split.length)
[1] "County " "January 2016 " "February 2016 " "March 2016 "
[5] "April 2016 " "May 2016 " "June 2016 " "July 2016 "
[9] "August 2016 " "September 2016 " "October 2016 " "November 2016 "
[13] "December 2016\r
split.pos我们可以对regex lookaround使用strsplit
strsplit(b, "(?<=[0-9])\\s+|\\s+(?=[A-Z])", perl = TRUE)[[1]]
#[1] "County" "January 2016" "February 2016" "March 2016" "April 2016" "May 2016" "June 2016" "July 2016" "August 2016"
#[10] "September 2016" "October 2016" "November 2016" "December 2016"
strsplit(b),(?分割标准是什么?数据选项卡是否分隔?固定宽度?始终是非数字、非间隔值,后面可选数字?您的“更好”标准是什么?感谢您敦促我正确考虑此操作。
b <- "County January 2016 February 2016 March 2016 April 2016 May 2016 June 2016 July 2016 August 2016 September 2016 October 2016 November 2016 December 2016\r"