我有一个数据框,如:
> df
week month year x
1 1-7 sep 2013 566
2 8-14 sep 2013 65
3 15-21 sep 2013 144
4 22-28 sep 2013 455
5 29-30 sep 2013 1212
需要将其转换为:
> df_out
date x
1 01/09/2013 80.86
2 02/09/2013 80.86
3 03/09/2013 80.86
4 04/09/2013 80.86
5 05/09/2013 80.86
6 06/09/2013 80.86
7 07/09/2013 80.86
8 08/09/2013 9.29
9 09/09/2013 9.29
10 10/09/2013 9.29
11 11/09/2013 9.29
12 12/09/2013 9.29
13 13/09/2013 9.29
14 14/09/2013 9.29
解释:2013年9月第1-7周有566个x单位。我想构建一个时间序列,它给出了一周中每天(从2013-09-01到2013-09-07)等于566/7的单位。
注意可能是一行(就像我的df的第5行),我们只有2天。所以2013-09-29的值x将是1212/2 = 606
我尝试在Excel中执行此操作,为每行df构建开始日期和结束日期。那是用“ - ”拆分周列并构建日期列。我可以在R中做到,但后来我被卡住了。
数据:
df <- structure(list(week = c("1-7", "8-14", "15-21", "22-28", "29-30"
), month = c("sep", "sep", "sep", "sep", "sep"), year = c(2013L,
2013L, 2013L, 2013L, 2013L), x = c(566L, 65L, 144L, 455L, 1212L
)), .Names = c("week", "month", "year", "x"), class = "data.frame", row.names = c(NA,
-5L))
答案 0 :(得分:2)
试试这个:
div.glass_sound {
width:300px;
height: 300px;
background-size: 100%;
}
.glass_sound {
background-image: url('https://unsplash.imgix.net/photo-1430760814266-9c81759e5e55?dpr=2&fit=crop&fm=jpg&q=75&w=1050'), url('https://unsplash.imgix.net/photo-1431184052543-809fa8cc9bd6?dpr=2&fit=crop&fm=jpg&q=75&w=1050');
background-blend-mode: multiply;
}
dfl=split(df,1:nrow(df)) do.call(rbind,lapply(dfl,function(wd){ d=as.numeric(unlist(strsplit(wd$week, "-", fixed = TRUE))) days=d[1]:d[2] date=as.Date(paste(wd$year,wd$month,days,sep="/"),"%Y/%b/%d") x=round(rep(wd$x/length(days),length(days)),2) data.frame(date,x) }))