zoowalk
zoowalk

Reputation: 2134

ddply - error msg when grouping over two variables

I am struggling with something what I think should be rather straight forward: The dataset below contains, among others, two variables: epis.start and epis.end; based on the combination of these two, I would like to assign them a grouping variable starting from 1, called epis.id.

I used the following command (.(DyadId) since in the full dataset there are many different dyads!):

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

However, I get the following error message:

Error: arguments imply differing number of rows

Any help? I started to wonder whether =id() is indeed the right command, but could not identify a different one. This entry also made me believe I am not that far off. Assigning group ID with ddply Many thanks!

(part of the) dataset:

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")

This is the outcome I am aiming for

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

Upvotes: 1

Views: 39

Answers (1)

BrodieG
BrodieG

Reputation: 52637

This should do it:

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

Note you need to put list around the variables as an argument to id. Produces:

   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

Upvotes: 1

Related Questions