首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于向量选择数据框行并对其进行排序

基于向量选择数据框行并对其进行排序
EN

Stack Overflow用户
提问于 2015-04-27 13:50:51
回答 1查看 125关注 0票数 0

我有以下数据框:

代码语言:javascript
复制
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))

它看起来像这样:

代码语言:javascript
复制
> 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

我想要做的是给出一个向量列表:

代码语言:javascript
复制
 target <- c("Cytokine-cytokine receptor interaction","negative regulation of multi-organism process ")

我想根据targetdf$Term进行选择和排序。产生此数据帧

代码语言:javascript
复制
                                                Term      c     d
              Cytokine-cytokine receptor interaction  9.542 0.000
       negative regulation of multi-organism process 12.058 0.000

但是为什么这个命令失败了呢?

代码语言:javascript
复制
> df[match(target,df$Term),]
                                     Term     c  d
86 Cytokine-cytokine receptor interaction 9.542  0
NA                                   <NA>    NA NA

什么才是正确的方法呢?

EN

回答 1

Stack Overflow用户

发布于 2015-04-27 13:58:40

您错误地将空格放在

代码语言:javascript
复制
target <- c("Cytokine-cytokine receptor interaction","negative regulation of multi-organism process ") # <--- this space after process

因此向量不匹配:)

实际上,下面的代码确实可以工作:

代码语言:javascript
复制
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,]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29888123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档