首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么R顺序函数不适用于此数据帧--仅返回第一个(未排序的)值

为什么R顺序函数不适用于此数据帧--仅返回第一个(未排序的)值
EN

Stack Overflow用户
提问于 2015-02-11 00:48:24
回答 2查看 2K关注 0票数 0

我有一个数据帧,其中包含以下列的调查结果:

代码语言:javascript
运行
复制
 1) number of unanswered questions, 
 2) number of times the respondent answered with the most common (consensus) response, 
 3) number of questions answered, and 
 4) the percentage of questions in which the respondent answered with the consensus response.  

我想按最后一列(共识答案百分比)对其进行排序,并选择最高的五分之一。不过,我似乎不能对它进行分类。

下面是str():

代码语言:javascript
运行
复制
> str(consensus_participant_totals)
'data.frame':   210 obs. of  5 variables:
 $ V1           : Factor w/ 210 levels "R_06xJVSOuTuhYLOt",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ num_nas      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ num_cons     : num  61 61 54 54 52 55 57 52 41 60 ...
 $ num_answered : num  68 68 68 68 68 68 68 68 68 68 ...
 $ pct_consensus: num  0.868 0.794 0.735 0.809 0.779 ...

下面是前几行:

代码语言:javascript
运行
复制
consensus_participant_totals
                   V1 num_nas num_cons num_answered pct_consensus
1   R_06xJVSOuTuhYLOt       0       61           68     0.8676471
2   R_09aLjPFNmYMmsbX       0       61           68     0.7941176
3   R_0AphAH5kJRGOFfL       0       54           68     0.7352941
4   R_0cTBiuOmRWuFCZL       0       54           68     0.8088235
5   R_0dBEYzi8C7A65P7       0       52           68     0.7794118
6   R_0dCNkauEqyd2Y97       0       55           68     0.8529412

当我尝试的时候:

代码语言:javascript
运行
复制
consensus_participant_totals[order(pct_consensus),]

我得到了

代码语言:javascript
运行
复制
Error in order(pct_consensus) : object 'pct_consensus' not found

这意味着我必须将它放在引号中(在示例中似乎没有人这样做--我不明白为什么)

当我尝试使用引号时,我只得到第一行:

代码语言:javascript
运行
复制
consensus_participant_totals[order("pct_consensus"),]             
                V1 num_nas num_cons num_answered pct_consensus
1 R_06xJVSOuTuhYLOt       0       61           68     0.8676471

我做错了什么?如何按"pct_consensus“排序?

谢谢你的指点!

EN

回答 2

Stack Overflow用户

发布于 2015-02-11 01:28:09

您的问题是不能在不指定数据框的情况下调用列。使用内置的数据示例(这样任何人都可以运行它,而且因为它的名称中包含的字符要少得多):

每个人都有iris数据集:

代码语言:javascript
运行
复制
head(iris)

第一列是Sepal.Length,它位于数据集中,但不在您的工作空间中,除非它是数据集的一部分

代码语言:javascript
运行
复制
head(iris$Sepal.Length)
head(iris[, "Sepal.Length"])
head(Sepal.Length) # error, no Sepal.Length

因此,当您根据列进行排序时,您必须告诉R列的位置。有很多方法可以做到这一点:

代码语言:javascript
运行
复制
iris[order(iris$Sepal.Length), ]
iris[order(iris[, "Sepal.Length"]), ]
iris[order(iris["Sepal.Length"]), ]
with(iris, iris[order(Sepal.Length), ])

但它不能被忽视

代码语言:javascript
运行
复制
iris[order(Sepal.Length), ]  # errors out

在弄清楚这样的事情时,请记住,您可以运行R代码的小片段。你说

代码语言:javascript
运行
复制
> when I try it with quotes, I just get the first row:

consensus_participant_totals[order("pct_consensus"),]

这是因为您对长度为1的字符向量进行了排序。

代码语言:javascript
运行
复制
order("pct_consensus")

它等同于这些

代码语言:javascript
运行
复制
order("a")
order("whatever string you put here")

它们返回1,因为您会问“如果我按字母顺序对单个字符串进行排序,它应该在什么位置?”在只有一个字符串的情况下,答案总是1,这就是为什么你会得到第一行。

票数 3
EN

Stack Overflow用户

发布于 2015-02-11 01:18:16

好吧,在尝试之后,它与以下内容一起工作:

代码语言:javascript
运行
复制
c2 <-consensus_participant_totals[c(order(consensus_participant_totals[,"pct_consensus"])),]

似乎我可以将连接放在order函数之前或之后(例如c(order(consensus_...或顺序(c(共识...在上面的

这看起来与大多数教程都不太一样。我认为我的错误在于大多数教程都使用了"attach“函数,该函数避免了在命令中输入数据帧名称,而我忽略了这一点。因此,简单地做(对于dataframe x) xorder(y)是行不通的,因为我没有附加x。

我想...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28437183

复制
相关文章

相似问题

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