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!