2012-01-27 235 views
2

介紹R:分組/按類別組合拆分數據集apply.weekly()

我不是的R專家又那麼請原諒我大概應該是尷尬的問了另一個問題。在another question我對stackoverflow問了一些關於如何通過 apply.weekly()函數將xts對象的非規則日常數據聚合到每週值的一些非常有用的意見。不幸的是,我沒有找到像tapply(),ddply(),by()aggregate()這樣的功能,它允許按照與apply.weekly()函數一起工作的類別進行分割。

我的數據

這是我的示例數據集。我已經在其他問題上發佈了。爲了便於說明,我走的是自由還張貼在這裏:

example <- as.data.frame(structure(c(" 1", " 2", " 1", " 2", " 1", " 1", " 2", " 1", " 2", 
" 1", " 2", " 3", " 1", " 1", " 2", " 2", " 3", " 1", " 2", " 2", 
" 1", " 2", " 1", " 1", " 2", NA, " 2", NA, NA, " 1", " 3", " 1", 
" 3", " 3", " 2", " 3", " 3", " 3", " 2", " 2", " 2", " 3", " 3", 
" 3", " 2", " 2", " 3", " 3", " 3", " 3", " 1", " 2", " 1", " 2", 
" 2", " 1", " 2", " 1", " 2", " 2", " 2", " 3", " 1", " 1", " 2", 
" 2", " 3", " 3", " 2", " 2", " 1", " 2", " 1", " 1", " 2", NA, 
" 2", NA, NA, " 1", " 3", " 2", " 3", " 2", " 0", " 3", " 3", 
" 3", " 2", " 0", " 2", " 3", " 3", " 3", " 0", " 2", " 2", " 3", 
" 3", " 0", "12", " 5", " 9", "14", " 5", "tra", "tra", "man", 
"inf", "agc", "07-2011", "07-2011", "07-2011", "07-2011", "07-2011" 
), .indexCLASS = c("POSIXlt", "POSIXt"), .indexTZ = "", class = c("xts", 
"zoo"), .indexFORMAT = "%U-%Y", index = structure(c(1297642226, 
1297672737, 1297741204, 1297748893, 1297749513), tzone = "", tclass = c("POSIXlt", 
"POSIXt")), .Dim = c(5L, 23L), .Dimnames = list(NULL, c("rev_sit", 
"prof_sit", "emp_nr_sit", "inv_sit", "ord_home_sit", "ord_abr_sit", 
"emp_cost_sit", "usage_cost_sit", "tax_cost_sit", "gov_cost_sit", 
"rev_exp", "prof_exp", "emp_nr_exp", "inv_exp", "ord_home_exp", 
"ord_abr_exp", "emp_cost_exp", "usage_cost_exp", "tax_cost_exp", 
"gov_cost_exp", "land", "nace", "index")))) 

「rev_sit」, 「prof_sit」, 「emp_nr_sit」, 「inv_sit」, 「ord_home_sit」,「ord_abr_sit 「,emp_cost_sit,usage_cost_sit,tax_cost_sit,gov_cost_sit,rev_exp,prof_exp,emp_nr_exp,inv_exp,ord_home_exp,ord_abr_exp,emp_cost_exp,usage_cost_exp, 「tax_cost_exp」,「gov_cost_exp」,

是指調查中的問題。有三個應答可能性代碼爲「1」,「2」和「3」。

「地」, 「NACE」

是類別與分別爲16分8個唯一的因素。

我的目標 我的目標是通過周在「NACE」和「地」的類別因素的每一種組合計數的「1」,「2」,「3」各自的發生。我的想法是創建的每個回答的可能性{1,2,3}事先二元矢量(example_1,example_2,example_2),然後應用類似:

apply.weekly(example_1, function(d){ddply(d,list(example$nace,example$land),sum)}) 

但是,這並不工作,既不ddplyaggregateby

我的目標

我的不專業的工作圍繞最初不是爲了創建一個時間序列,只是一個最新矢量example$date用給定的時間列通過%V編碼爲每週然後使用,例如:

tapply(example_1[,5], list(example$date,example$nace,example$land),sum) 

我當然不會對上述顯示的20個問題中的每一個都做。我然後得到ie例如:

week1,nace1.land1,nace1.land2,nace1.land3,...,nace1.land16,nace2.land1,..,nace8.land16 week2,nace1.land1, nace1.land2,nace1.land3,...,nace1.land16,nace2.land1,..,nace8.land16 ... ... weekn,nace1.land1,nace1.land2,nace1.land3,...,nace1.land16,nace2.land1,..,nace8.land16

