简体   繁体   中英

SQLDF R: Counting unique values in a data frame

I have a data frame with one column. There are 10 rows.

(4.0 * 3.0)
(4.0 * 3.0)
(2.0 * (1.0 * (1.0 * 6.0)))
(4.0 * (3.0 * 1.0))
(6.0 * 2.0)
(6.0 * 2.0)
(2.0 * 6.0)
(2.0 * 6.0)
(2.0 * 6.0)
(6.0 * 2.0)

I need to extract the unique values in the column and the number of times it occurs. Using sqldf package I was able to get the unique values. But not the count.

Query:

sqldf("SELECT V1, COUNT(DISTINCT V1) as DinctC from dataset GROUP BY V1")

Output:

                           V1 DinctC
1 (2.0 * (1.0 * (1.0 * 6.0)))      1
2                 (2.0 * 6.0)      1
3         (4.0 * (3.0 * 1.0))      1
4                 (4.0 * 3.0)      1
5                 (6.0 * 2.0)      1

What I want is:

                           V1 DinctC
1 (2.0 * (1.0 * (1.0 * 6.0)))      1
2                 (2.0 * 6.0)      3
3         (4.0 * (3.0 * 1.0))      1
4                 (4.0 * 3.0)      2
5                 (6.0 * 2.0)      3

Edit : As Tim Biegeleisen pointed out "Distinct" is not a function therefore no need of the brackets. So updating DISTINCT(V1) to DISTINCT V1

We do not need the distinct keyword as we are using the GROUP BY clause.

sqldf("SELECT V1, COUNT(V1) as DinctC from dataset GROUP BY V1")

Result:

                           V1 DinctC
1 (2.0 * (1.0 * (1.0 * 6.0)))      1
2                 (2.0 * 6.0)      3
3         (4.0 * (3.0 * 1.0))      1
4                 (4.0 * 3.0)      2
5                 (6.0 * 2.0)      3

We can use count

library(dplyr)
count(df, V1)
# A tibble: 5 x 2
#                          V1     n
#                       <chr> <int>
#1 (2.0 * (1.0 * (1.0 * 6.0)))     1
#2                 (2.0 * 6.0)     3
#3         (4.0 * (3.0 * 1.0))     1
#4                 (4.0 * 3.0)     2
#5                 (6.0 * 2.0)     3

Or table from base R

table(df$V1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM