Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: invalid subscript type 'list'

Tags:

list

r

subscript

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 
like image 210
Peter Becich Avatar asked Nov 26 '11 02:11

Peter Becich


1 Answers

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...

like image 126
Tommy Avatar answered Sep 18 '22 11:09

Tommy