根据月份日期将季节列添加到数据表

时间:2016-04-28 01:13:23

标签: r data.table

我正在使用data.table,我正在尝试创建一个名为" season"的新列,它会创建一个具有相应季节的列,例如夏天,冬天...基于名为" MonthName"。

的列

我想知道是否有更有效的方法可以根据月份值将季节列添加到数据表中。

这是300,000个观察中的前6个,假设该表被称为" dt"。

    rrp         Year   Month Finyear hourminute AvgPriceByTOD MonthName
1: 35.27500     1999     1    1999      00:00      33.09037       Jan
2: 21.01167     1999     1    1999      00:00      33.09037       Jan
3: 25.28667     1999     2    1999      00:00      33.09037       Feb
4: 18.42334     1999     2    1999      00:00      33.09037       Feb
5: 16.67499     1999     2    1999      00:00      33.09037       Feb
6: 18.90001     1999     2    1999      00:00      33.09037       Feb

我尝试过以下代码:

dt[, Season :=  ifelse(MonthName = c("Jun", "Jul", "Aug"),"Winter", ifelse(MonthName = c("Dec", "Jan", "Feb"), "Summer", ifelse(MonthName = c("Sep", "Oct", "Nov"), "Spring" , ifelse(MonthName = c("Mar", "Apr", "May"), "Autumn", NA))))]

返回:

 rrp totaldemand   Year Month Finyear hourminute AvgPriceByTOD MonthName Season
1: 35.27500     1999     1    1999      00:00      33.09037       Jan     NA
2: 21.01167     1999     1    1999      00:00      33.09037       Jan Summer
3: 25.28667     1999     2    1999      00:00      33.09037       Feb Summer
4: 18.42334     1999     2    1999      00:00      33.09037       Feb     NA
5: 16.67499     1999     2    1999      00:00      33.09037       Feb     NA
6: 18.90001     1999     2    1999      00:00      33.09037       Feb Summer

我收到错误:

Warning messages:
1: In MonthName == c("Jun", "Jul", "Aug") :
  longer object length is not a multiple of shorter object length
2: In MonthName == c("Dec", "Jan", "Feb") :
  longer object length is not a multiple of shorter object length
3: In MonthName == c("Sep", "Oct", "Nov") :
  longer object length is not a multiple of shorter object length
4: In MonthName == c("Mar", "Apr", "May") :
  longer object length is not a multiple of shorter object length 

除此之外,由于我不知道的原因,夏季的一些月份被正确分配了#34;夏天",但其他人被分配了NA,例如第1行和第2行都应该是夏天,但回报不同。

提前致谢!

2 个答案:

答案 0 :(得分:9)

一种非常直接的方法是使用查找表将月份名称映射到季节:

# create a named vector where names are the month names and elements are seasons
seasons <- rep(c("winter","spring","summer","fall"), each = 3)
names(seasons) <- month.abb[c(6:12,1:5)] # thanks thelatemail for pointing out month.abb
seasons
#     Jun      Jul      Aug      Sep      Oct      Nov      Dec      Jan 
#"winter" "winter" "winter" "spring" "spring" "spring" "summer" "summer" 
#     Feb      Mar      Apr      May 
#"summer"   "fall"   "fall"   "fall" 

使用它:

dt[, season := seasons[MonthName]]

数据:

dt <- setDT(read.table(text="    rrp         Year   Month Finyear hourminute AvgPriceByTOD MonthName
1: 35.27500     1999     1    1999      00:00      33.09037       Jan
2: 21.01167     1999     1    1999      00:00      33.09037       Jan
3: 25.28667     1999     2    1999      00:00      33.09037       Feb
4: 18.42334     1999     2    1999      00:00      33.09037       Feb
5: 16.67499     1999     2    1999      00:00      33.09037       Feb
6: 18.90001     1999     2    1999      00:00      33.09037       Feb",
   header = TRUE, stringsAsFactors = FALSE))

答案 1 :(得分:5)

有点打字,但代码效率很高

dt[MonthName %in% c("Jun","Jul","Aug"), Season := "Winter"]
dt[MonthName %in% c("Dec","Jan","Feb"), Season := "Summer"]
dt[MonthName %in% c("Sep","Oct","Nov"), Season := "Spring"]
dt[is.na(MonthName), Season := "Autumn"]

我们在这里为data.table

的子集分配引用

我更喜欢这种嵌套ifelse s

如果要检查某个值是否在向量中,则必须使用%in%。查看以下不同的行为:

myVec <- c("a","b","c")

"a" == myVec
[1] TRUE FALSE FALSE

"a" %in% myVec
[1] TRUE