私はこのデータフレームを持っています:
Date Visitor-ID
1 2018-01-01 1
2 2018-01-01 2
3 2018-01-01 3
4 2018-01-02 2
5 2018-01-02 3
6 2018-01-02 2
7 2018-01-03 2
8 2018-01-03 3
データフレームは次のコードで生成されます。
myDF=data.frame(c("2018-01-01","2018-01-01","2018-01-01","2018-01-02","2018-01-02","2018-01-02","2018-01-03","2018-01-03"),c(1,2,3,2,3,2,2,3))
names(myDF)=c("Date","Visitor-ID")
元のデータフレームをこの新しいデータフレームに変更したい:
Date day 0 day 1 day 2
1 2018-01-01 3 2 2
2 2018-01-02 2 2
3 2018-01-03 2
新しいデータフレームでは、各セルは行の指定された日にすでにそこにいたx日のユニーク訪問者の数です。
質問: どのコード行でこれを実行できますか?
回答:
回答№1は1これはあなたが必要なものですか?
library(tidyr)
library(dplyr)
df=myDF%>%group_by(Date)%>%summarise(s=list(`Visitor-ID`))# convert to list to find the intersection after merge
df["key"]=1# create a help key for merge , this will help to get the product combination
s=merge(df,df,by="key")
s["New"]=apply(s,1,function(x) length(intersect(x$s.x, x$s.y)))# find the intersection of each
s["day"]=as.Date(s$Date.y)-as.Date(s$Date.x)# get the date different
s=s[s$day>=0,]# filter only for the next day , which means we only look forward not backward
s[,c("Date.x","New","day")]%>%tidyr::spread(day,New)# reshape three column to matrix you need
Date.x 0 1 2
1 2018-01-01 3 2 2
2 2018-01-02 2 2 NA
3 2018-01-03 2 NA NA
回答№2の場合は0
コードはやや粗雑ですが、これはあなたのために働くはずです、
myDF=data.frame(c("2018-01-01","2018-01-01","2018-01-01","2018-01-02","2018-01-02","2018-01-02","2018-01-03","2018-01-03"),c(1,2,3,2,3,2,2,3))
names(myDF)=c("Date","Visitor-ID")
myDF$Date <- as.Date(myDF$Date)
num.days <- as.numeric(max(myDF$Date) - min(myDF$Date))
new.cols.names <- paste("day", 0:num.days)
unique.dates <- unique(myDF$Date)
final.df <- matrix(0, ncol = length(new.cols.names)+1, nrow = length(unique.dates))
for (i in 1:length(unique.dates)){
ids <- unique(myDF[myDF$Date == unique.dates[i], ]$`Visitor-ID`)
for (j in 0:(as.numeric(max(myDF$Date) - unique.dates[i]))){
final.df[i, j+2] <- sum(ids %in% myDF[myDF$Date == unique.dates[i] + j, ]$`Visitor-ID`)
}
}
final.df <- data.frame(final.df)
names(final.df) <- c("Date", new.cols.names)
final.df$Date <- unique.dates
これは機能しますが、大規模なデータセットの場合は時間がかかる場合があります。何らかの形で使用できる場合があります sapply
より効率的に。これがお役に立てば幸いです!