0

I have a matrix of the following setup:

d <- read.table(text='Sample     Target    Value
Sample1    A          NA
Sample1    A          2
Sample1    A          3
Sample2    A          1
Sample2    A          2
Sample2    A          3
Sample1    B          1
Sample1    B          2
Sample1    B          3
Sample2    B          NA
Sample2    B          2
Sample2    B          3',  header=TRUE)

I would like to take the means of the Value column for each data repetition. So the mean of all rows that have Sample=Sample1 and Target=A, Sample=Sample2 and Target=B and so on and so forth.

The output should be a matrix like this:

result <- read.table(text='Sample     Target    Value
Sample1    A          mean
Sample2    A          mean
Sample1    B          mean
Sample2    B          mean' ,  header=TRUE)

I tried to solve this with aggregate(), but I am unsure how to code it, so it preserves the Sample and Target columns and disregard the NA

Thank you!

Kevin Roth
  • 93
  • 2
  • 8
  • 5
    `aggregate(Value ~ Sample + Target, data=d, FUN=mean)` – jogo Nov 15 '17 at 12:22
  • 1
    `d` and `result` are not an R matrices. They are data frames. – G. Grothendieck Nov 15 '17 at 12:28
  • Thank you very much! That seems to solve the problem. I am quite new to R, so I am not so familiar with the differences between dataframes and matrixes. I run into a problem though: In my origional code, the NA is in brackets, This causes all results to be NA Any idea why this is? – Kevin Roth Nov 15 '17 at 13:11
  • Edit: What I call a matrix and asked for a solution as a dataframe seems to be a list as `typeof(x)` returns `list` – Kevin Roth Nov 15 '17 at 13:23

1 Answers1

2

I think aggregate is ok in this case. Try:

aggregate(d$Value, list(d$Sample, d$Target), mean, na.rm = TRUE)
  Group.1 Group.2   x
1 Sample1       A 2.5
2 Sample2       A 2.0
3 Sample1       B 2.0
4 Sample2       B 2.5
Dejan
  • 21
  • 4