Этот ответ использует логический вектор для поднабора строк данных.Подробнее об этом смотрите: http://adv -r.had.co.nz / Subsetting.html # data-types .
# Mockup data
x <- data.frame(
`Gene Name` = c("HPRT1", "ABC", "Malat1", "Cd74"),
Cluster_1 = 1:4,
Cluster_2 = 5:8,
check.names = FALSE
)
# Defining gene names of interest to look for
target_genes <- c("Malat1", "Cd74")
# Getting a logical vector that implicitly codes for row positions
# Note: we need to wrap Gene Name in backticks (``) because of the space character in "Gene Name"
row_matches <- x$`Gene Name` %in% target_genes
# Subsetting the gene expression matrix (actually a dataframe object)
# mydata2: dataframe whose rows are for target genes only
# Note: the empty placeholder after the comma in the subsetting below indicates all columns
mydata2 <- x[row_matches, ]
mydata2
#> Gene Name Cluster_1 Cluster_2
#> 3 Malat1 3 7
#> 4 Cd74 4 8
В качестве альтернативы мы также можем использоватьФункция subset
для более краткого кода:
# Mockup data
x <- data.frame(
`Gene Name` = c("HPRT1", "ABC", "Malat1", "Cd74"),
Cluster_1 = 1:4,
Cluster_2 = 5:8,
check.names = FALSE
)
# Defining gene names of interest to look for
target_genes <- c("Malat1", "Cd74")
# As an alternative use the function subset
mydata2 <- subset(x, `Gene Name` %in% target_genes)
mydata2
#> Gene Name Cluster_1 Cluster_2
#> 3 Malat1 3 7
#> 4 Cd74 4 8