@russhh: in your solution I have to type manually but I want to make "group1 - group2" automatically. because instead of "group1 - group2" I can have: "groupA- groupB".
I am using edgeR package in R and here the code I used
library(edgeR)
target <-read.delim("metadata.txt", header=T)
x <- read.csv('data4.txt', row.names = 1)
group <- factor(target$group)
design <- model.matrix(~0+group)
when I print the design variable I would get the following results:
> design
group1 group2
1 1 0
2 1 0
3 0 1
4 1 0
5 0 1
6 1 0
7 1 0
8 1 0
9 0 1
10 0 1
11 1 0
12 0 1
attr(,"assign")
[1] 1 1
attr(,"contrasts")
attr(,"contrasts")$group
[1] "contr.treatment"
I would like to make a new variable called comparison like this:
comparison = "group1-group2"
I actually do not know how to do that because group1 and group2 are not headers or separated sections. do you guys know how to that?
2 answers
Get the dimnames:
#example data
group <- factor(1:2)
design <- model.matrix(~0+group)
design
# group1 group2
# 1 1 0
# 2 0 1
# attr(,"assign")
# [1] 1 1
# attr(,"contrasts")
# attr(,"contrasts")$group
# [1] "contr.treatment"
paste(dimnames(design)[[2]], collapse = "-")
# [1] "group1-group2"
Hi are you trying to subtract the second column of the design from the first, you could do that with matrix manipulation: as.matrix(design) %*% c(1, -1). However, I suspect you're trying to define an experimental comparison (contrast) for use in the edgeR workflow. As such you're probably looking for limma::makeContrasts https://www.rdocumentation.org/packages/limma/versions/3.28.14/topics/makeContrasts ;
contrasts <- limma::makeContrasts(comparison = "group1 - group2", levels = design)
Sorry, that wasn't clear
Log in to answer this question.