我有一个像这样的开始/结束年份的租约数据框
region=c("a","b","c","d")
lease=c("x","y","z","k")
startyr=c(2000,2001,2003,2002)
endyr=c(2004,2004,2006,2005)
annualAmt=c(7000,8500,6000,5500)
df=data.frame(region,lease,startyr,endyr,annualAmt)
我希望通过将数据框整形为所需的输出来展开这些年:
region lease 2000 2001 2002 ... 2006
a x 7000 7000 7000 7000 7000 0 0
b y 0 8500 8500 8500 8500 0 0
逻辑是,如果租约涵盖2000 - 2004年,那么它将计入2000,2001 ... 2004年
最好的方法是什么? 如果我写一个循环,我应该如何命名新创建的年份cols 2000-2006? 或者我应该使用申请?
答案 0 :(得分:1)
这是一个主要涉及基本加法和减法的替代方案:
Rows <- df$endyr - df$startyr # How many times to repeat rows?
df <- df[rep(rownames(df), Rows), ] # Repeat the rows
df$year <- df$startyr + sequence(Rows) - 1 # Add a new "year" variable
reshape(df, direction = "wide", # Reshape, long to wide
idvar = c("region", "lease"), # idvars are the first two cols
timevar = "year", # timevar is the new year col
drop = c("startyr", "endyr")) # and drop the start/endyr cols
# region lease annualAmt.2000 annualAmt.2001 annualAmt.2002
# 1 a x 7000 7000 7000
# 2 b y NA 8500 8500
# 3 c z NA NA NA
# 4 d k NA NA 5500
# annualAmt.2003 annualAmt.2004 annualAmt.2005
# 1 7000 NA NA
# 2 8500 NA NA
# 3 6000 6000 6000
# 4 5500 5500 NA
或者,您可以使用“data.table”,如下所示:
library(data.table)
## Start with your original df
dt <- data.table(df)
dcast.data.table(
DT[, list(year = seq(startyr, endyr),
annualAmt),
by = list(region, lease)],
region + lease ~ year,
value.var = "annualAmt", fill = 0)
# region lease 2000 2001 2002 2003 2004 2005 2006
# 1: a x 7000 7000 7000 7000 7000 0 0
# 2: b y 0 8500 8500 8500 8500 0 0
# 3: c z 0 0 0 6000 6000 6000 6000
# 4: d k 0 0 5500 5500 5500 5500 0
答案 1 :(得分:0)
怎么样
years <- seq(min(df$startyr), max(df$endyr))
dd <- data.frame(region, lease, t(mapply(function(a,b, v) {
v* !is.na(match(years, seq(a, b)))
}, startyr, endyr, annualAmt)))
names(dd)[-(1:2)]<-years
dd
返回
region lease 2000 2001 2002 2003 2004 2005 2006
1 a x 7000 7000 7000 7000 7000 0 0
2 b y 0 8500 8500 8500 8500 0 0
3 c z 0 0 0 6000 6000 6000 6000
4 d k 0 0 5500 5500 5500 5500 0