Based on your example, it appears that Gene is not a column of your data.frame, but rather the rownames. You can deal with this by either assigning a new column as the rownames, or filtering on the rownames.
-
cts$gene <- rownames(cts); cts[cts$gene %in% risk_genes, ]
-
or this: cts[rownames(cts) %in% risk_genes, ]
RepEx
cts <- data.frame(sample1 = round(rnorm(100,10,2),0),
sample2 = round(rnorm(100,15,3),0),
sample3 = round(rnorm(100,8,3),0))
> `head(cts)`
> sample1 sample2 sample3
> 1 11 10 10
> 2 9 19 8
> 3 6 9 10
> 4 10 11 10
> 5 9 11 8
> 6 7 15 8
> rownames(cts) <- paste0("gene", 1:100)
> head(cts)
> sample1 sample2 sample3
> gene1 11 10 10
> gene2 9 19 8
> gene3 6 9 10
> gene4 10 11 10
> gene5 9 11 8
> gene6 7 15 8
>
> genelist <- c("gene1","gene50","gene22")
>
> cts[rownames(cts) %in% genelist,]
> sample1 sample2 sample3
> gene1 11 10 10
> gene22 12 16 8
> gene50 10 13 10