【发布时间】:2016-03-14 00:02:43
【问题描述】:
我希望在数据框 (integrates2) 中添加一列,以按顺序计算重复项。下面是数据的样子:
name program date of contact helper column
John ffp 10/11/2014 2
John TP 10/27/2014 2
Carlos TP 11/19/2015 3
Carlos ffp 12/1/2015 3
Carlos wfd 12/31/2015 3
Jen ffp 9/9/2014 2
Jen TP 9/30/2014 2
这是在特定日期参加了特定计划的人员列表。我添加了一个帮助列来计算重复次数并对联系日期进行排序。我正在计算现有程序的组合(例如 ffp-tp、tp-ffp-wfd)。
为了做到这一点,我想实现以下代码,以便在名为“program2”的新列的帮助下转置有序组合:
#transpose the programs
require(reshape2) dcast(integrates2, name ~ program2, value.var=”program”)
然后我打算用下面的代码把结果转成表格和数据框并统计频率:
res = table(integrates2)
resdf = as.data.frame(res)
我在以下链接中看到了这个: Count number of time combination of events appear in dataframe columns ext
我需要的“program2”是这样的:
Name program date of contact helper column program2
John ffp 10/11/2014 2 1
John TP 10/27/2014 2 2
Carlos TP 11/19/2015 3 1
Carlos ffp 12/1/2015 3 2
Carlos wfd 12/31/2015 3 3
这样,我可以使用“program2”转置到不同的列,然后计算组合。最终结果应如下所示:
program pro1 pro2 freq
ffp tp 2
TP ffp wfd 1
我确信有更简单的方法可以做到这一点,但随着我的学习,这就是我所在的地方。感谢大家的帮助!
【问题讨论】:
标签: r