I am trying to split my replicate from my results in my dataset in an efficient way. I used this data as an example:
x <- data.frame(sample = c("AA", "AA", "BB", "BB", "CC", "CC"),
Gene = c("HSA-let1","HSA-let1","HSA-let1","HSA-let1","HSA-let1","HSA-let1"),
Cq = c(14.55, 14.45, 13.55, 13.45, 16.55, 16.45))
The problem is that the two duplicates have the same name in "Sample" and "Gene". so when I tried:
spread(x,Gene,Cq)
I get duplicate identifiers error.I have tried this fix code below and it gives two values in one coloumn separated by ",". This was almost successful, but I want them separated:
x_test <- dcast(setDT(x), Gene ~ sample, value.var = 'Cq',
fun.aggregate = function(x) toString(unique(x)))
I did also tried this this tidyr solution, but I dont understand enough R to make it work.
x_test2 <- x %>%
gather(variable, value, -(Gene:Cq)) %>%
unite(temp, Cq, variable) %>%
spread(temp, value)
I want my dataset to look like this:
# Gene AA_1 AA_2 BB_1 BB_2 CC_1 CC_2
# HSA-let 14.55 14.45 13.55 13.45 16.55 16.45