2013-05-03 74 views
0

創建多個酒吧地塊拆分數據集我有一個大的數據集,看起來像這樣:通過分組變量一次

時間,雨,玻璃鋼

2005-09-30 11點45分00秒,1.1,1

2005-09-30 23:45:00,1.5,1

2005-10-01 23:45:00,0.2,2

2005-10-02 11 :45:00,0.3,2

2005-10-02 23:45:00,1.7,2

2005-10-03 11:45:00,0.8,2

2005-10-04 23時45分零零秒,1.4,4

我想爲每個組(grp,我的完整數據集中總共有65個組)創建單獨的數據框,併爲每個組生成一個條形圖。

到目前爲止,我做了一個清單:

X <- split(df, df$grp) 

str(X) 

Y <- lapply(seq_along(X), function(x) as.data.frame(X[[x]])[, 1:2]) 

lapply(seq_along(Y), function(x) {assign(grp[x], Y[[x]], envir=.GlobalEnv)}) 

產生這樣的:

>Y 

>[[1]] 

       time precip 

3 2005-10-01 11:45:00  0 

8 2005-10-03 23:45:00  0 

9 2005-10-04 11:45:00  0 




[[2]] 

       time precip 

1 2005-09-30 11:45:00 1.1 

2 2005-09-30 23:45:00 1.5 


... 

有沒有辦法到現在每個繪製這些讓我有不同的條形圖對於每個組,在X軸上的時間和在Y軸上的沉降?理想情況下,條形圖將全部位於相同的頁面或部分相同的輸出中。

另外,有沒有辦法導出每個這些作爲單獨的數據框?

預先感謝您!

回答

1

lattice爲這種事做。

使用RoyalTS的數據df

library(lattice) 
barchart(time~precip|grp, data=df) 

enter image description here

0

可以使用ggplot的小功能做簡單得多:

df_string <- 'time, precip, grp 
2005-09-30 11:45:00,1.1,1 
2005-09-30 23:45:00,1.5,1 
2005-10-01 23:45:00,0.2,2 
2005-10-02 11:45:00,0.3,2 
2005-10-02 23:45:00,1.7,2 
2005-10-03 11:45:00,0.8,2 
2005-10-04 23:45:00,1.4,4' 

con <- textConnection(df_string) 
df <- read.csv(con) 

# optional if the time isn't actually in a format R understands yet 
library(lubridate) 
df$time <- ymd_hms(df$time) 

library(ggplot2) 
p <- ggplot(df,(aes(time,precip))) 
p <- p + geom_bar(stat = "identity") 
p <- p + facet_wrap(~ grp) 
p 
+0

在再次閱讀你的問題,你要爲每個組單獨的圖形或在同一圖中的所有團體和僅僅傑出的(彩色?)某種方式? – RoyalTS 2013-05-03 04:04:24