[英]R- loop over ddply
我需要獲取每一行的歷史記錄。 如果我的表是:
aa<-data.frame(tel=c(1,1,1,1,2,2,2,2,3,3), hora=c(1,2,4,4,1,1,3,4,1,2),
intentos=c(1,5,1,4,9,2,7,8,8,1), contactos=c(0,1,0,0,0,1,0,1,0,1))
我需要為每個電話獲取一種趨勢變量“ intentos”:用於安裝實際值/先前值,但用於每一行。 為第一個電話創建了1 = c(NA,5/1,1/5,4/1)。
我想要的表是:
tel hora intentos contactos created1
1 1 1 1 0 NA
2 1 2 5 1 5
3 1 4 1 0 0.2
4 1 4 4 0 4
5 2 1 9 0 NA
6 2 1 2 1 0.222222222
7 2 3 7 0 3.5
8 2 4 8 1 1.142857143
9 3 1 8 0 NA
10 3 2 1 1 0.125
我試圖創建一個函數傳遞給ddply:
g<-function (tbl) {x<-data.frame(tbl)
for (i in 2:length(tbl) ){
print(paste0(i-1))
print(tbl[i-1])
x[i,1]<-
tbl[i]/tbl[i-1] }
return (x)}
如果我在矢量上運行它,則可以工作。 因此,我嘗試將其傳遞給ddply函數:
library(plyr)
ddply(aa, .(tel), mutate, mean_hora=mean(intentos), min_hora=min(intentos), created1=g(intentos))
但是我收到以下錯誤:
rbind.fill不支持數據框列“ created1”
我的方法(通過一個函數來評估每個向量)可以嗎? 如何使用創建的函數獲得所需的結果?
library(dplyr)
a1<-group_by(df,tel)
mutate(a1,mycol=intentos/lag(intentos,1))
Source: local data frame [10 x 5]
Groups: tel
tel hora intentos contactos mycol
1 1 1 1 0 NA
2 1 2 5 1 5.0000000
3 1 4 1 0 0.2000000
4 1 4 4 0 4.0000000
5 2 1 9 0 NA
6 2 1 2 1 0.2222222
7 2 3 7 0 3.5000000
8 2 4 8 1 1.1428571
9 3 1 8 0 NA
10 3 2 1 1 0.1250000
#Or, using pipe notation:
df %>%
group_by(tel)%>%
mutate(mycol=intentos/lag(intentos,1))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.