ddply - 对两个变量进行分组时的错误消息

时间:2014-08-22 12:36:44

标签: r plyr

我正在努力做一些我认为应该直截了当的事情: 下面的数据集包含两个变量:epis.start和epis.end;基于这两者的组合,我想为它们分配一个从1开始的分组变量,称为epis.id。

我使用了以下命令(。(DyadId),因为在完整数据集中有许多不同的对子!):

d <- ddply(d, .(DyadId), transform,
           epis.id=id(epis.start, epis.end))

但是,我收到以下错误消息:

Error: arguments imply differing number of rows

有任何帮助吗?我开始怀疑= id()确实是正确的命令,但无法识别不同的命令。这篇文章也让我相信我并不遥远。 Assigning group ID with ddply非常感谢!

(部分)数据集:

d<–structure(list(ConflictId = c("1-6", "1-6", "1-6", "1-6", "1-6", 
"1-6", "1-6", "1-6", "1-6", "1-6", "1-6", "1-6", "1-6", "1-6", 
"1-6", "1-6", "1-6", NA), DyadId = c(260L, 260L, 260L, 260L, 
260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 
260L, 260L, NA), Year = c(1946L, 1966L, 1967L, 1968L, 1979L, 
1980L, 1981L, 1982L, 1983L, 1984L, 1985L, 1986L, 1987L, 1988L, 
1990L, 1993L, 1996L, NA), StartDate2 = structure(c(-8616, -1097, 
-1097, -1097, 3651, 3651, 3651, 3651, 3651, 3651, 3651, 3651, 
3651, 3651, 7491, 8655, 9705, NA), class = "Date"), epis.start = c(1946L, 
1966L, 1966L, 1966L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
1979L, 1979L, 1979L, 1979L, 1990L, 1993L, 1996L, NA), epis.end = c(1946L, 
1968L, 1968L, 1968L, 1988L, 1988L, 1988L, 1988L, 1988L, 1988L, 
1988L, 1988L, 1988L, 1988L, 1990L, 1993L, 1996L, NA)), .Names = c("ConflictId", 
"DyadId", "Year", "StartDate2", "epis.start", "epis.end"), row.names = c("700", 
"701", "702", "703", "704", "705", "706", "707", "708", "709", 
"710", "711", "712", "713", "714", "715", "716", "NA"), class = "data.frame")

这是我的目标

ConflictId  DyadId  Year    StartDate2  epis.start  epis.end epis.id
1-6         260    1946     1946-05-31  1946         1946    1 
1-6         260    1966     1966-12-31  1966         1968    2
1-6         260    1967     1966-12-31  1966         1968    2
1-6         260    1968     1966-12-31  1966         1968    2
1-6         260    1979     1979-12-31  1979         1988    3
1-6         260    1980     1979-12-31  1979         1988    3
1-6         260    1981     1979-12-31  1979         1988    3
1-6         260    1982     1979-12-31  1979         1988    3
1-6         260    1983     1979-12-31  1979         1988    3
1-6         260    1984     1979-12-31  1979         1988    3
1-6         260    1985     1979-12-31  1979         1988    3 
1-6         260    1986     1979-12-31  1979         1988    3
1-6         260    1987     1979-12-31  1979         1988    3
1-6         260    1988     1979-12-31  1979         1988    3
1-6         260    1990     1990-07-06  1990         1990    4
1-6         260    1993     1993-09-12  1993         1993    5
1-6         260    1996     1996-07-28  1996         1996    6

1 个答案:

答案 0 :(得分:1)

这应该这样做:

ddply(d, .(DyadId), transform, epis.id=id(list(epis.start, epis.end), drop=T))

请注意,您需要将变量list作为id的参数。生产:

   ConflictId DyadId Year StartDate2 epis.start epis.end epis.id
1         1-6    260 1946 1946-05-31       1946     1946       1
2         1-6    260 1966 1966-12-31       1966     1968       2
3         1-6    260 1967 1966-12-31       1966     1968       2
4         1-6    260 1968 1966-12-31       1966     1968       2
5         1-6    260 1979 1979-12-31       1979     1988       3
6         1-6    260 1980 1979-12-31       1979     1988       3
7         1-6    260 1981 1979-12-31       1979     1988       3
8         1-6    260 1982 1979-12-31       1979     1988       3
9         1-6    260 1983 1979-12-31       1979     1988       3
10        1-6    260 1984 1979-12-31       1979     1988       3
11        1-6    260 1985 1979-12-31       1979     1988       3
12        1-6    260 1986 1979-12-31       1979     1988       3
13        1-6    260 1987 1979-12-31       1979     1988       3
14        1-6    260 1988 1979-12-31       1979     1988       3
15        1-6    260 1990 1990-07-06       1990     1990       4
16        1-6    260 1993 1993-09-12       1993     1993       5
17        1-6    260 1996 1996-07-28       1996     1996       6