在R中的向量中列出不同的值。

时间:2022-10-01 07:58:38

How can I list the distinct values in a vector where the values are replicative? I mean, similarly to the following SQL statement:

我如何在一个向量中列出这些值是复制的?我的意思是,类似于下面的SQL语句:

SELECT DISTINCT product_code
FROM data

3 个解决方案

#1


114  

Do you mean unique:

你的意思是独特的:

R> x = c(1,1,2,3,4,4,4)
R> x
[1] 1 1 2 3 4 4 4
R> unique(x)
[1] 1 2 3 4

#2


5  

You can also use the sqldf package in R. Z <-sqldf('SELECT DISTINCT tablename.columnname FROM tablename ')

您还可以在R. Z <-sqldf('SELECT DISTINCT tablename)中使用sqldf包。columnname从表”)

#3


4  

Try using the duplicated function in combination with the negation operator "!".

尝试使用重复的函数与否定运算符“!”

Example:

例子:

wdups <- rep(1:5,5)
wodups <- wdups[which(!duplicated(wdups))]

Hope that helps.

希望有帮助。

#1


114  

Do you mean unique:

你的意思是独特的:

R> x = c(1,1,2,3,4,4,4)
R> x
[1] 1 1 2 3 4 4 4
R> unique(x)
[1] 1 2 3 4

#2


5  

You can also use the sqldf package in R. Z <-sqldf('SELECT DISTINCT tablename.columnname FROM tablename ')

您还可以在R. Z <-sqldf('SELECT DISTINCT tablename)中使用sqldf包。columnname从表”)

#3


4  

Try using the duplicated function in combination with the negation operator "!".

尝试使用重复的函数与否定运算符“!”

Example:

例子:

wdups <- rep(1:5,5)
wodups <- wdups[which(!duplicated(wdups))]

Hope that helps.

希望有帮助。