如何按行拆分数据框,然后处理块? [英] How to split a data frame by rows, and then process the blocks?

查看:22
本文介绍了如何按行拆分数据框,然后处理块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多列的数据框,其中之一是称为站点"的因素.如何将数据框拆分为每行具有唯一值站点"的块,然后使用函数处理每个块?数据如下所示:

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

我想为每个站点创建一个 yearpeak 的图.

and I want to create a plot of year vs peak for each site.

推荐答案

另一种选择是使用 ggplot2 库中的 ddply 函数.但是您提到您最想绘制峰值与年份的关系图,因此您也可以使用 qplot:

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")

另一方面,我确实喜欢 David Smith 的解决方案,它允许在多个处理器上运行该函数的应用.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