我有每月的数据,我想为这段时间增加另一栏。专栏里写着一月的M01,二月的M02,三月的M03等等。有办法这样做吗?
这就是我所拥有的:
unemployment = data.frame(Month = c("Sept 2002", "Oct 2002", "Nov 2002", "Dec 2002", "Jan 2003", "Feb 2003"),
Total = c(5.7, 5.7, 5.9,
6, 5.8, 5.9))
> unemployment
Month Total
1 Sept 2002 5.7
2 Oct 2002 5.7
3 Nov 2002 5.9
4 Dec 2002 6.0
5 Jan 2003 5.8
6 Feb 2003 5.9这就是我想要的:
Month Period Total
1 Sept 2002 M09 5.7
2 Oct 2002 M10 5.7
3 Nov 2002 M11 5.9
4 Dec 2002 M12 6.0
5 Jan 2003 M01 5.8
6 Feb 2003 M02 5.9编辑更新代码以显示全部12个月
structure(list(Month = c("Jan", "Feb", "Mar", "Apr", "May", "June"
), Year = c("2003", "2003", "2003", "2003", "2003", "2003"),
Unemp_percent = c(5.8, 5.9, 5.9, 6, 6.1, 6.3)), row.names = 5:10, class = "data.frame")发布于 2022-10-28 22:32:06
使用dplyr
unemployment %>%
mutate(Period = case_when(grepl("Jan",Month) ~ "M01",
grepl("Feb",Month) ~ "M02",
grepl("Mar",Month) ~ "M03",
grepl("Apr",Month) ~ "M04",
grepl("May",Month) ~ "M05",
grepl("June",Month) ~ "M06",
grepl("July",Month) ~ "M07",
grepl("Aug",Month) ~ "M08",
grepl("Sept",Month) ~ "M09",
grepl("Oct",Month) ~ "M10",
grepl("Nov",Month) ~ "M11",
grepl("Dec",Month) ~ "M12"))发布于 2022-10-28 22:30:10
您可以使用mutate、gsub和match,前三个字母与内置的month.abb数据一起使用
library(dplyr)
unemployment |>
mutate(.after = Month,
Period = paste0("M", match(gsub("(.{3})(.*)", "\\1", Month ), month.abb))) Month Period Total
1 Sept 2002 M9 5.7
2 Oct 2002 M10 5.7
3 Nov 2002 M11 5.9
4 Dec 2002 M12 6.0
5 Jan 2003 M1 5.8
6 Feb 2003 M2 5.9发布于 2022-10-28 22:40:52
以下是另一种选择:
unemployment %>%
mutate(month = gsub("(^.{3}).*", "\\1", Month),
Period = paste0("M", as.numeric(factor(x$month, month.abb)))) %>%
select(Month, Period, Total)输出:
Month Period Total
1 Sept 2002 M9 5.7
2 Oct 2002 M10 5.7
3 Nov 2002 M11 5.9
4 Dec 2002 M12 6.0
5 Jan 2003 M1 5.8
6 Feb 2003 M2 5.9https://stackoverflow.com/questions/74241306
复制相似问题