Table of Contents
To find the unique values in a column using the R programming language, one can use the “unique()” function. This function takes in a column as an argument and returns a vector of all the unique values present in that column. This allows for easy identification and manipulation of data without any repeated values. The “unique()” function is a useful tool for data analysis and management in R.
R: Find Unique Values in a Column
You can use the unique() function in R to find unique values in a column of a data frame.
This tutorial provides several examples of how to use this function with the following data frame:
#create data frame
df <- data.frame(team=c('A', 'A', 'B', 'B', 'C', 'C'),
points=c(90, 99, 90, 85, 90, 85),
assists=c(33, 33, 31, 39, 34, 34),
rebounds=c(30, 28, 24, 24, 28, 28))
#view data frame
df
team points assists rebounds
1 A 90 33 30
2 A 99 33 28
3 B 90 31 24
4 B 85 39 24
5 C 90 34 28
6 C 85 34 28
Example 1: Find Unique Values in Column
The following code shows how to find unique values in the ‘team’ column:
#find unique values in 'team' column
unique(df$team)
[1] "A" "B" "C"
We can use similar syntax to find unique values in the ‘points’ column:
#find unique values in 'points' column
unique(df$points)
[1] 90 99 85Example 2: Find & Sort Unique Values in Column
The following code shows how to find and sort unique values in the ‘points’ column:
#find and sort unique values in 'points' column
sort(unique(df$points))
[1] 85 90 99
We can also sort unique values in a descending order:
#find and sort unique values in 'points' column
sort(unique(df$points), decreasing=TRUE)
[1] 99 90 85Example 3: Find & Count Unique Values in Column
The following code shows how to find and count the number of each unique value in the ‘points’ column:
#find and count unique values in 'points' column
table(df$points)
85 90 99
2 3 1 From the output we can see:
- The value 85 occurs 2 times.
- The value 90 occurs 3 times.
- The value 99 occurs 1 time.
Cite this article
stats writer (2024). How can I find the unique values in a column using the R programming language?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-find-the-unique-values-in-a-column-using-the-r-programming-language/
stats writer. "How can I find the unique values in a column using the R programming language?." PSYCHOLOGICAL SCALES, 15 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-find-the-unique-values-in-a-column-using-the-r-programming-language/.
stats writer. "How can I find the unique values in a column using the R programming language?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-find-the-unique-values-in-a-column-using-the-r-programming-language/.
stats writer (2024) 'How can I find the unique values in a column using the R programming language?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-find-the-unique-values-in-a-column-using-the-r-programming-language/.
[1] stats writer, "How can I find the unique values in a column using the R programming language?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I find the unique values in a column using the R programming language?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
