R 为组内一年内的月份分配编号

R 为组内一年内的月份分配编号,r,R,我想给组内的月份分配数字 如何在R中实现这一点?这就是您要寻找的吗 数据: 我打赌有更好的方法可以做到这一点,但事情就是这样。使用包zoo函数as.yearmon mon2num <- function(year, month){ m <- zoo::as.yearmon(as.Date(paste(year, month, 1, sep = "-"), "%Y-%b-%d")) d <- seq(as.Date("2000-01-01"), Sys.Date(),

我想给组内的月份分配数字


如何在R中实现这一点?

这就是您要寻找的吗

数据:


我打赌有更好的方法可以做到这一点,但事情就是这样。使用包
zoo
函数
as.yearmon

mon2num <- function(year, month){
  m <- zoo::as.yearmon(as.Date(paste(year, month, 1, sep = "-"), "%Y-%b-%d"))
  d <- seq(as.Date("2000-01-01"), Sys.Date(), by = "month")
  d <- zoo::as.yearmon(d)
  match(m, d)
}

mon2num(df$year, df$month)
#[1]  1  2  4 38  8 21 28

mon2num将年和月转换为
yearmon
类。这些对象在内部表示年/月为年+分数,其中分数为0表示一月,1/12表示二月,2/12表示三月,依此类推。现在,如果我们取
yearmon
内部表示和2000之间的差,将其乘以12,再加上1,我们将得到所需的num

library(zoo)

transform(DF, num = 12 * as.numeric(as.yearmon(paste(month, year), "%b %Y") - 2000) + 1)
给予:

  group month number year num
1     1   Jan      1 2000   1
2     1   Feb      2 2000   2
3     1   Apr      4 2000   4
4     1   Feb     38 2003  38
5     2   Aug      8 2000   8
6     2   Sep     21 2001  21
7     2   Apr     28 2002  28
:可复制形式的输入如下所示。我们已经包括了
number
列,以便我们可以将其与上面计算的
num
列进行比较

DF <- structure(list(group = c(1, 1, 1, 1, 2, 2, 2), month = structure(c(4L, 
3L, 1L, 3L, 2L, 5L, 1L), .Label = c("Apr", "Aug", "Feb", "Jan", 
"Sep"), class = "factor"), number = c(1, 2, 4, 38, 8, 21, 28), 
    year = c(2000, 2000, 2000, 2003, 2000, 2001, 2002)), class = "data.frame", row.names = c(NA, 
-7L))

DF在base R中,可以使用
match
和一些简单的数学运算

transform(df, number=match(as.character(month), month.abb) + (year - 2000) * 12)
#   group month year number
# 1     1   Jan 2000      1
# 2     1   Feb 2000      2
# 3     1   Apr 2000      4
# 4     1   Feb 2003     38
# 5     2   Aug 2000      8
# 6     2   Sep 2001     21
# 7     2   Apr 2002     28
数据:


df您能澄清一下您的例子吗?看起来你从2000年1月开始数月。“组”与此编号有什么关系?计数必须在每个组内进行,而不是跨组进行。即从2000年1月到2003年12月在集团内。除非问题是关于图像处理,否则图像既不是代码也不是数据。请尊重贡献者的时间,并遵循其他好问题的示例,将代码和数据发布到适当的代码块中。代码工作得不太好。我不知道如何将输出粘贴到每个井。我可以看出有人已经对我的表格格式很生气了。我不想冒犯任何人。请输入代码中val的值:1,2,4,2,8,9,但我要生成的是数据中的“数字”列,并按此顺序生成。谢谢,我已经编辑了我的帖子来展示一个输出,这就是你想要的吗?帖子中的代码为每个月分配数字,即
Jan=1、Apr=4、Sep=9
。感谢您的回复和帮助。不需要。我需要的是根据“组”、“月”和“年”列生成“数字”列中的值,但编号应在2000-2003年的组内。谢谢你想要的是为
group=1
month=Feb
year=2003
分配一个
38
,然后为
group 2
month=sep
year=2000
分配一个
21
的数字。谢谢这很有效。我只需要将val绑定到数据集。谢谢你们的回复。@Jyde我已经编辑了答案。看看是不是这样。
group <- c(1, 1, 1, 1, 2, 2, 2)
month <- c("Jan", "Feb", "Apr", "Feb", "Aug", "Sep","Apr")
year <- c(2000, 2000, 2000, 2003, 2000, 2001, 2002)
number <- c(1, 2, 4, 38, 8, 21, 28)
df <- as.data.frame(cbind(group, month, number, year))
library(zoo)

transform(DF, num = 12 * as.numeric(as.yearmon(paste(month, year), "%b %Y") - 2000) + 1)
  group month number year num
1     1   Jan      1 2000   1
2     1   Feb      2 2000   2
3     1   Apr      4 2000   4
4     1   Feb     38 2003  38
5     2   Aug      8 2000   8
6     2   Sep     21 2001  21
7     2   Apr     28 2002  28
DF <- structure(list(group = c(1, 1, 1, 1, 2, 2, 2), month = structure(c(4L, 
3L, 1L, 3L, 2L, 5L, 1L), .Label = c("Apr", "Aug", "Feb", "Jan", 
"Sep"), class = "factor"), number = c(1, 2, 4, 38, 8, 21, 28), 
    year = c(2000, 2000, 2000, 2003, 2000, 2001, 2002)), class = "data.frame", row.names = c(NA, 
-7L))
transform(df, number=match(as.character(month), month.abb) + (year - 2000) * 12)
#   group month year number
# 1     1   Jan 2000      1
# 2     1   Feb 2000      2
# 3     1   Apr 2000      4
# 4     1   Feb 2003     38
# 5     2   Aug 2000      8
# 6     2   Sep 2001     21
# 7     2   Apr 2002     28
df <- structure(list(group = c(1, 1, 1, 1, 2, 2, 2), month = structure(c(4L, 
3L, 1L, 3L, 2L, 5L, 1L), .Label = c("Apr", "Aug", "Feb", "Jan", 
"Sep"), class = "factor")), .Names = c("group", "month"), row.names = c(NA, 
-7L), class = "data.frame")