Reputation: 6409
I have a vector
a<- c(0.2849579, 0.2849579, 0.2849579)
I would like to use:
tabulate(a,nbins=max(a))
but this returns integer(0)
as output.
Can tabulate be used for numbers with decimals?
Upvotes: 1
Views: 519
Reputation: 46876
Here are some random numbers, replicated
x = sample(runif(10), 1000, TRUE)
Find the unique (optionally, rounding to significant figures) values, then find the index of each x
in the table of unique values and tabulate those
## x = signif(x, 6)
ux = sort(unique(x))
idx = match(x, ux)
n = tabulate(idx, nbins=length(ux))
finally, summarize the results
df = data.frame(x=ux, n=n)
Use the summary to see all counts
> head(df)
x n
1 0.02832152 108
2 0.04973473 90
3 0.19770913 96
4 0.31591234 103
5 0.59334322 97
6 0.64145901 98
or identify values with maximum counts
> df[df$n == max(df$n), , drop=FALSE]
x n
10 0.9711141 127
Upvotes: 1
Reputation:
The value of nbins
determines the number of bins in which the numbers are put. Since max(a) = 0.2849579
there will be zero bins and therefore your result is to be expected.
Also the help page for tabulate
(called with ?tabulate
) says:
If the elements of ‘bin’ are numeric but not integers, they are truncated to the nearest integer.
Depending on your problem you may want to upscale your decimals first and then apply tabulate
. Or you might want to use hist
or something like that instead.
Upvotes: 0