如何在R数据帧的列中找到唯一值?
在本文中,我们将讨论如何在 R 编程语言中找出数据帧列中的唯一值。对于此任务,unique()函数用于传递要打印的唯一值的列名。
Syntax: unique(x)
parameters:
- x: data frame
对于列名称,使用 $ 选择列
dataframe$column_name
示例 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)
输出:
[1] INDIA AMERICA JAPAN CHINA BANGLADESH SRILANKA NEPAL
Levels: AMERICA BANGLADESH CHINA INDIA JAPAN NEPAL SRILANKA
示例 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 中的唯一数据
x2
1 2
2 4
4 7
5 1
列 x1 中的唯一数据
x1
1 9
2 5
3 6
4 8
示例 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)
输出: