1

I am new-ish to R and have what should be a simple enough question to answer; any help would be greatly appreciated.

The situation is I have a tab delimited data matrix (data matrix.txt) like below with group information included on the last column.

               sampleA    sampleB     sampleC   Group
    obs11        23.2      52.5       -86.3      1
    obs12       -86.3      32.5       -84.7      1
    obs41       -76.2      35.8       -16.3      2 
    obs74       23.2       32.5       -86.8      2
    obs82       -86.2      52.8       -83.2      3
    obs38       -36.2      59.5       -74.3      3

I would like to replace the values of each of the groups with the average value for that group

How can a group average rather than a row or column average be calculated in R?

And how can I use this value to replace original values? Is the replace() function useable in this situation or is that only for replacing two known values?

Thanks in advance

jakob-r
  • 6,824
  • 3
  • 29
  • 47
sinead
  • 269
  • 1
  • 4
  • 7

1 Answers1

1

The package ddply should do the trick.

dat <- as.data.frame(matrix(runif(80),ncol=4))
dat$group <- sample(letters[1:4],size=20,replace=T)
head(dat)

library(plyr)
ddply(.data = dat, .variables =.(group), colwise(mean))

Result

  group        V1        V2        V3        V4
1     a 0.4741673 0.7669612 0.5043857 0.5039938
2     b 0.3648794 0.5776748 0.4033758 0.5748613
3     c 0.1450466 0.5399372 0.2440170 0.5124578
4     d 0.4249183 0.3252093 0.5467726 0.4416924
jakob-r
  • 6,824
  • 3
  • 29
  • 47
  • Hi thanks for the tip, will look into getting this packae installed, I realised I should have been more specific in my question, how would you do this when the number of columns and rows in each group (and indded in each mxtrix) is unknown and varies from group to group? – sinead Nov 21 '12 at 16:54
  • Please be more precise and give an example wich illustrates the complexity of your problem. However, my example is independent from row and colum number. – jakob-r Nov 21 '12 at 20:54