多列数据并获得平均R程序 [英] Multiple columns of data and getting average R program

查看:65
本文介绍了多列数据并获得平均R程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前问过这样的问题,但是我决定简化数据格式,因为我是R的新手,不了解发生了什么....这是问题

I asked a question like this before but I decided to simplify my data format because I'm very new at R and didnt understand what was going on....here's the link for the question How to handle more than multiple sets of data in R programming?

但是我编辑了数据的外观,并决定以这种格式保留它.

But I edited what my data should look like and decided to leave it like this..in this format...

X1.0   X X2.0 X.1
   0.9 0.9  0.2 1.2
  1.3 1.4  0.8 1.4

如您所见,我有四列数据,我正在处理的实际数据最多为2000个数据点.....列"X1.0"和"X2.0"指的是时间". ..so我想要的是基于我的2列时间分别为"X1.0"和"X2.0"的每100秒的"X"和"X.1"的平均值...我可以使用此命令

As you can see I have four columns of data, The real data I'm dealing with is up to 2000 data points.....Columns "X1.0" and "X2.0" refer "Time"...so what I want is the average of "X" and "X.1" every 100 seconds based on my 2 columns of time which are "X1.0" and "X2.0"...I can do it using this command

cuts <- cut(data$X1.0, breaks=seq(0, max(data$X1.0)+400, 400))
   by(data$X, cuts, mean)

但是,这只会给我一组数据的平均值....是"X1.0"和"X" ....我将如何做,以便我可以从多个数据中获取平均值一个数据集....我也想停止这种输出

But this will only give me the average from one set of data....which is "X1.0" and "X".....How will I do it so that I could get averages from more than one data set....I also want to stop having this kind of output

cuts: (0,400]
[1] 0.7
------------------------------------------------------------ 
cuts: (400,800]
[1] 0.805

请注意,输出每400秒完成一次....我真的想要列出这些切割的列表,这些切割是不同时间间隔的平均值...请帮助...我只是使用data=read.delim("clipboard")来获得我的数据进入程序

Note that the output was done every 400 s....I really want a list of those cuts which are the averages at different intervals...please help......I just used data=read.delim("clipboard") to get my data into the program

推荐答案

您想要获得什么输出有点令人困惑.

It is a little bit confusing what output do you want to get.

首先,我更改名字,但这是可选的

First I change colnames but this is optional

colnames(dat) <- c('t1','v1','t2','v2')

然后我将使用ave,它与by相似,但输出更好.我正在使用矩阵的技巧来索引列:

Then I will use ave which is like by but with better output. I am using a trick of a matrix to index column:

matrix(1:ncol(dat),ncol=2)  ## column1 is col1 adn col2...
     [,1] [,2]
[1,]    1    3
[2,]    2    4

然后我将此矩阵与apply一起使用.这里是整个解决方案:

Then I am using this matrix with apply. Here the entire solution:

cbind(dat,
      apply(matrix(1:ncol(dat),ncol=2),2,
     function(x,by=10){      ## by 10 seconds! you can replace this 
                             ## with 100 or 400 in you real data
     t.col <- dat[,x][,1]   ## txxx
     v.col <- dat[,x][,2]   ## vxxx
     ave(v.col,cut(t.col, 
                   breaks=seq(0, max(t.col),by)),
         FUN=mean)})
  )

编辑更正剪切并简化代码

cbind(dat,
     apply(matrix(1:ncol(dat),ncol=2),2,
           function(x,by=10)ave(dat[,x][,1], dat[,x][,1] %/% by)))
   X1.0   X X2.0 X.1       1         2
1   0.9 0.9  0.2 1.2  3.3000  3.991667
2   1.3 1.4  0.8 1.4  3.3000  3.991667
3   2.0 1.7  1.6 1.1  3.3000  3.991667
4   2.6 1.9  2.2 1.6  3.3000  3.991667
5   9.7 1.0  2.8 1.3  3.3000  3.991667
6  10.7 0.8  3.5 1.1 12.8375  3.991667
7  11.6 1.5  4.1 1.8 12.8375  3.991667
8  12.1 1.4  4.7 1.2 12.8375  3.991667
9  12.6 1.8  5.4 1.2 12.8375  3.991667
10 13.2 2.1  6.3 1.3 12.8375  3.991667
11 13.7 1.6  6.9 1.1 12.8375  3.991667
12 14.2 2.2  9.4 1.3 12.8375  3.991667
13 14.6 1.8 10.0 1.5 12.8375 10.000000

这篇关于多列数据并获得平均R程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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