0

If I define a list like this:

a <- list(1,2,3,4,5,1)

and then call table(a) , I get:

, , a.3 = 3, a.4 = 4, a.5 = 5, a.6 = 1

   a.2
a.1 2
  1 1

Isn't this behavior weird? How could I use the table function on this list to get something like:

1 2 3 4 5 
2 1 1 1 1 
4
  • 2
    table(unlist(a))? Is there a reason it needs to remain a list? Commented Oct 20, 2018 at 4:46
  • @r2evans. I'm new to R and I could not find the answer by googling it, so I posted this question here! Thanks for your simple solution. Is my question a duplicate on this site? Commented Oct 20, 2018 at 4:50
  • 1
    I haven't seen it before, but that doesn't mean anything. It is likely a fairly basic question, but perhaps not so basic when you are learning it from scratch. Commented Oct 20, 2018 at 5:00
  • @r2evans Would you please add your comment as an answer so I can accept it? Commented Oct 24, 2018 at 14:11

1 Answer 1

1

The table function provides "a contingency table of the counts at each combination of factor levels" (from ?table). Since it works on vectors and not a list, you just need to unlist it:

unlist(a)
# [1] 1 2 3 4 5 1
table(unlist(a))
# 1 2 3 4 5 
# 2 1 1 1 1 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.