将数字分配给组中一年中的月份

时间:2018-11-11 14:51:33

标签: r

我想为组中的月份分配数字,例如

enter image description here

如何在R中执行此操作?

4 个答案:

答案 0 :(得分:1)

这是您要寻找的吗?

数据:

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, 2002)
number <- c(1, 2, 4, 38, 8, 21)
df <- as.data.frame(cbind(group, month, number, year))

创建一个函数并应用:

mo2Num <- function(x) match(tolower(x), tolower(month.abb))
mo2Num(df$month)

library(dplyr)
df %>%
  group_by(group) %>%
  mutate(val=mo2Num(month)) %>%
  data.frame()

输出:

  group month number year val
1     1   Jan      1 2000   1
2     1   Feb      2 2000   2
3     1   Apr      4 2000   4
4     1   Feb     38 2003   2
5     2   Aug      8 2000   8
6     2   Sep     21 2002   9
7     2   Apr      1 2000   4

答案 1 :(得分:1)

将年和月转换为yearmon类。这样的对象在内部将年/月表示为年+分数,其中对于Jan,分数是0;对于Feb,分数是1/12;对于Mar,分数是2/12,依此类推。现在,如果我们将yearmon内部表示形式与2000之间的差值乘以12,再加上1,我们将得到所需的数字。

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))

答案 2 :(得分:0)

我敢打赌,有更好的方法可以做到这一点,但这确实可行。使用包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

仅将返回值绑定到原始数​​据帧

num <- mon2num(df$year, df$month)
cbind(df, Number = num)
#  group month number year Number
#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

数据。
我将重新发布数据,因为在user113156的answer中缺少一些值。

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))

答案 3 :(得分:0)

在基数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 <- 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")