📌  相关文章
📜  如何在R数据帧的列中找到唯一值?

📅  最后修改于: 2022-05-13 01:55:25.113000             🧑  作者: Mango

如何在R数据帧的列中找到唯一值?

在本文中,我们将讨论如何在 R 编程语言中找出数据帧列中的唯一值。对于此任务,unique()函数用于传递要打印的唯一值的列名。

对于列名称,使用 $ 选择列

示例 1:

R
id<- c(1,2,3,4,5,6,7,8,9)
  
country <- c("INDIA","AMERICA","JAPAN","CHINA","BANGLADESH",
             "SRILANKA","NEPAL","AMERICA","CHINA")
  
data<-data.frame(id,country)
  
unique(data$country)


R
data <- data.frame(x1 = c(9, 5, 6, 8, 9),      
                        x2 = c(2, 4, 2, 7, 1), 
                        x3 = c(3,6,7,0,3), 
                        x4 = c("Hello", "value", "value", "geeksforgeeks", NA)
                  ) 
                                                                             
 unique(data[c("x2")])   
 unique(data[c("x1")])


R
data<- data.frame(Student=c('John','Lee','Guy',
                            'John','Lee','Guy'),
                    
                  Age=c(18,19,20,18,19,20),
                    
                  Gender=c('Male','Female','Male',
                           'Male','Female','Male'))
   
unique(data)


输出:

示例 2:

电阻

data <- data.frame(x1 = c(9, 5, 6, 8, 9),      
                        x2 = c(2, 4, 2, 7, 1), 
                        x3 = c(3,6,7,0,3), 
                        x4 = c("Hello", "value", "value", "geeksforgeeks", NA)
                  ) 
                                                                             
 unique(data[c("x2")])   
 unique(data[c("x1")])

输出:



列 x2 中的唯一数据

列 x1 中的唯一数据

示例 3:

电阻

data<- data.frame(Student=c('John','Lee','Guy',
                            'John','Lee','Guy'),
                    
                  Age=c(18,19,20,18,19,20),
                    
                  Gender=c('Male','Female','Male',
                           'Male','Female','Male'))
   
unique(data)

输出: