我有一个数据框,其中包含以下列的调查结果:
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():
> 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 ...
以下是前几行:
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
当我尝试:
consensus_participant_totals[order(pct_consensus),]
我得到了
Error in order(pct_consensus) : object 'pct_consensus' not found
这表明我必须把它放在引号中(在例子中没有人似乎这样做 - 我不明白为什么)
当我用引号尝试时,我只得到第一行:
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”排序
感谢您的任何指示!
答案 0 :(得分:3)
您的问题是您无法在未指定数据框的情况下调用列。使用内置数据示例(所有人都可以运行它,因为名称中的字符数少得多):
每个人都有iris
数据集:
head(iris)
第一列是Sepal.Length
,它位于数据集中但不在您的工作区中,除非作为数据集的一部分
head(iris$Sepal.Length)
head(iris[, "Sepal.Length"])
head(Sepal.Length) # error, no Sepal.Length
因此,当您根据列进行排序时,必须告诉R列的位置。有很多方法可以做到这一点:
iris[order(iris$Sepal.Length), ]
iris[order(iris[, "Sepal.Length"]), ]
iris[order(iris["Sepal.Length"]), ]
with(iris, iris[order(Sepal.Length), ])
但它不能被忽视
iris[order(Sepal.Length), ] # errors out
在弄清楚这样的事情时,请记住你可以运行R代码的小片段。你说
> when I try it with quotes, I just get the first row:
consensus_participant_totals[order("pct_consensus"),]
这是因为您订购了长度为1的字符向量。如果您运行
order("pct_consensus")
它等同于这些
order("a")
order("whatever string you put here")
他们返回1,因为你要求"如果我按字母顺序排序一个字符串,它应该在什么位置?"只有一个字符串,答案总是1.这就是你获得第一行的原因。
答案 1 :(得分:0)
好吧,在玩完之后,它可以使用以下内容:
c2 <-consensus_participant_totals[c(order(consensus_participant_totals[,"pct_consensus"])),]
似乎我可以在订单功能之前或之后放置concat(例如c(订单(共识_...或订单)(c(上面的共识......)
这看起来与大多数教程不同。我认为我的错误在于大多数教程使用“附加”功能,这避免了必须将数据框名称放在命令中,我忽略了这一点。因此,简单地(对于数据帧x)x [order(y),]将不起作用,因为我没有附加x。
我想......