如何按行分割数据框,然后处理块? [英] How to split a data frame by rows, and then process the blocks?
问题描述
网站年度峰值
ALBEN 5 101529.6
ALBEN 10 117483.4
ALBEN 20 132960.9
ALBEN 50 153251.2
ALBEN 100 168647.8
ALBEN 200 184153.6
ALBEN 500 204866.5
ALDER 5 6561.3
ALDER 10 7897.1
ALDER 20 9208.1
ALDER 50 10949.3
ALDER 100 12287.6
ALDER 200 13650.2
ALDER 500 15493.6
AMERI 5 43656.5
AMERI 10 51475.3
AMERI 20 58854.4
AMERI 50 68233.3
AMERI 100 75135.9
AMERI 200 81908.3
,我想为每个网站创建一个年
vs peak
的图。
另一个选择是使用 ggplot2 $中的
ddply
函数c $ c>库。但是你提到你主要想要做一个高峰与一年的情节,所以你也可以使用 qplot
:
A< - read.table(example.txt,header = TRUE)
库(ggplot2)
qplot(peak,year,data = A ,color = site,geom =line,group = site)
ggsave(peak-year-comparison.png)
alt text http://i32.tinypic.com/16nuza.png
另一方面,我喜欢David Smith的解决方案,允许将应用程序应用于多个处理器。
I have a data frame with several columns, one of which is a factor called "site". How can I split the data frame into blocks of rows each with a unique value of "site", and then process each block with a function? The data look like this:
site year peak
ALBEN 5 101529.6
ALBEN 10 117483.4
ALBEN 20 132960.9
ALBEN 50 153251.2
ALBEN 100 168647.8
ALBEN 200 184153.6
ALBEN 500 204866.5
ALDER 5 6561.3
ALDER 10 7897.1
ALDER 20 9208.1
ALDER 50 10949.3
ALDER 100 12287.6
ALDER 200 13650.2
ALDER 500 15493.6
AMERI 5 43656.5
AMERI 10 51475.3
AMERI 20 58854.4
AMERI 50 68233.3
AMERI 100 75135.9
AMERI 200 81908.3
and I want to create a plot of year
vs peak
for each site.
Another choice is use the ddply
function from the ggplot2
library. But you mention you mostly want to do a plot of peak vs. year, so you could also just use qplot
:
A <- read.table("example.txt",header=TRUE)
library(ggplot2)
qplot(peak,year,data=A,colour=site,geom="line",group=site)
ggsave("peak-year-comparison.png")
alt text http://i32.tinypic.com/16nuza.png
On the other hand, I do like David Smith's solution that allows the applying of the function to be run across several processors.
这篇关于如何按行分割数据框,然后处理块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!