I have a dataset that simplifies to something like this, let's call that dataset B
V1 V2 V3 V4
sample1 1 2 3
sample2 4 5 6
sample3 7 8 9
then I have another separate row (on its own) called blank,
it would look something like this.
V1 V2 V3 V4
blank 0.5 1.0 1.5
I would like to subtract blank to all the rows of B.
So far I've tried:
B[,2:ncol(B)] <- lapply(B[,2:ncol(B)], function(x) x - blank[,2:ncol(blank)])
B[,2:ncol(B)] <- sweep(B[,2:ncol(B)], 1, blank[,2:ncol(blank)])
B[,2:ncol(B)] <- B[,2:ncol(B)] - blank[,2:ncol(blank)])
B[,2:ncol(B)] <- for(i in 1:nrow(B)){B[ i ,2:ncol(B)] - blank[,2:ncol(B)]}
None of which would work. first one tells me that "replacement element 1 is a matrix/data of 1 row, need 3". Second one tells me "STATS is longer than the extent of 'dim(x)[MARGIN]'", changing margin into 2 does not solve the problem. The third one says "‘-’ only defined for equally-sized data frames". The fourth one returns me a blank matrix.
I've looked through the forum to the best of my ability, but they are limited to applying only one value across the entire dataset, I would like to subtract a whole row of values across the rest of the rows in a dataset.
The end result should look like this (no rounding required).
V1 V2 V3 V4
sample1 0.5 1.0 1.5
sample2 3.5 4.0 4.5
sample3 6.5 7.0 7.5