How To Filter Values In Matrix R
Extract Unique Values in R (3 Examples)
In this article you'll learn how to select just unique values from a vector or data frame column in the R programming language.
The tutorial consists of this:
Allow'south dive right into the examples…
Creation of Exemplifying Information
We use the following data as basement for this R programming tutorial:
ten <- rep(messages[ 1 : 3 ], each = three ) # Create case vector x # Print example vector # [1] "a" "a" "a" "b" "b" "b" "c" "c" "c"
x <- rep(messages[1:three], each = 3) # Create case vector x # Print example vector # [ane] "a" "a" "a" "b" "b" "b" "c" "c" "c"
The previous output of the RStudio console shows that our instance vector consists of nine character elements. Each vector chemical element exists iii times.
Example 1: Use unique() Function to Select Unique Values
This case shows how to utilise the unique function to select only unique values from our example vector.
Consider the following R code:
unique(x) # Using unique function # [ane] "a" "b" "c"
unique(x) # Using unique function # [ane] "a" "b" "c"
After executing the previous R syntax, the RStudio panel returns a new vector showing the three unique values of our vector (i.eastward. a, b, and c).
Example 2: Utilise duplicated() Function to Select Unique Values
The post-obit R syntax illustrates how to use the duplicated function to become all unique values from our vector:
x[ !duplicated(ten) ] # Using duplicated role # [i] "a" "b" "c"
x[!duplicated(x)] # Using duplicated function # [1] "a" "b" "c"
The effect is the aforementioned as in Example 1.
Example 3: Apply distinct() Function of dplyr Package to Select Unique Values
It is also possible to use functions of the dplyr parcel to extract unique values.
Nosotros first need to install and load the dplyr bundle:
install. packages ( "dplyr" ) # Install dplyr parcel library( "dplyr" ) # Load dplyr
install.packages("dplyr") # Install dplyr bundle library("dplyr") # Load dplyr
Now, we can use the singled-out part of the dplyr package in combination with the data.frame function to return unique values from our data:
distinct(information. frame (x) ) # Using singled-out function
singled-out(data.frame(x)) # Using distinct part
Equally shown in Table one, we accept created a data frame showing only unique values of our input vector ten by running the previous R programming code.
Note that we had to convert our vector object to the data.frame class first, since the singled-out function is non taking vector objects as input.
If you are looking for unique values in a data frame variable anyway, you don't need to apply the data.frame office as we did in this example.
Video, Further Resources & Summary
Would you like to know more than almost unique values? Then I recommend watching the following video of my YouTube channel. In the video, I'one thousand illustrating the R lawmaking of this commodity in the R programming language:
In addition, you might want to read the other R programming articles of Statistics Globe. I take published numerous tutorials about related topics such as matrices, counting, and groups:
- Count Unique Values in R
- Extract Values from Matrix by Column & Row Names
- Count Unique Values by Grouping in R
- The R Programming Language
In this R article you lot take learned how to get unique values of an array or a data frame variable. If you have additional questions, please let me know in the comments department below.
How To Filter Values In Matrix R,
Source: https://statisticsglobe.com/extract-unique-values-in-r
Posted by: smithwiton1980.blogspot.com
0 Response to "How To Filter Values In Matrix R"
Post a Comment