I have a background data file and an experimental data file. What I need is to calculate the colMeans
from the background file, and subtract from the experimental data the corresponding average background reading.
This is easy in base r:
dataField1 <- "someField"
dataField2 <- "someField2"
ctrlMeans <- colMeans (read.csv ("ctrl.csv"))
exprData <- read.csv ("expr.csv")
exprData [, c(dataField1, dataField2)] <- exprData [, c(dataField1, dataField2)] - ctrlMeans [c(dataField1, dataField2)]
But I found the last step difficult to implement in dplyr. The best I can get is the following:
ctrlMeansTbl <- read_csv ('ctrl.csv') %>% summarize_all (mean)
exprDataTbl<- read_csv('expr.csv') %>% mutate (
dataField1 := !! quo (dataField1) - select (ctrlMeansTbl, !!quo (dataField1)),
dataField2 := !! quo (dataField2) - select (ctrlMeansTbl, !!quo (dataField2))
)
But this throws an error:
Error in rep_len(as.vector(e1), prod(dim(e2))) :
attempt to replicate non-vector
Just to be clear, the formats of ctrlMeansTbl
and exprDataTbl
(before the mutate) are as follows:
> head (ctrlMeansTbl)
# A tibble: 1 x 4
`someField1` `someField2` `someField3` `someField4`
<dbl> <dbl> <dbl> <dbl>
1 489.7096 74.24759 547.9139 16.0828
> head (donorSingle)
# A tibble: 6 x 4
`someField1` `someField2` `someField3` `someField4`
<dbl> <dbl> <dbl> <dbl>
1 132123.44 1560.74 166069.17 0.619378
2 11125.93 156.95 14045.20 0.620412
3 14590.51 243.82 18132.47 0.621446
4 76014.17 839.50 95961.42 0.623514
5 91344.17 1054.85 115226.85 0.627650
6 7651.86 146.73 9528.69 0.631786
Do anyone have any idea on this? Thanks!