我也必須爲2(example_2)和3(example_3),對於20個問題中的每個問題,這個問題全部產生16 * 8 * 3 * 20 = 7680列。這種極端和此方法的產品不是一個時間序列,因此它不按周順序排列。

摘要

因此,誰能教我或者給我一個提示如何使用該功能apply.weekly()結合功能的那種tapply()ddply()by()split()unstack()等,或任何其它方法實現如上所述的分組。每一個提示真的很感激。我非常沮喪,已經想着放棄我的R實驗,並改變回stata,在那裏有很多東西比collapse()by()等更直觀。但是不明白我的錯誤:我熱衷於學習,所以請幫助我!

+2

我不能相信你在相同的句子中使用「Stata」和「直覺」(沒有否定)...... – 2012-01-27 20:11:40

+0

@DWin:我認爲這取決於你想做什麼。但是在stata中,你有一個by()函數,它只給出總和b一個特定的索引而不直接崩潰。因此對於一週內的所有日期,您具有相同的價值。 – chameau13 2012-01-27 20:35:30

+0

錯誤消失與'require(xts)'。 – 2012-01-27 20:36:01

回答

1

非常感謝您的幫助。在此期間,我忙於其他一些事情,但現在我正在處理我的問題,並在您的好評中幫助我找到了解決方案:

我放棄了直接使用時間序列的工作,推遲了這一步直到我的分析結束。因此,我把日期載體,並轉化成周:

庫(ISOweek) d $指數< - ISOweek(d $日期)

(我這樣做與ISOweek因爲我使用的是Windows)

然後我使用tapply和lapply的組合。以下函數計算每個日曆周(d $ index = t [[22]])和兩個分類列t [[21]]的每個組合的調查中的肯定答案數(由1編碼) [22]]。在同一步驟中,整個事物轉換爲數據幀:

groupweeksums < - function(x,t){as.data.frame(tapply((x == 1)* 1,list(t [[ 23]],t [[21]],t [[22]]),函數(d)sum(d,na。rm = TRUE)))}

==> x表示特定列,t表示數據幀(我不知道該怎麼做,否則因爲在某一點我必須在另一個地址處尋址列數據框和我想避免大量輸入);如果d然後將數據幀:

DF < - groupweeksums(d,d)

,以便我不必重複此過程爲所有的我的20個問題是使用l​​apply:

DF < - as.data.frame(lapply(DF [,1:20],函數(d)groupweeksums(d,euwifo)))

這使我與所有我需要用於進一步分析漂亮的數據幀。感謝您的幫助,您的有用評論讓我越來越接近解決方案!

P.S.我也會發布這個答案給我發佈在連接到這一個的stackoverflow上的其他問題。我希望這不是問題,也不違反任何規則。

0

根據您的建議,我會添加一個「星期」列,但在處理之前將數據轉換爲高度格式 - 如果需要,您可以將其轉換回時間序列。

library(reshape2) 
d <- melt(example, id.vars=c("land", "nace", "index")) 
# You apparently want one of the followings 
dcast(d, land + nace + index ~ value, length) 
dcast(d, land + nace + index + variable ~ value, length) 
dcast(d, land + nace + index ~ variable + value, length) 

同樣地,你可以使用ddply

library(plyr) 
d <- melt(example, id.vars=c("land", "nace", "index")) 
ddply(d, 
    c("land", "nace", "index", "value"), 
    summarize, 
    number=length(value) # The argument "value" does not play any role 
) 

index列包含一週的本年度(%Y-%U)數:這是否所有的日期是在同一個只會工作公曆年。使用實際日期而不是週數可能更安全,例如本週開始時的星期日 - 它還可以更容易地將結果轉換爲時間序列。

week_start <- function(u) as.Date(u) - as.numeric(format(u, "%u")) 
example$index <- weekstart(as.POSIXct(rownames(example))) 
# The following may also work. 
example$index <- format(as.POSIXct(rownames(example)), "%G-%V") 
+0

非常感謝!我會在當天晚些時候嘗試這個解決方案,並會報告我的成功!最好的問候,andreas – chameau13 2012-01-28 08:48:19

+0

對不起!我沒有找到時間來測試它。我現在有點匆忙。但我會盡快報告。 – chameau13 2012-01-29 12:50:07