2013-09-04 3 views
-2

다음 열이있는 데이터 프레임이 있습니다.텍스트 처리/정규식? in R

user_id: g17165fd2e0bba9a449857645bb6g3a9a7ef8e6c 

time: 1361553741 

url: a string with an url. 

URL은 언젠가 양식 https://SOMETHING.COM/NAME/forum/thread?thread_id=51 걸립니다.

각 사용자에게 시간 x와 y 사이에 각 thread_id를 방문한 시간을 알려주는 데이터 프레임을 만들고 싶습니다. 따라서 관측 수는 사용자 수와 같고 스레드 ID 수 + 1 (총 뷰 수)과 같습니다.

데이터 세트가 실제로 크기 때문에 병렬로 수행하면 절대로 필요한 것.

R에서이 작업을 수행하는 가장 좋은 방법은 무엇입니까?

고맙습니다.

추신 : @David 내가 언급 한 것과 같은 데이터 프레임을 생성하는 코드를 만들고 내 질문에 대한 완벽한 대답을 제공합니다.

set.seed(2) 
#make junk data 
dat <- data.frame(user=1:5, 
           time=1:20, 
           url=paste0("https://domain.com/forum/thread?  thread_id=",sample(5,20,T))) 
+4

재현 예는 만들어 낸 적은 양의 데이터를 사용하는 장난감 예가 될 수있다. 당신의 문제를 설명하기에 충분합니다. 해결책을 제시하고자하는 사람은 필연적으로이 일을해야 할 것입니다. –

+0

죄송합니다. 다음에 코드를 제공하기 위해 최선을 다할 것입니다. – Ignacio

답변

1

이 당신을 위해 작동 확신은 :

> library(plyr) 
> library(doMC) 
> library(reshape2) 
> 
> set.seed(2) 
> #make junk data 
> dat <- data.frame(user=1:5, 
+     time=1:20, 
+     url=paste0("https://domain.com/forum/thread?thread_id=",sample(5,20,T))) 
> head(dat) 
    user time           url 
1 1 1 https://domain.com/forum/thread?thread_id=1 
2 2 2 https://domain.com/forum/thread?thread_id=4 
3 3 3 https://domain.com/forum/thread?thread_id=3 
4 4 4 https://domain.com/forum/thread?thread_id=1 
5 5 5 https://domain.com/forum/thread?thread_id=5 
6 1 6 https://domain.com/forum/thread?thread_id=5 
> #subet within time range 
> dat <- dat[dat$time >=1 & dat$time <= 20,] 
> 
> #make threadID variable 
> dat$threadid <- gsub("^.*thread_id=",'',dat$url) 
> 
> 
> #register parallel cores 
> registerDoMC(4) 
> #count number of thread occurrences for each user (in parallel) 
> dat.new <- ddply(dat,.(user,threadid),summarize,threadcount=length(threadid),.parallel=TRUE) 
> #reshape data to be in the format you want 
> dat.new <- dcast(dat.new,user~threadid,value.var="threadcount",fill=0) 
> #add total views 
> dat.new$totalview <- rowSums(dat.new[,-1]) 
> dat.new 
    user 1 2 3 4 5 totalview 
1 1 1 0 1 0 2   4 
2 2 1 1 0 1 1   4 
3 3 0 1 1 1 1   4 
4 4 2 0 2 0 0   4 
5 5 1 0 2 0 1   4 
+0

그것은 아름다운 코드입니다, 정말 고마워요! – Ignacio