我有以下数据框:
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我想要做的是给出一个向量列表:
target <- c("Cytokine-cytokine receptor interaction","negative regulation of multi-organism process ")我想根据target对df$Term进行选择和排序。产生此数据帧
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什么才是正确的方法呢?
发布于 2015-04-27 13:58:40
您错误地将空格放在
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,]https://stackoverflow.com/questions/29888123
复制相似问题