Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
822 views
in Technique[技术] by (71.8m points)

r - Error: x must be atomic for 'sort.list'

This is weird. I get this error

Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

when I execute this code on a list

 cc3 <- as.data.frame(table(cc2))

What could possibly be wrong?

this is a head of the list.

head(cc2)   
V1
1:    174
2:    174
3:    211
4: 177106
5: 177106
6: 177106 

Edit: When I run, str(cc2) I get this

Classes ‘data.table’ and 'data.frame':  149706 obs. of  1 variable:
$ V1:List of 149706
..$ : Named chr "174"
.. ..- attr(*, "names")= chr "V11"
..$ : Named chr "174"
 .. ..- attr(*, "names")= chr "V7"
 ..$ : Named chr "211"
  .. ..- attr(*, "names")= chr "V6"
 .. [list output truncated]
 - attr(*, ".internal.selfref")=<externalptr> 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the output of str(cc2), the variable inside of the data.table, V1, is itself a list. This means that cc2 is a nested list of length 1. The error is occurring because table calls sort.list, which requires an atomic vector as input.

Try using unlist:

cc3 <- as.data.frame(table(unlist(cc2)))

unlist will (recursively) extract elements from their list containers. So unlist(cc2) will return a vector, which works directly with table.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...