이것은 데이터
dat<-structure(list(id1 = c(11L, 11L, NA, NA, NA, 22L, 22L, NA, NA,
33L, 44L), id2 = c(NA, NA, 11L, 11L, 11L, NA, NA, 22L, 22L, NA,
NA), attr = structure(c(1L, 1L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 4L,
5L), .Label = c("a", "b", "c", "d", "e"), class = "factor")), .Names = c("id1",
"id2", "attr"), class = "data.frame", row.names = c(NA, -11L))
필요한 출력은 일반적인 아니지만,이 'plyr'
library(plyr)
#use ddply and count to count the number of instances of each case in each id
temp<-ddply(dat, .(id1, id2), transform,
freq = count(attr))
#only keep unique rows
temp<-unique(temp)
#need to create an id column for whether there is 11,22,33,44 in either id1 or id2
temp$id<-pmax(temp$id1, temp$id2, na.rm=TRUE)
#order the rows into desired order
temp <- temp[order(temp$id, temp$attr),]
#use these ifelse statements to replace id1 and id2
temp$id1<-ifelse(is.na(temp$id1), NA, temp$freq.freq)
temp$id2<-ifelse(is.na(temp$id2), NA, temp$freq.freq)
#just keep variables you want
temp<-temp[c(1,2,3)]
temp
id1 id2 attr
1 2 NA a
7 NA 1 a
8 NA 1 b
9 NA 1 c
3 2 NA a
10 NA 2 a
5 1 NA d
6 1 NA e
''22 (33)이 편집 된''' – phonixor
죄송합니다. – user2575429
잘'''plyr'' 패키지는'''count'' 함수를 가지고 있습니다. 당신을 꽤 가까이에 가져옵니다. – phonixor