我有以下数据框:
df <- structure(list(Term = structure(c(6L, 2L, 4L, 3L, 1L, 5L), .Label = c("Cytokine-cytokine receptor interaction",
"cellular response to cytokine stimulus", "negative regulation of multi-organism process",
"positive regulation of biological process", "positive regulation of multicellular organismal process",
"regulation of viral process"), class = "factor"), c = c(10.698,
24.445, 0, 12.058, 9.542, 0), d = c(0, 7.093, 0, 0, 0, 0)), .Names = c("Term",
"c", "d"), class = "data.frame", row.names = c(154L, 147L, 105L,
157L, 86L, 104L))
看起来像这样:
> df
Term c d
154 regulation of viral process 10.698 0.000
147 cellular response to cytokine stimulus 24.445 7.093
105 positive regulation of biological process 0.000 0.000
157 negative regulation of multi-organism process 12.058 0.000
86 Cytokine-cytokine receptor interaction 9.542 0.000
104 positive regulation of multicellular organismal process 0.000 0.000
我想要做的是给出一个vector列表:
target <- c("Cytokine-cytokine receptor interaction","negative regulation of multi-organism process ")
我想根据df$Term
选择target
并对其进行排序。
导致此数据框
Term c d
Cytokine-cytokine receptor interaction 9.542 0.000
negative regulation of multi-organism process 12.058 0.000
但为什么这个命令失败了?
> df[match(target,df$Term),]
Term c d
86 Cytokine-cytokine receptor interaction 9.542 0
NA <NA> NA NA
做正确的方法是什么?
答案 0 :(得分:1)
你错误地在
的末尾放了一个空格target <- c("Cytokine-cytokine receptor interaction","negative regulation of multi-organism process ") # <--- this space after process
因此矢量不匹配:)
确实,以下代码确实有效:
df <- structure(list(Term = structure(c(6L, 2L, 4L, 3L, 1L, 5L),
.Label = c("Cytokine-cytokine receptor interaction",
"cellular response to cytokine stimulus",
"negative regulation of multi-organism process",
"positive regulation of biological process", "positive regulation of multicellular organismal process", "regulation of viral process"), class = "factor"), c = c(10.698,
24.445, 0, 12.058, 9.542, 0), d = c(0, 7.093, 0, 0, 0, 0)), .Names = c("Term",
"c", "d"), class = "data.frame", row.names = c(154L, 147L, 105L,
157L, 86L, 104L))
target <- c("Cytokine-cytokine receptor interaction","negative regulation of multi-organism process")
df[df$Term %in% target,]