I have two dataframes:
df.means.refs:

df.target:

I need to loop through df.target and make the following substraction:
df.target$Cq - df.means.refs$Cq
df.target is group_by Tissue, Accession, Genotype, Gene, BReplicate
df.means.refs is group_by Tissue, Accession, Genotype, BReplicate
For example I need to calculate
df.target$Cq - df.means.refs$Cq (row 1),
df.target$Cq - df.means.refs$Cq (row 2),
df.target$Cq - df.means.refs$Cq (row 3),
so far no problem. But now
df.target$Cq - df.means.refs$Cq (row 4 in df.target - row1 in df.means.refs)
df.target$Cq - df.means.refs$Cq (row 5 in df.target - row2 in df.means.refs)
df.target$Cq - df.means.refs$Cq (row 6 in df.target - row3 in df.means.refs)
and so on.
Starting from df.target row 10 the column Genotype switches to WT and now I need to loop using the Cq values in df.means.refs according to Genotype:WT.
(essentially Cq values corresponding to 3 biological replicates per genotype, two different genotypes; three different genes tested and listed in df.target)
How do I code this in R?
Thank you very much for any help, Kay
The dataframes (via dput(): df.means.refs:
structure(list(Tissue = c("Seedling", "Seedling", "Seedling", 
"Seedling", "Seedling", "Seedling"), Accession = c("Col", "Col", 
"Col", "Col", "Col", "Col"), Genotype = c("sub-9", "sub-9", "sub-9", 
"WT", "WT", "WT"), BReplicate = c("1", "2", "3", "1", "2", "3"
), Cq = c(25.2540053029395, 24.6386988176262, 24.5407237397682, 
24.7295032752289, 24.4544553518053, 24.4604738608338)), .Names = c("Tissue", 
"Accession", "Genotype", "BReplicate", "Cq"), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), vars = c("Tissue", 
"Accession", "Genotype"), drop = TRUE)
df.target:
structure(list(Tissue = c("Seedling", "Seedling", "Seedling", 
"Seedling", "Seedling", "Seedling", "Seedling", "Seedling", "Seedling", 
"Seedling", "Seedling", "Seedling", "Seedling", "Seedling", "Seedling", 
"Seedling", "Seedling", "Seedling"), Accession = c("Col", "Col", 
"Col", "Col", "Col", "Col", "Col", "Col", "Col", "Col", "Col", 
"Col", "Col", "Col", "Col", "Col", "Col", "Col"), Genotype = c("sub-9", 
"sub-9", "sub-9", "sub-9", "sub-9", "sub-9", "sub-9", "sub-9", 
"sub-9", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT"
), Gene = c("CESA1", "CESA1", "CESA1", "CESA3", "CESA3", "CESA3", 
"PRC1", "PRC1", "PRC1", "CESA1", "CESA1", "CESA1", "CESA3", "CESA3", 
"CESA3", "PRC1", "PRC1", "PRC1"), BReplicate = c("1", "2", "3", 
"1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", 
"2", "3"), Cq = c(23.496195267366, 22.7054342062343, 22.639685306532, 
22.8211127310626, 22.0820545565921, 22.1110378642623, 24.1462601520338, 
23.3972522049923, 23.3270831096319, 22.5998036632355, 22.7161277680243, 
22.6526346162252, 22.237990186265, 22.0715318793714, 22.0171712171306, 
23.4524362896598, 23.4121887867123, 23.3648625264175)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -18L), .Names = c("Tissue", 
"Accession", "Genotype", "Gene", "BReplicate", "Cq"), vars = c("Tissue", 
"Accession", "Genotype", "Gene", "BReplicate"), drop = TRUE, indices = list(
    0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
    14L, 15L, 16L, 17L), group_sizes = c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
    Tissue = c("Seedling", "Seedling", "Seedling", "Seedling", 
    "Seedling", "Seedling", "Seedling", "Seedling", "Seedling", 
    "Seedling", "Seedling", "Seedling", "Seedling", "Seedling", 
    "Seedling", "Seedling", "Seedling", "Seedling"), Accession = c("Col", 
    "Col", "Col", "Col", "Col", "Col", "Col", "Col", "Col", "Col", 
    "Col", "Col", "Col", "Col", "Col", "Col", "Col", "Col"), 
    Genotype = c("sub-9", "sub-9", "sub-9", "sub-9", "sub-9", 
    "sub-9", "sub-9", "sub-9", "sub-9", "WT", "WT", "WT", "WT", 
    "WT", "WT", "WT", "WT", "WT"), Gene = c("CESA1", "CESA1", 
    "CESA1", "CESA3", "CESA3", "CESA3", "PRC1", "PRC1", "PRC1", 
    "CESA1", "CESA1", "CESA1", "CESA3", "CESA3", "CESA3", "PRC1", 
    "PRC1", "PRC1"), BReplicate = c("1", "2", "3", "1", "2", 
    "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", 
    "3")), class = "data.frame", row.names = c(NA, -18L), vars = c("Tissue", 
"Accession", "Genotype", "Gene", "BReplicate"), drop = TRUE, .Names = c("Tissue", 
"Accession", "Genotype", "Gene", "BReplicate")))
 
     
    