我正在尝试根据某项数据每年更新的数据子集创建一个新列。
Name, Action, OrigAmt, Discount, NetAmt, Date
Aaa New 5 1 4 2014-07-04
Bbb Renewal 10 0 10 2015-03-02
Ccc Auto-Renew 8 0 8 2015-09-11
DDD Manual-Renew 14 5 9 2016-04-16
我正在尝试根据姓名确定每年的平均续签费用。所以最终结果应该是 命名为New(NetAmt)RenewalAmt1 RenewalAmt2(即,名称可能会逐年更新,我想知道它的续订金额) (如果名称未更新,则不应包含在列表中) 续签的类型无关紧要。
只有一张表,即我没有合并数据
Dplyr似乎是每个人都推荐的,并尝试过ifelse语句,但是如果要续订,我似乎无法这样做,而不是添加一列,而是根据名称每年在NetAmt上添加
#attempt using dplyr
listin$renewed <- ifelse(listin$Action=="Renewal - Manual" |
listin$Action=="Renewal - Auto" | listin$Action=="Transfer" , 1,0)
table(listin$renewed)
listin %>%
select(Name, Action, OrigAmt, Discounts, NetAmt) %>%
filter(Action== "Registration") %>%
mutate (listin$renewed ==1)
library (reshape2)
newlist <- melt(listin2, id=c("Name", "Action", "OrigAmt", "Discounts"))
Error in mutate_impl(.data, dots) :
Column `listin$renewed == 1` must be length 232 (the number of rows) or
one, not 4831
数据集链接:https://onedrive.live.com/view.aspx?resid=D421AEB4A27DFF87%21118457&id=documents