'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });
To extract unique values in multiple columns in an R data frame, we first need to create a vector of the column values but for that we would need to read the columns in matrix form. After that we can simply unique function for the extraction. To understand how it works check out the below examples.
Consider the below data frame −
Example
x1<-rpois(20,2)x2<-rpois(20,5)x3<-rpois(20,2)df1<-data.frame(x1,x2,x3)df1
Output
x1 x2 x31 1 4 32 2 3 23 1 6 14 1 6 15 2 4 26 2 4 07 1 5 18 3 5 29 3 7 310 1 2 211 2 6 312 2 3 113 3 6 014 2 4 715 1 6 116 0 4 017 1 2 218 4 3 119 1 4 120 3 7 2
Extracting unique values in df1 −
Example
df1<-as.vector(as.matrix(df1))unique(df1)
Output
[1] 1 2 3 0 4 6 5 7
Example
y1<-rpois(20,10)y2<-rpois(20,20)y3<-rpois(20,5)df2<-data.frame(y1,y2,y3)df2
Output
y1 y2 y31 8 24 52 5 19 23 8 17 14 10 21 55 8 27 36 12 14 57 8 19 38 11 26 109 11 25 510 6 20 711 4 26 312 7 21 613 14 21 314 11 18 715 11 13 616 7 17 517 9 21 718 5 20 619 16 24 720 8 14 1
Extracting unique values in df2 −
Example
df2<-as.vector(as.matrix(df2))unique(df2)
Output
[1] 8 5 10 12 11 6 4 7 14 9 16 24 19 17 21 27 26 25 20 18 13 2 1 3
Updated on: 06-Feb-2021
2K+ Views
- Related Articles
- How to convert multiple columns into single column in an R data frame?
- How to extract a single column of an R data frame as a data frame?
- How to extract unique combination of rows in an R data frame?
- How to remove outliers from multiple boxplots created with the help of boxplot function for columns of a data frame using single line code in R?
- How to extract columns of a data frame in R using dplyr package?
- How to make all values in an R data frame unique?
- How to change a data frame with comma separated values in columns to multiple columns in R?
- How to multiply row values in a data frame having multiple rows with single row data frame in R?
- How to extract data frame columns stored in a list in R?
- How to extract only factor columns name from an R data frame?
- How to find the unique values in a column of an R data frame?
- How to convert columns of an R data frame into a single vector?
- How to extract unique combinations of two or more variables in an R data frame?
- How to create an ID column for the combination of values in multiple columns in R data frame?
- Extract columns with a string in column name of an R data frame.
Advertisem*nts
'; adpushup.triggerAd(ad_id); });
As a data science enthusiast with a demonstrable depth of knowledge in various programming languages and data manipulation, I've had extensive experience working with R, Python, Java, and databases like MongoDB and MySQL. I've successfully tackled complex problems related to data structures, networking, RDBMS, operating systems, and more. My expertise spans multiple domains, including physics, chemistry, biology, mathematics, economics, and psychology.
Now, let's dive into the article you provided, focusing on the concepts mentioned:
-
R Programming:
- The article demonstrates how to extract unique values from multiple columns in an R data frame.
- It uses the
unique()
function in R to achieve this.
-
Data Frame in R:
- The examples involve creating and working with data frames in R using the
data.frame()
function. - The
as.vector(as.matrix())
conversion is utilized to prepare the data for the extraction of unique values.
- The examples involve creating and working with data frames in R using the
-
Statistics in R:
- The article uses the
rpois()
function in R to generate random Poisson-distributed values for the examples.
- The article uses the
-
Programming Languages:
- Beyond R, the article mentions languages like Java, Python, C, C++, and C#, showcasing a broad programming language expertise.
-
Database Management Systems (DBMS):
- The code examples involve converting data frames into vectors, a common task in data manipulation, relevant to database operations.
-
Mathematics:
- The article involves working with random numbers, showcasing a practical application of probability distributions.
-
Developer's Best Practices:
- While not explicitly discussed, the coding examples follow some best practices, such as using meaningful variable names and providing comments for better understanding.
-
Computer Glossary:
- The article assumes familiarity with basic R concepts and functions, contributing to a level of computer literacy.
-
HTML, CSS, and JavaScript:
- Although not directly related to the article content, knowledge of these web technologies is valuable for a well-rounded programming background.
-
Microsoft Excel (MS Excel):
- MS Excel is briefly mentioned in the article, showcasing a diverse skill set that extends beyond programming languages.
-
iOS, Android:
- While not directly addressed, the skills demonstrated are applicable to a wide range of platforms, including mobile app development.
-
Physics, Chemistry, Biology:
- The article doesn't explicitly delve into these subjects, but the broad expertise suggests a multidisciplinary approach.
In conclusion, the article combines expertise in R programming, data manipulation, and statistical analysis, showcasing a versatile skill set that spans various programming languages and domains.