I have a vector of numbers. Let's call it mydata
:
str(mydata)
# num [1:236] 2 1 1 2 2 1 2 1 2 2 ...
I can then count each value using table
:
table(mydata)
# mydata
# 1 2 9 10
# 20 200 14 2
Now, I want to select the value with the highest count (in this case, "2").
I can find the highest count (e.g. 200 in this case) by using the
max function: max(table(mydata))
. But how to get the name associated with the max count in the table, i.e. "2"?
I'd probably do this
That will return "2" as a string. You can do as.numeric() if you want to get it back to a number. This one-liner is a bit more ugly and probably less efficient, but hey, it's one line.
or maybe
which will actually return a factor with a value of "2". If you want to make that numeric, you need to do
as.numeric(as.character(x))
. I was just trying to find ways to avoid having a table variable lying around if i really didn't need it. I wish there were an easier way to convert a table to a named vector.A
table
is very much like a list or a data frame, in that it has values and names (attributes) that are accessible through vector subsetting.The following are equivalent
Other useful things to know about an object is described in its
attributes