list R:无效的下标类型“列表”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8275876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
R: invalid subscript type 'list'
提问by Peter Becich
I'm trying to use the indices of a sorted column of a dataset. I want to reorder the entire dataset by one sorted column.
我正在尝试使用数据集的排序列的索引。我想通过一个排序的列对整个数据集重新排序。
area.sort<-sort(xsample$area1, index.return=TRUE)[2]
The output is a list, so I can't use it index through the whole dataset.
输出是一个列表,所以我不能使用它来索引整个数据集。
Error in xj[i] : invalid subscript type 'list'
Someone suggested using unlist but I can't get rid of the ix*
.
Any ideas? Thanks
有人建议使用 unlist 但我无法摆脱ix*
. 有任何想法吗?谢谢
> area.sort<-unlist(area.sort)
ix1 ix2 ix3 ix4 ix5 ix6 ix7 ix8 ix9 ix10 ix11 ix12 ix13
45 96 92 80 53 54 24 21 63 81 40 66 64
采纳答案by Tommy
The call to sort with index.return=TRUE
returns a list with two components: x and ix. Indexing with [2] returns a subset of the list - still a list.
使用索引进行排序的调用。return=TRUE
返回一个包含两个组件的列表:x 和 ix。用 [2] 索引返回列表的一个子集 - 仍然是一个列表。
If you index using [[2]] it should work better. That returns the element in the list. But indexing using $ix is perhaps a bit clearer.
如果您使用 [[2]] 进行索引,它应该工作得更好。这将返回列表中的元素。但是使用 $ix 进行索引可能会更清晰一些。
But then again, if you only need the sorted indices, you should call order
instead of sort
...
但话又说回来,如果你只需要排序的索引,你应该调用order
而不是sort
...