Retrieving unique values ​​from a data frame using R

I have a data frame with several columns, and I want to be able to isolate two columns and get the total number of unique values ​​... here is an example of what I mean:

Let's say I have a df data frame:

df<- data.frame(v1 = c(1, 2, 3, 2, "a"), v2 = c("a", 2 ,"b","b", 4))
df

  v1 v2
1  1  a
2  2  2
3  3  b
4  2  b
5  a  4

Now what I'm trying to do is extract only unique values ​​over two columns. So if I just used unique () for each column, it would look like this:

> unique(df[,1])
[1] 1 2 3 a
> unique(df[,2])
[1] a 2 b 4

, , ! , "a" , , . , ; , V1 V2 :

  V1_V2
1      1
2      2
3      3
4      2
5      a
6      a
7      2
8      b
9      b
10     4

V1_V2 :

   V1_V2
1      1
2      2
3      3
5      a
8      b
10     4

, nrow(). , ?

+4
2

union:

data.frame(V1_V2=union(df$v1, df$v2))

#  V1_V2
#1     1
#2     2
#3     3
#4     a
#5     b
#6     4
+6

:

unique(c(df[,1], df[,2]))
0

Source: https://habr.com/ru/post/1657518/


All Articles