下面的代码适用于我的示例数据(单个值为50),但是我无法创建一种方法来通过click.amount变量(具体为20:200)对结果进行参数化。变量定义为:
date =事件的时间戳记
ID =唯一确定用户
Group =分组变量
click =每个事件的点击次数(总是= 1)
click.amount =事件期间花费的金额
Correct.Outcome =我的代码应该返回什么
原因=结果为“ N”的原因
事件的结果为“ Y”的条件
对于示例代码N = 50,最终我想返回一个数据框,其中的行表示结果范围内每次click.amount的结果占结果“ Y”的比例和总click.amount。 20:200。我的实际数据在500K到100万行之间。如果存在一个更干净的解决方案而不使用dplyr,那么下面的代码仅表示我如何获得部分解决方案。 TIA
数据:
structure(list(date = c(1550245200, 1550245211, 1550245411, 1552566300,
1552566350, 1552567300, 1552567400, 1552567500, 1552568600, 1552568600,
1552568601, 1552568691, 1552568691, 1552568891, 1552568899, 1552568601,
1552568691, 1552568691, 1552568891, 1552568899), ID = c(1, 1,
1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 6, 7, 8), Group = c("A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "B", "B", "B", "B", "B"), clicks = c(1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), click.amount = c(60,
4.89, 12.84, 70, 52, 10, 33, 80, 54, 11.89, 15.24, 25, 2, 1,
1, 15.24, 25, 2, 1, 1), Correct.Outcome = c("N", "Y", "Y", "N",
"N", "Y", "Y", "N", "N", "Y", "Y", "N", "N", "N", "N", "N", "N",
"N", "N", "N"), Reason = c("over 50", NA, NA, "over 50", "over 50",
NA, NA, "cumulative amount over 50", "over 50", NA, NA, "cumulative amount over 50",
"cumulative amount over 50", "cumulative amount over 50", "cumulative clicks over 5 and cumulative amount over 50",
"Group B", "Group B", "Group B", "Group B", "Group B")), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
代码:
results=testdat%>% group_by(ID)%>%arrange(date)%>%
mutate(cum.clicks=cumsum(ifelse(click.amount<=50 & Group=="A",clicks,0)),
cum.clickamount=cumsum(ifelse(click.amount<=50 & Group=="A",click.amount,0)))%>%
mutate(Outcome=ifelse(click.amount<=50 & Group=="A" & cum.clicks<=5 & cum.clickamount<=50,"Y","N" ))%>%
select(Outcome,Correct.Outcome,Reason,everything())%>%
arrange(ID,date)%>%
mutate(check=ifelse(Outcome !=Correct.Outcome,1,0))%>% ungroup()%>%
summarize(prop50=mean(Outcome=="Y"),amount50=sum(click.amount[Outcome=="Y"]))
答案 0 :(得分:2)
我不确定自己在做什么,但是可以使用 public static string Convert(string s)
{
var ret = new StringBuilder((int) (71 + (s.Length * 1.1)));
ret.Append(@"{\rtf1\ansi\ansicpg1250\deff0{\fonttbl\f0\fswiss Helvetica;}\f0\pard ");
foreach (var letter in s)
{
switch (letter)
{
case '\\':
case '{':
case '}':
ret.Append('\\');
break;
case '\r':
ret.Append("\\par");
break;
}
ret.Append(letter);
}
ret.Append(" }");
return ret.ToString();
}
将dplyr转换为功能。
sapply
由reprex package(v0.2.1)于2019-03-17创建