Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regexp从年月日起拆分月份_Regex_R_Date - Fatal编程技术网

Regexp从年月日起拆分月份

Regexp从年月日起拆分月份,regex,r,date,Regex,R,Date,我有一个变量,它是月和年的串联,以数字格式。月份的格式为1-12,而不是01-12 我的变量如下所示: mmyyyy 12014 22014 102014 52015 112015 我正在寻找只与月份或年份匹配的regexp: 这一年,我做了一些类似的事情: year <- ifelse(grepl("2014", mmyyyy), 2014, ifelse(grepl("2015", mmyyyy), 2015, 2016)) year一个选项是 # for the months:

我有一个变量,它是月和年的串联,以数字格式。月份的格式为1-12,而不是01-12

我的变量如下所示:

mmyyyy
12014
22014
102014
52015
112015
我正在寻找只与月份或年份匹配的regexp:

这一年,我做了一些类似的事情:

year <- ifelse(grepl("2014", mmyyyy), 2014, ifelse(grepl("2015", mmyyyy), 2015, 2016))
year一个选项是

# for the months:
> as.numeric(gsub("(.*)[0-9]{4}$", "\\1", x))
#[1]  1  2 10  5 11
# for the years:
> as.numeric(gsub(".*([0-9]{4})$", "\\1", x))
#[1] 2014 2014 2014 2015 2015
这适用于任何4位数年份。

一个选项是

# for the months:
> as.numeric(gsub("(.*)[0-9]{4}$", "\\1", x))
#[1]  1  2 10  5 11
# for the years:
> as.numeric(gsub(".*([0-9]{4})$", "\\1", x))
#[1] 2014 2014 2014 2015 2015

这适用于任何4位数的年份。

下面的例子如何(假设您只处理年份>2000年)


month下面的例子怎么样(假设你只处理年份>2000)


month一种可能的解决方案,使用
tidyr
在一次调用中同时创建
month
year

library(tidyr)
extract(df, mmyyyy, c("month", "year"), "(\\d+)(\\d{4})", convert = TRUE)
#   month year
# 1     1 2014
# 2     2 2014
# 3    10 2014
# 4     5 2015
# 5    11 2015

数据

df <- data.frame(mmyyyy = c(12014,
                            22014,
                            102014,
                            52015,
                            112015))

df一种可能的解决方案,使用
tidyr
,它将在一次调用中同时创建
month
year

library(tidyr)
extract(df, mmyyyy, c("month", "year"), "(\\d+)(\\d{4})", convert = TRUE)
#   month year
# 1     1 2014
# 2     2 2014
# 3    10 2014
# 4     5 2015
# 5    11 2015

数据

df <- data.frame(mmyyyy = c(12014,
                            22014,
                            102014,
                            52015,
                            112015))
df

为什么不将最后一个字符拆分为年份? 请参阅stringr软件包中的STRU sub。

为什么不将最后一个字符拆分为年份?
请参阅stringr软件包中的stru sub。

我真的不知道如何使用正则表达式,但这里有一个简单的代码。此代码将在9999年之前的所有年份都有效:)


dmmyyyy我真的不知道如何使用正则表达式,但这里有一个简单的代码。此代码将在9999年之前的所有年份都有效:)

dmmyyyy
您可以使用
zoo
软件包中的
yearmon
功能

library(zoo)
dates1 <- as.yearmon(dates, format = "%m%Y")
format(dates1, "%m")
# [1] "01" "02" "10" "05" "11"
format(dates1, "%Y")
# [1] "2014" "2014" "2014" "2015" "2015"
图书馆(动物园)
日期1
您可以使用
zoo
软件包中的
yearmon
功能

library(zoo)
dates1 <- as.yearmon(dates, format = "%m%Y")
format(dates1, "%m")
# [1] "01" "02" "10" "05" "11"
format(dates1, "%Y")
# [1] "2014" "2014" "2014" "2015" "2015"
图书馆(动物园)
dates1您可以使用包unglue:

df月-年
#> 1     1 2014
#> 2     2 2014
#> 3    10 2014
#> 4     5 2015
#> 5    11 2015
您可以使用该软件包来解胶:

df月-年
#> 1     1 2014
#> 2     2 2014
#> 3    10 2014
#> 4     5 2015
#> 5    11 2015

对不起,您想要的输出是什么?我想要一个可变年和可变月。您的值是数值类还是字符?@Daid:numeric,但如果需要,我可以更改格式。您是否尝试过类似
(?m)(?\d+(\d{4})$之类的内容,您想要的输出是什么?我想要一个可变年和可变月。您的值是数值类还是字符?@Daid:numeric,但如果需要,我可以更改格式。您尝试过类似
(?m)(?\d+(\d+)(\d{4})$
@PalashKumar,
[0-9]{4}这样的东西吗$
表示最后4个数字,
*
表示任意次数的任意字符。然后,
(…)
是一个捕获组,您可以将其释放。在这两种情况下,我都提取了第一个捕获组,但更改了
(…)
@PalashKumar的位置,
[0-9]{4}$
表示最后4个数字,
*
表示任意次数的任何字符。然后,
(…)
是一个捕获组,您可以将其释放。在这两种情况下,我提取第一个捕获组,但更改
(…)
的位置。
library(zoo)
dates1 <- as.yearmon(dates, format = "%m%Y")
format(dates1, "%m")
# [1] "01" "02" "10" "05" "11"
format(dates1, "%Y")
# [1] "2014" "2014" "2014" "2015" "2015"