2016-03-15 88 views
0

我想彙總並報告下面每個BOM的WW的最小值和最大值。R爲實體的每個值彙總最小值和最大值的編程

BOM = c("A","A","A","A","A","B","B","B","B","B","C","C","C","C") 

WW = c("201305","201306","201308","201310","201311","201345","201346","201347","201349","201352","201336","201337","201339","201341") 

df <- data.frame(BOM,WW) 

我試圖這樣做以下,但不幸的是我得到了全球最小和最大WW,不是針對每個單獨的BOM,我想它。

df <- group_by(df, BOM) 

extremes <- summarise(df, minw=min(as.numeric(WW)), maxw=max(as.numeric(WW))) 

你能幫忙嗎? 下面是我期待如果代碼正常工作的最終結果。

BOM= c("A","B","C") 

minWW = c("201305","201345","201336") 

maxWW = c("201311","201352","201341") 

df2 <-data.frame(BOM,minWW,maxWW) 

非常感謝。

+0

你爲什麼要建立WW爲特徵向量?或者他們是否是日期? – Jav

+1

您可以檢查是否還加載了「plyr」。如果是這種情況,請使用'dplyr :: summarize(df,....'應該可以解決它(因爲函數被屏蔽) – akrun

+0

謝謝,這也起到了作用 – GabStacker

回答

1

假設WW是數值型變量:

BOM = c("A","A","A","A","A","B","B","B","B","B","C","C","C","C") 

# Next time just remove "" around your varibales to import as numeric 

WW = as.numeric(c("201305","201306","201308","201310","201311","201345","201346","201347","201349","201352","201336","201337","201339","201341")) 


df <- data.frame(BOM,WW) 

使用聚合:

aggregate(WW ~ BOM, data = df, min) 

    BOM  WW 
1 A 201305 
2 B 201345 
3 C 201336 

aggregate(WW ~ BOM, data = df, max) 

    BOM  WW 
1 A 201311 
2 B 201352 
3 C 201341 
+0

這完成了彙總。 – GabStacker

相關問題