2014-08-27 3 views
0

plyr을 사용하여 논리적 인 코드를 공식화하는데 약간의 문제가 있습니다. 내 문제는 다음과 같이 샘플, 길이가 다른 두 개의 큰 dataframes을 포함한다 :데이터 프레임을 ddply로 서브 세트 한 다음 서브 세트에 adply와 함께 함수를 적용하십시오. R

dfSample <- 
structure(list(Type = structure(c(8L, 100L, 86L, 86L, 86L, 86L, 
33L, 8L, 105L, 44L, 36L, 107L, 107L, 78L, 33L, 105L, 99L, 10L, 
16L, 75L), .Label = c("Alumni Services", "Anti-Virus and Malware", 
"Application Integration", "Application Monitoring", "Application Testing", 
"Audio Visual Support", "Audio Visual Support - CLS", "Audio Visual Support - Non-CLS", 
"Backup Services", "Banner", "Bus and Law", "Business Analysis", 
"Careers", "Common Learning Spaces", "Communication and Marketing", 
"Computer Aided Assessment", "Conference Accounts", "Content Management", 
"Database Services", "Datacentre", "Desktop Monitoring", "Desktop Software", 
"Document Management", "Email", "Email Programs", "Encryption", 
"Eng and the Enviro", "Equipment Disposal", "Estates and Facilities", 
"Examination Papers", "Faculty Engagement", "Filestore Support Services", 
"Finance Services", "General Admin Services", "General InfoSec Advice", 
"Generic Accounts", "Grid Accounts (HPC)", "Health Sciences", 
"High Performance Computing (HPC)", "Hosted webspace (LAMP/IIS)", 
"HR and Payroll Services", "HR General", "HR Recruitment", "HR Systems", 
"Hub Rooms", "Humanities", "ICT Facilities", "ID Card Services", 
"Identity Management (User accounts)", "Identity Services", "Information Policy Breaches", 
"Information Risk Analysis", "iSolutions Admin Services", "iSolutions Administration", 
"IT Training and Development", "Large File Transfer", "Lecture Capture", 
"Lecture Capture - CLS", "Lecture Capture - Non-CLS", "Legacy Corporate Systems", 
"Library Services", "Licence Management", "Managed Print Service", 
"Management Servers", "Media Asset Management", "Media Support", 
"Medicine", "Meet and Greet", "Misuse and Security Incidents", 
"Misuse Of Systems", "Mobile Apps", "Mobile Devices", "Natural and Enviro Sci", 
"Network Access Services", "Network Services", "OS Builds", "Other Learning Systems", 
"Personal Filestore", "Personal web pages", "Phys and Applied", 
"Printing (Managed)", "Printing (Not MPS)", "Project Management and Resourcing", 
"Repair", "Reporting Services", "Request for Software", "Research Filestore", 
"Research Governance", "Research Management", "Research Output", 
    "Resource Filestore", "Risk Analysis and Assessment", "Security", 
"Self Service Help", "Server Monitoring", "Service Hosting", 
"ServiceLine", "Soc and Human Sci", "Software Configuration Management", 
"Software Licensing and Management", "Software Services", "SportRec", 
"Staff Accounts", "Staff Desktop Deployment", "Staff Desktop Services", 
"Staff Desktop Services (Not UoS Build)", "Student Accounts", 
"Student Admin Services", "Student Personal Workstations", "SUSSED", 
"Switchboard", "Switchboard Infrastructure", "System Access Request", 
"Telephony", "University Admin Services", "Unmanaged Printing", 
"Videoconferencing", "Videoconferencing - CLS", "Videoconferencing - Non-CLS", 
"Virtual Learning Environment (VLE)", "Visitor Accounts", "Web Statistics", 
"Windows Core Environment"), class = "factor"), Tkt.Category = structure(c(19L, 
17L, 17L, 17L, 17L, 17L, 2L, 19L, 5L, 2L, 9L, 9L, 9L, 4L, 2L, 
5L, 20L, 2L, 19L, 20L), .Label = c("Communication and Collaboration", 
"Corporate Services", "Data Centre", "Data Storage Services", 
"Desktop IT", "Faculty IT", "Help Services", "HR", "Identity Management (User accounts)", 
"Information Security", "Logistics", "Programmes and Projects", 
"Quality and Testing", "Research Services", "Security", "SLO Corporate Services", 
"Software", "Standard", "Teaching Services", "Underpinning Services", 
"Web Services"), class = "factor"), `CreateDateTime` = structure(c(1370087940, 
1370156160, 1370162340, 1370178840, 1370190000, 1370240400, 1370242920, 
1370243040, 1370243040, 1370243280, 1370243280, 1370243520, 1370243580, 
1370243880, 1370243880, 1370244000, 1370244120, 1370244240, 1370244300, 
1370244360), class = c("POSIXct", "POSIXt")), `ClosingDateTime` = structure(c(1374501300, 
1372068300, 1379062020, 1390487100, 1379062080, 1375090560, 1373984760, 
1370856420, 1370440140, 1370508240, 1370338080, 1370243820, 1370243700, 
1370255520, 1370341440, 1370248680, 1370353560, 1370338800, 1370257140, 
1374222600), class = c("POSIXct", "POSIXt"))), .Names = c("Type", 
"Tkt.Category", "CreateDateTime", "ClosingDateTime" 
), row.names = c(NA, 20L), class = "data.frame") 

