2013-08-15 2 views
0

dcast Rcodes가 더 이상 실행되지 않습니다. 여기에서 문제를 논의했습니다 : segfault in R using reshape2 package and dcastdcast (reshape2)를 사용하지 않고 R로 데이터 재 형성

버그가 아직 수정되지 않았으므로 다른 방법으로 내 캐스트를 얻을 수 있습니다. 어떤 제안이라도 대단히 감사하겠습니다!

내 데이터 세트의 아주 작은 수치 이하. 기본적으로 조사 ID ("EID") 당 종당 하나의 항목이 있습니다. 설문 ID ("EID") 당 하나의 항목을 모든 종에서 관련 값 ('값') 즉 와이드 형식의 열로 가져오고 싶습니다. 끝에

> dput(sample) 
structure(list(EID = c("L00155/69/2000-09-06", "Q99107/178/1999-08-23", 
"G02192/1/2002-07-08", "G97158/1/1997-10-26", "Q06091/2/2006-07-04", 
"L00004/171/2000-03-01", "G11094/15/2011-09-05", "Q04127/16/2004-07-28", 
"Q02122/230/2002-10-29", "G08002/6/2008-02-03", "Q99006/143/1999-02-17", 
"Q08053/3/2008-06-12", "Q99128/22/1999-08-19", "L00177/83/2000-12-18", 
"Q05122/11/2005-08-30", "Q04156/44/2004-10-29", "L01097/69/2001-06-26", 
"G08004/169/2008-05-14", "Q03041/26/2003-06-14", "G98115/60/1998-09-11", 
"G00002/20/2000-01-17", "G00002/20/2000-01-17", "G00054/1/2000-05-31", 
"G00054/1/2000-05-31"), tspp.name = structure(c(13L, 13L, 13L, 
13L, 16L, 13L, 13L, 4L, 13L, 13L, 13L, 13L, 13L, 11L, 4L, 13L, 
13L, 13L, 13L, 20L, 13L, 13L, 24L, 24L), .Label = c("American plaice", 
"American sand lance", "Arctic cod", "Atlantic cod", "Atlantic halibut", 
"Atlantic herring", "Bigeye tuna", "Black dogfish", "Bluefin tuna", 
"Capelin", "Greenland halibut", "Lookdown", "Northern shrimp", 
"Ocean quahog", "Porbeagle", "Redfishes", "Slenteye headlightfish", 
"Smooth flounder", "Spiny dogfish", "Striped pink shrimp", "Summer flounder", 
"White hake", "Winter flounder", "Witch flounder", "Yellowtail flounder" 
), class = "factor"), elasmo.name = structure(c(26L, 30L, 30L, 
30L, 30L, 25L, 21L, 30L, 30L, 30L, 30L, 21L, 30L, 5L, 30L, 30L, 
30L, 21L, 30L, 30L, 14L, 21L, 24L, 21L), .Label = c("Arctic skate", 
"Atlantic sharpnose shark", "Barndoor skate", "Basking shark", 
"Black dogfish", "Blue shark", "Deepsea cat shark", "Greenland shark", 
"Jensen's skate", "Little skate", "Manta", "Ocean quahog", "Oceanic whitetip shark", 
"Porbeagle", "Portuguese shark", "Rough sagre", "Roughtail stingray", 
"Round skate", "Sharks", "Shortfin mako", "Skates", "Smooth skate", 
"Soft skate", "Spiny dogfish", "Spinytail skate", "Thorny skate", 
"White shark", "White skate", "Winter skate", "NA"), class = "factor"), 
    elasmo.discard = c(1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 
    25, 0, 0, 0, 1, 0, 0, 1, 1, 15, 25)), .Names = c("EID", "tspp.name", 
"elasmo.name", "elasmo.discard"), class = "data.frame", row.names = c("18496", 
"488791", "87549", "236671", "139268", "15606", "11132", "115531", 
"93441", "159675", "403751", "42587", "485941", "19285", "130395", 
"119974", "73826", "7953", "99124", "351461", "71", "72", "184", 
"185")) 

, 나는이 취득하고자 다음 "dcast"코드가 여기에 작동

library(plyr) 
test<-dcast(sample, ...~elasmo.name,value.var ="elasmo.discard",fun.aggregate=sum) 
test 

참고,하지만 난 내 전체 데이터 세트에서 실행할 때 치명적인 오류가 발생 할 수있는 145349 개의 행이 있습니다.

많은 감사합니다.

+2

이것은 실제로이 질문을하는 올바른 방법이 아닙니다. Segfault는 정의상 버그이므로 관리자에게 보내야합니다. 이것은 저자가 일반 SO 독자이기 때문에이 경우에 그 목적을 수행 할 수 있지만, 일반적으로 이메일만큼 정중하지는 않습니다. –

+0

확인. 덕분에 @DWin, 누군가가 나에게 dcast를 사용하지 않고 내 데이터 프레임을 바꿀 수있는 방법을 제안 할 수 있기를 바랍니다. – GodinA

+2

예제가 없어도 도움이되지 않습니다. – djhurio

답변

1

이것은 Hadley 이전 방법입니다. 먼저 합계를 구한 다음 다시 형성하십시오.

foo <- aggregate(d[,4,drop=FALSE], by=d[,1:3], sum) 
reshape(foo, v.names="elasmo.discard", idvar=c("EID", "tspp.name"), 
      timevar="elasmo.name", direction="wide") 

첫 번째 파트가 느린 경우 "by"부분에 더 적은 수의 열을 가질 수 있습니다. tspp.nameEID에 의해 정의 된 것처럼 보입니다. 그렇다면 실제로 집계하지 말고 사실 이후에 추가하십시오.

두 번째 부분이 느린 경우 여기에서 방법 중 하나를 시도하십시오 : https://stackoverflow.com/a/9617424/210673.

속도 향상에 대한 더 나은 도움말을 얻으려면 코드를 테스트 할 수있는 적절한 예제 (예 : 샘플 또는 담당자 사용)를 제공하십시오. 솔루션 속도는 종종 변수의 고유 한 조합 수에 따라 달라집니다.

+0

감사합니다 @ 애론, 내 데이터 집합에 사용하려고하지만 몇 가지 문제가 있습니다 ... drop = F는 무엇을 의미합니까? – GodinA

+0

그냥 단순한 벡터 대신 단일 열 데이터 프레임으로 유지합니다. 그 이유는 컬럼의 이름이 보존되도록하기 위해서입니다. – Aaron

+0

감사합니다! 그것은 작동하지만 ... 그것은 몇 번 조용히 걸립니다! 내가 이렇게 빨리 할 수있는 방법이 있니? – GodinA

0

오류를 재현 할 수 없습니다. 첨부 된 코드를 참조하십시오. 행 번호 을 196608으로 증가 시켰습니다.

아마 sample$elasmo.name에있는 범주 수가 중요 할 것입니다.

library(reshape2) 

sample <- structure(list(EID = c("L00155/69/2000-09-06", "Q99107/178/1999-08-23", 
    "G02192/1/2002-07-08", "G97158/1/1997-10-26", "Q06091/2/2006-07-04", 
    "L00004/171/2000-03-01", "G11094/15/2011-09-05", "Q04127/16/2004-07-28", 
    "Q02122/230/2002-10-29", "G08002/6/2008-02-03", "Q99006/143/1999-02-17", 
    "Q08053/3/2008-06-12", "Q99128/22/1999-08-19", "L00177/83/2000-12-18", 
    "Q05122/11/2005-08-30", "Q04156/44/2004-10-29", "L01097/69/2001-06-26", 
    "G08004/169/2008-05-14", "Q03041/26/2003-06-14", "G98115/60/1998-09-11", 
    "G00002/20/2000-01-17", "G00002/20/2000-01-17", "G00054/1/2000-05-31", 
    "G00054/1/2000-05-31"), tspp.name = structure(c(13L, 13L, 13L, 
    13L, 16L, 13L, 13L, 4L, 13L, 13L, 13L, 13L, 13L, 11L, 4L, 13L, 
    13L, 13L, 13L, 20L, 13L, 13L, 24L, 24L), .Label = c("American plaice", 
    "American sand lance", "Arctic cod", "Atlantic cod", "Atlantic halibut", 
    "Atlantic herring", "Bigeye tuna", "Black dogfish", "Bluefin tuna", 
    "Capelin", "Greenland halibut", "Lookdown", "Northern shrimp", 
    "Ocean quahog", "Porbeagle", "Redfishes", "Slenteye headlightfish", 
    "Smooth flounder", "Spiny dogfish", "Striped pink shrimp", "Summer flounder", 
    "White hake", "Winter flounder", "Witch flounder", "Yellowtail flounder" 
), class = "factor"), elasmo.name = structure(c(26L, 30L, 30L, 
    30L, 30L, 25L, 21L, 30L, 30L, 30L, 30L, 21L, 30L, 5L, 30L, 30L, 
    30L, 21L, 30L, 30L, 14L, 21L, 24L, 21L), .Label = c("Arctic skate", 
    "Atlantic sharpnose shark", "Barndoor skate", "Basking shark", 
    "Black dogfish", "Blue shark", "Deepsea cat shark", "Greenland shark", 
    "Jensen's skate", "Little skate", "Manta", "Ocean quahog", "Oceanic whitetip shark", 
    "Porbeagle", "Portuguese shark", "Rough sagre", "Roughtail stingray", 
    "Round skate", "Sharks", "Shortfin mako", "Skates", "Smooth skate", 
    "Soft skate", "Spiny dogfish", "Spinytail skate", "Thorny skate", 
    "White shark", "White skate", "Winter skate", "NA"), class = "factor"), 
     elasmo.discard = c(1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 
     25, 0, 0, 0, 1, 0, 0, 1, 1, 15, 25)), .Names = c("EID", "tspp.name", 
    "elasmo.name", "elasmo.discard"), class = "data.frame", row.names = c("18496", 
    "488791", "87549", "236671", "139268", "15606", "11132", "115531", 
    "93441", "159675", "403751", "42587", "485941", "19285", "130395", 
    "119974", "73826", "7953", "99124", "351461", "71", "72", "184", 
    "185")) 

n <- nrow(sample) 
N <- 145349 
p <- ceiling(log2(N/n)) 
n * 2^p 
n * 2^p > N 

# Bad way of increasing the row number 
for (i in 1:p) sample <- rbind(sample, sample) 

nrow(sample) 

class(sample) 
head(sample) 

table(sample$elasmo.name) 
table(as.character(sample$elasmo.name)) 

test <- dcast(sample, ... ~ elasmo.name, 
       value.var = "elasmo.discard", 
       fun.aggregate = sum) 
head(test) 
+0

hummm ... 재미있는 @djhurio. 나는 이것을 조금 더 살펴볼 것이다. 무엇이 잘못 될지 조용히하지 마라! – GodinA

관련 문제