Question

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

> 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 
Was it helpful?

Solution

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.

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.

But then again, if you only need the sorted indices, you should call order instead of sort...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top