Multiple columns of data and getting average R program

这一生的挚爱 提交于 2019-12-13 04:24:56

问题


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

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)

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

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


回答1:


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

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

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

EDIT correct the cut and simplify the code

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


来源:https://stackoverflow.com/questions/14994084/multiple-columns-of-data-and-getting-average-r-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!