我在R中有一个这样的数据框:-
NO_OF_EMPLOYMENT MONTH YEAR
127 9 2017
125 10 2017
120 11 2017
130 12 2017
110 1 2018
125 2 2018
我需要将MONTH数据转换为按日计算的数据,并且我的数据框应如下所示:-
NO_OF_EMPLOYMENT MONTH YEAR DAY
127 9 2017 1
127 9 2017 2
127 9 2017 3
127 9 2017 4
.
.
.
127 9 2017 30
125 10 2017 1
125 10 2017 2
,以此类推。 我试图找到一个类似的问题,但没有成功。
数据
df <- read.table(text="
NO_OF_EMPLOYMENT MONTH YEAR
127 9 2017
125 10 2017
120 11 2017
130 12 2017
110 1 2018
125 2 2018", h = T)
答案 0 :(得分:1)
另一种lubdridate
/ tidyverse
的方式:
library(tidyverse)
library(lubridate)
df %>%
mutate(DAY = map2(
YEAR, MONTH, ~seq(days_in_month(as.Date(str_c(.x,"-",.y,"-",1)))))) %>%
unnest
# NO_OF_EMPLOYMENT MONTH YEAR day
# 1 127 9 2017 1
# 2 127 9 2017 2
# 3 127 9 2017 3
# 4 127 9 2017 4
# ...
答案 1 :(得分:1)
使用tidyverse
和lubridate
的另一种方法-
library(tidyverse)
library(lubridate)
df %>%
uncount(
weights = days_in_month(make_date(YEAR, MONTH)),
.id = "Day"
)
答案 2 :(得分:0)
这是一种简单的方法:
mydata<-data.frame(NO_Empl=c(127,125,124),Month=c(9,8,7),Year=c(2017,2018,2017))
library(lubridate)
library(tidyverse)
as.tibble(mydata) %>%
mutate(Date=make_date(Year,Month)) %>%
select(-Month,-Year) %>%
mutate(Day=wday(Date),Day_date=month(Date))
结果:
# A tibble: 3 x 4
NO_Empl Date Day Day_date
<dbl> <date> <dbl> <dbl>
1 127 2017-09-01 6 9
2 125 2018-08-01 4 8
3 124 2017-07-01 7 7
答案 3 :(得分:0)
0依赖性,基本R解决方案快2倍:
read.table(text="NO_OF_EMPLOYMENT MONTH YEAR
127 9 2017
125 10 2017
120 11 2017
130 12 2017
110 1 2018
125 2 2018", header=TRUE) -> xdf
do.call(
rbind.data.frame,
lapply(1:nrow(xdf), function(idx) {
time <- as.POSIXlt(as.Date(sprintf("%s-%02s-01", xdf$YEAR[idx], xdf$MONTH[idx])))
time$mday[] <- time$sec[] <- time$min <- time$hour <- 0
time$mon <- time$mon + 1
data.frame(
YEAR = xdf$YEAR[idx],
MONTH = xdf$MONTH[idx],
DAY = seq(1:as.POSIXlt(as.POSIXct(time))$mday),
NO_OF_EMPLOYMENT = xdf$NO_OF_EMPLOYMENT[idx]
)
})
)