2014-07-25 2 views
-1

난 내가 뭔가를 같이 할이R - 총 두 개의 열

id1 id2 attr 
------------------ 
11    a  
11    a  
     11  a 
     11  b 
     11  c 
22    a 
22    a 
     22  a 
     22  a 
33    d 
44    e 

과 같은 데이터 프레임을 가지고있다. id1, id2는 카운트 (빈도)입니다.

id1 id2 attr 
------------------ 
2    a  
     1  a 
     1  b 
     1  c 
2    a 
     2  a 
1    d 
1    e 

간격에 값이 없으므로 필요한 경우 NA로 채울 수 있습니다. 나는 집계 함수를 사용해 보았지만 원하는 출력을 얻을 수 없었다. 고맙습니다.

+1

''22 (33)이 편집 된''' – phonixor

+0

죄송합니다. – user2575429

+1

잘'''plyr'' 패키지는'''count'' 함수를 가지고 있습니다. 당신을 꽤 가까이에 가져옵니다. – phonixor

답변

3

이것은 데이터

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 
+0

고맙습니다. 비슷한 것을 시도했지만 해결책이 더 좋습니다. – user2575429

+0

@jalapic. 'id2' 열의 출력은 user2575249가 보여준 예상 출력과 다를 수 있습니다. – akrun

+0

@akrun 좋은 캐치 - 고유 한 사례가 충분하지 않아 '일치'가 작동하지 않습니다. 내가 그것을 ifelse 솔루션으로 바 꾸었습니다. – jalapic

1

당신이 원하는 방법이 방법의 결과가 정확하게 포맷되지 않은를 사용하여 작동하는 것 같다, 이해하는 것이 더 간단 할 수도 있습니다.

# load library 
library(dplyr) 

# your data 
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)) 

# tally counts the number of observations 
dat %>% 
    group_by(id1,id2,attr) %>% 
    tally 

# output 
Source: local data frame [8 x 4] 
Groups: id1, id2 

    id1 id2 attr n 
1 11 NA a 2 
2 22 NA a 2 
3 33 NA d 1 
4 44 NA e 1 
5 NA 11 a 1 
6 NA 11 b 1 
7 NA 11 c 1 
8 NA 22 a 2 
+0

+1 '집계' – akrun

0

변명 불쌍한 내 R 코드는하지만 당신은 할 수 원하는 것을 만들기 위해, 나는 틀에 얽매이지 않는 일을해야했다. 코드는 불행하게도 확장 성이별로 없습니다. 확실히 향상시킬 수 있지만 예제 출력을 제공합니다. 유일한 차이점은 입력 값에 빈 공간에 NAs가 있다고 가정됩니다.

#Concatenate each row to a single value and find the unique rows 
unique.pasted<-apply(rawdata[!duplicated(rawdata),],1,paste,collapse="-") 

#Concatenate each row 
pasted.rows<-apply(rawdata,1,paste,collapse="-") 

#Get frequencies and maintain row order 
frequencies<-table(pasted.rows)[unique.pasted] 

#Separate id1 and id2 
id1.freq<-frequencies 
id1.freq[is.na(rawdata[!duplicated(rawdata),"id1"])]<-NA 
id2.freq<-frequencies 
id2.freq[is.na(rawdata[!duplicated(rawdata),"id2"])]<-NA 

#Obtain the final table 
final.table<-data.frame(id1=id1.freq,id2=id2.freq,attr=rawdata[!duplicated(rawdata),"attr"]) 

#Remove row names 
row.names(final.table)<-NULL 

#Replace NA with empty values 
final.table[is.na(final.table)]<-"" 
final.table 

id1 id2 attr 
1 2  a 
2  1 a 
3  1 b 
4  1 c 
5 2  a 
6  2 a 
7 1  d 
8 1  e 
2

사용 @jfreels의 dplyr에서 tally의 사용과 ID2의 (33)가`행에서 처리하는 방법을 dat

library(dplyr) 

dat1 <- dat%>% 
     group_by(id1,id2, attr) %>% 
     tally() 
dat2 <- dat %>% 
     unique() 

left_join(dat2,dat1) %>% 
mutate(id1=ifelse(!is.na(id1), n, NA),id2=ifelse(!is.na(id2), n, NA)) %>% 
select(-n) 
#Joining by: c("id1", "id2", "attr") 
# id1 id2 attr 
#1 2 NA a 
#2 NA 1 a 
#3 NA 1 b 
#4 NA 1 c 
#5 2 NA a 
#6 NA 2 a 
#7 1 NA d 
#8 1 NA e