R - number of unique values in a column of data frame

Try with [[ instead of [. [ returns a list (a data.frame in fact), [[ returns a vector.

df <- data.frame( some_col = c(1,2,3,4),
                  another_col = c(4,5,6,7) )

length(unique(df[["some_col"]]))
#[1] 4

class( df[["some_col"]] )
[1] "numeric"

class( df["some_col"] )
[1] "data.frame"

You're getting a value of 1 because the list is of length 1 (1 column), even though that 1 element contains several values.


you need to use

length(unique(unlist(df[c("some_col")])))

When you call column by df[c("some_col")] or by df["some_col"] ; it pulls it as a list. Unlist will convert it into the vector and you can work easily with it. When you call column by df$some_col .. it pulls the data column as vector

Tags:

R