Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.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
如何将月份和年份合并到R中的单个列中_R - Fatal编程技术网

如何将月份和年份合并到R中的单个列中

如何将月份和年份合并到R中的单个列中,r,R,我想使用zoo库,特别是as.yearmon函数,如下所示: df$YearMonth <- as.yearmon(paste(df$year, df$month), "%Y %m") df$YearMonth您必须将paste()中的内容与格式参数%Y%m”匹配,如下所示: df$YearMonth <- as.yearmon(paste(df$year, " ", df$month), "%Y %m") df$YearMonth您必须将paste()中的内容与格式参数%Y%m

我想使用zoo库,特别是as.yearmon函数,如下所示:

df$YearMonth <- as.yearmon(paste(df$year, df$month), "%Y %m")

df$YearMonth您必须将
paste()
中的内容与格式参数
%Y%m”
匹配,如下所示:

df$YearMonth <- as.yearmon(paste(df$year, " ", df$month), "%Y %m")

df$YearMonth您必须将
paste()
中的内容与格式参数
%Y%m”
匹配,如下所示:

df$YearMonth <- as.yearmon(paste(df$year, " ", df$month), "%Y %m")

df$YearMonth如果您有适当的输入,您的代码应该可以工作。例如,所有这些工作:

library(zoo)

yr <- 2001:2003
mo <- 1:3

as.yearmon(paste(yr, " ", mo), format = "%Y %m")
## [1] "Jan 2001" "Feb 2002" "Mar 2003"

as.yearmon(paste(yr, mo), format = "%Y %m")
## [1] "Jan 2001" "Feb 2002" "Mar 2003"

as.yearmon(paste0(yr, " ", mo), format = "%Y %m")
## [1] "Jan 2001" "Feb 2002" "Mar 2003"

as.yearmon(paste(yr, mo, sep = "-"))
## [1] "Jan 2001" "Feb 2002" "Mar 2003"

as.yearmon(paste0(yr, "-", mo))
## [1] "Jan 2001" "Feb 2002" "Mar 2003"

as.yearmon(yr + (mo - 1)/12)
## [1] "Jan 2001" "Feb 2002" "Mar 2003"
图书馆(动物园)

yr如果您有适当的输入,您的代码应该可以工作。例如,所有这些工作:

library(zoo)

yr <- 2001:2003
mo <- 1:3

as.yearmon(paste(yr, " ", mo), format = "%Y %m")
## [1] "Jan 2001" "Feb 2002" "Mar 2003"

as.yearmon(paste(yr, mo), format = "%Y %m")
## [1] "Jan 2001" "Feb 2002" "Mar 2003"

as.yearmon(paste0(yr, " ", mo), format = "%Y %m")
## [1] "Jan 2001" "Feb 2002" "Mar 2003"

as.yearmon(paste(yr, mo, sep = "-"))
## [1] "Jan 2001" "Feb 2002" "Mar 2003"

as.yearmon(paste0(yr, "-", mo))
## [1] "Jan 2001" "Feb 2002" "Mar 2003"

as.yearmon(yr + (mo - 1)/12)
## [1] "Jan 2001" "Feb 2002" "Mar 2003"
图书馆(动物园)

你能举一个输入和输出数据的例子吗?使用
sep=“-”
我已经试过sep=“-”,yearmonth列仍然充满了NAThen。你的输入数据有点问题:
zoo::as.yearmon(“2007-12”)
返回
[1]“2007年12月”
也许超级福马特把事情搞砸了?我再说一遍:阅读帮助页面。请查看。您能给出一个输入和输出数据的示例吗?使用
sep=“-”
我已经尝试了sep=“-”,而yearmonth列仍然充满了NAThen。您的输入数据有问题:
zoo::as.yearmon(“2007-12”)
返回
[1]“2007年12月”
也许超级豪华的fomat把事情搞砸了?我再说一遍:阅读帮助页。请复习。