그리고

DF2<- 
structure(list(DateTime = structure(c(1370041200, 1370052000, 
1370062800, 1370073600, 1370084400, 1370095200, 1370106000, 1370116800, 
1370127600, 1370138400, 1370149200, 1370160000, 1370170800, 1370181600, 
1370192400, 1370203200, 1370214000, 1370224800, 1370235600, 1370246400 
), class = c("POSIXct", "POSIXt"))), .Names = "DateTime", row.names = c(NA, 
20L), class = "data.frame") 

내가 포함 몇 가지 조건에 따라 dfSample의 부분 집합의 길이를 얻기 위해 노력하고 있어요 아래와 같이 각 Tkt.Category에 대한 DF2에서 데이터 :

QCalc <- function(m) { 
    adply(DF2, 1, transform, q=as.character(
           nrow(subset(m, CreateDateTime <= DateTime & 
               ClosingDateTime >= DateTime)))) 
} 

ServiceQueue <- ddply(dfSample, .(Tkt.Category), QCalc) 

이 작동하지 않는 것, 그래서 나는 짐작 나는 01,238,854,436,737의 기능을 공식화하는 방식에 문제가있을 수 있어야합니다 작품 아래의 코드 조각 이후 58,996,393,210 부분 나는 (Tkt.Category에 의해 그룹화되지 않음) 내 모든 데이터를 사용하고 있습니다 :

Q <- adply(DF2, 1, transform, q=as.character(
            nrow(subset(dfSample, CreateDateTime<= DateTime & 
                 `ClosingDateTime>= DateTime)))) 

ddply를 사용하여, 내가 오류 메시지가 객체 m이 '을 (를) 찾을 수 없습니다 수 있다는 것입니다. 누군가이 문제를 해결하기 위해 올바른 방향으로 나를 가리킬 수 있습니까?

답변

0

문제를 다시 이야기 할 수 있다면 문제를 해결하는 더 쉬운 방법을 찾을 수있을 것 같습니다. 목록에있는 모든 티켓 유형 및 모든 타임 스탬프, 몇 개의 티켓이 전에 시작했는지, 그 다음에 끝나는 지, 그 카테고리가 있는지 계산하고 싶습니다. SQL에서 우리는 같은 것을 작성합니다

SELECT Tkt.Category, DateTime, count(*) 
FROM dfSample join DF2 on 
CreateDateTime<= DateTime 
and ClosingDateTime>= DateTime 
GROUP BY Tkt.Category, DateTime 

을하지만이 SQL하지, 그 R을 - 그리고 기본 R은 허용하지 않습니다 (어쩌면 그것이 있어야하지만, 관계형 DB에서 데이터를 당기는?) 우리는 불평등을 사용하여 합병 할 것입니다. 그래서 그 대신 우리는 병합과 약간의 트릭을 할 수 있으며, 모두 함께 plyr을 피하기 :

dfSample$id <- rownames(dfSample) 
DFc <- merge(dfSample,DF2) 
DFlimited <- DFc[DFc$CreateDateTime <= DFc$DateTime & DFc$ClosingDateTime >= DFc$DateTime,] 
DFagg <- aggregate(id ~ Tkt.Category + DateTime, data = DFlimited, length) 

이 그것의 본질적으로 완전 외부 조인을하고 다음 필터링 때문에, 당신의 테이블의 크기에 따라 상당히 느려질 수 있습니다. 그렇다면 Data.Table 패키지를 살펴보십시오. 자세한 내용은 Stack Overflow 질문을 확인하십시오.

+0

두 데이터 프레임을 병합하는 동안 문제가 있습니다. 엉덩이는 길이가 다른데 (하나는 70,816 개의 행이 있고 다른 하나는 2921 개의 행을 가짐). 모두 = TRUE를 사용해 보았습니다. 그러나 컴퓨터를 멈추지 않고 계속 할 수있는 다른 방법이 있습니까? – NarT

+0

필자는 plyr을 사용하기를 원했기 때문에 라인을 더 내려 가면 유형과 Tkt로 그룹 수를 그룹화해야합니다. 나중에 카테고리를 선택하십시오. – NarT

관련 문제