我有这个小标题,上面有一堆日期,下面是小标题的示例。
library(tibble); library(lubridate)
c("25/10/1952", "27/05/1961", "07/11/1962", "20/12/1963", "15/01/1966", "12/02/1966", "12/05/1966", "11/11/1967", "14/01/1975", "17/10/1976", "05/11/1981", "03/11/1982", "14/11/1982", "12/10/1984", "24/12/1988", "26/10/1998", "11/11/2003", "14/12/2003") -> start
c("08/11/1952", "27/05/1961", "07/11/1962", "28/12/1963", "10/03/1966", "05/03/1966", "12/05/1966", "16/11/1967", "15/02/1975", "30/10/1976", "05/11/1981", "16/11/1982", "18/11/1982", "12/10/1984", "29/12/1988", "28/10/1998", "12/11/2003", "02/02/2004") -> finish
tibble(start = as.Date(dmy(start)), finish = as.Date(dmy(finish))) -> df
我需要获取任一列中包含的唯一月份。所以上面的输出将是这样的:
January, February, March, May, October, November, December
我怎样才能实现这个目标?
你可以做
> df[] = lapply(df, \(x) format(as.Date(x), "%B"))
> df
start finish
1 October November
2 May May
3 November November
4 December December
5 January March
6 February March
7 May May
8 November November
9 January February
10 October October
11 November November
12 November November
13 November November
14 October October
15 December December
16 October October
17 November November
18 December February
数据:
df = data.frame(start = c("25/10/1952", "27/05/1961", "07/11/1962", "20/12/1963", "15/01/1966", "12/02/1966", "12/05/1966", "11/11/1967", "14/01/1975", "17/10/1976", "05/11/1981", "03/11/1982", "14/11/1982", "12/10/1984", "24/12/1988", "26/10/1998", "11/11/2003", "14/12/2003"),
finish = c("08/11/1952", "27/05/1961", "07/11/1962", "28/12/1963", "10/03/1966", "05/03/1966", "12/05/1966", "16/11/1967", "15/02/1975", "30/10/1976", "05/11/1981", "16/11/1982", "18/11/1982", "12/10/1984", "29/12/1988", "28/10/1998", "12/11/2003", "02/02/2004"))