我一直在努力将函数的参数放在循环中。 我想要做的事情如下:
得到这个:
testResult1.2<-ddply(payments_All_cum_pct,"Vintage",summarise,
N_obs = sum(!is.na(TotalDue)),
Sum_TotalDue = sum(TotalDue, na.rm=TRUE),
Mean_TotalDue = mean(TotalDue,na.rm = TRUE),
wm1 = weighted.mean(Month1,TotalDue,na.rm=TRUE),
wm2 = weighted.mean(Month2,TotalDue,na.rm=TRUE),
wm3 = weighted.mean(Month3,TotalDue,na.rm=TRUE),
wm4 = weighted.mean(Month4,TotalDue,na.rm=TRUE),
wm5 = weighted.mean(Month5,TotalDue,na.rm=TRUE),
wm6 = weighted.mean(Month6,TotalDue,na.rm=TRUE),
wm7 = weighted.mean(Month7,TotalDue,na.rm=TRUE),
wm8 = weighted.mean(Month8,TotalDue,na.rm=TRUE),
wm9 = weighted.mean(Month9,TotalDue,na.rm=TRUE),
wm10 = weighted.mean(Month10,TotalDue,na.rm=TRUE),
wm11 = weighted.mean(Month11,TotalDue,na.rm=TRUE),
wm12 = weighted.mean(Month12,TotalDue,na.rm=TRUE),
wm13 = weighted.mean(Month13,TotalDue,na.rm=TRUE),
wm14 = weighted.mean(Month14,TotalDue,na.rm=TRUE),
wm15 = weighted.mean(Month15,TotalDue,na.rm=TRUE),
wm16 = weighted.mean(Month16,TotalDue,na.rm=TRUE),
wm17 = weighted.mean(Month17,TotalDue,na.rm=TRUE),
wm18 = weighted.mean(Month18,TotalDue,na.rm=TRUE)
)
这样的事情:
testResult1.2<-ddply(payments_All_cum_pct,"Vintage",summarise,
N_obs = sum(!is.na(TotalDue)),
Sum_TotalDue = sum(TotalDue, na.rm=TRUE),
Mean_TotalDue = mean(TotalDue,na.rm = TRUE),
**for (i in 1:18)** {
**wm(i)** = weighted.mean(**Month(i)**,TotalDue,na.rm=TRUE),
}
)
如果我尝试一个函数并在函数内连接“wm”和“Month”与 i 我得到一个字符串作为回报而不是一个对象... 我需要能够以某种方式打印一系列将由系统正确读取的脚本。
我正在从SAS过渡 - 分析师可以在宏中打印他/她想要的任何脚本行,并且它们将被包含在执行中。
请在下面找到我的数据集示例:
structure(list(TotalDue = c(581.2, 1187.61, 763.04, 44.73, 250.5
), Vintage = c(201306, 201306, 201306, 201306, 201306), EasyClientMerge = c(400036L,
400037L, 400040L, 400045L, 400047L), Month1 = c(0, 0, 0, 0, 0
), Month2 = c(25.81, 15.16, 65.53, 100, 100), Month3 = c(53.34,
15.16, 100, 100, 100), Month4 = c(87.75, 15.16, 100, 100, 100
), Month5 = c(100, 15.16, 100, 100, 100), Month6 = c(100, 15.16,
100, 100, 100), Month7 = c(100, 15.16, 100, 100, 100), Month8 = c(100,
15.16, 100, 100, 100), Month9 = c(100, 15.16, 100, 100, 100),
Month10 = c(100, 15.16, 100, 100, 100), Month11 = c(100,
15.16, 100, 100, 100), Month12 = c(100, 15.16, 100, 100,
100), Month13 = c(100, 15.16, 100, 100, 100), Month14 = c(100,
15.16, 100, 100, 100), Month15 = c(100, 15.16, 100, 100,
100), Month16 = c(100, 15.16, 100, 100, 100), Month17 = c(100,
15.16, 100, 100, 100), Month18 = c(100, 15.16, 100, 100,
100)), .Names = c("TotalDue", "Vintage", "EasyClientMerge",
"Month1", "Month2", "Month3", "Month4", "Month5", "Month6", "Month7",
"Month8", "Month9", "Month10", "Month11", "Month12", "Month13",
"Month14", "Month15", "Month16", "Month17", "Month18"), row.names = c(2L,
3L, 4L, 8L, 9L), class = "data.frame")
有什么建议吗?
BR
答案 0 :(得分:0)
总猜测,没有可重复的例子,但我认为tapply将解决你的问题。
这将循环一个由一系列因素细分的数据帧。 I've written an entry level tutorial on my blog