- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Combine values of two columns separated with hyphen in an R data frame.
To combine values of two columns separated with hyphen in an R data frame, we can use apply function.
For Example, if we have a data frame called df that contains only two columns say X and Y then we can combine the values in X and Y by using the below command given below −
df$X_Y<-apply(df,1,paste,collapse="-")
Example 1
Consider the data frame given below −
Age<-sample(20:50,20) Height<-sample(130:200,20) df1<-data.frame(Age,Height) df1
The following dataframe is created
Age Height 1 22 147 2 37 138 3 28 184 4 40 154 5 32 193 6 20 135 7 47 185 8 27 198 9 46 156 10 29 170 11 44 140 12 43 167 13 23 182 14 49 171 15 31 150 16 25 148 17 21 180 18 45 169 19 39 179 20 36 133
To combine the values of both columns in df1 separated with hyphen on the above created data frame, add the following code to the above snippet −
Age<-sample(20:50,20) Height<-sample(130:200,20) df1<-data.frame(Age,Height) df1$Age_Height<-apply(df1,1,paste,collapse="-") df1
Output
If you execute all the above given snippets as a single program, it generates the following Output −
Age Height Age_Height 1 22 147 22-147 2 37 138 37-138 3 28 184 28-184 4 40 154 40-154 5 32 193 32-193 6 20 135 20-135 7 47 185 47-185 8 27 198 27-198 9 46 156 46-156 10 29 170 29-170 11 44 140 44-140 12 43 167 43-167 13 23 182 23-182 14 49 171 49-171 15 31 150 31-150 16 25 148 25-148 17 21 180 21-180 18 45 169 45-169 19 39 179 39-179 20 36 133 36-133
Example 2
Following snippet creates a sample data frame −
Group<-sample(c("First","Second","Third"),20,replace=TRUE) Rate<-sample(1:10,20,replace=TRUE) df2<-data.frame(Group,Rate) df2
Output
If you execute the above given snippet, it generates the following Output −
Group Rate 1 First 8 2 Second 4 3 First 5 4 Second 7 5 Second 4 6 Third 7 7 Second 9 8 Second 7 9 First 7 10 Second 3 11 First 10 12 Second 9 13 First 7 14 First 8 15 Second 1 16 Second 8 17 Second 5 18 Third 10 19 Second 4 20 First 5
To combine the values of both columns in df2 separated with hyphen on the above created data frame, add the following code to the above snippet −
Group<-sample(c("First","Second","Third"),20,replace=TRUE) Rate<-sample(1:10,20,replace=TRUE) df2<-data.frame(Group,Rate) df2$Group_Rate<-apply(df2,1,paste,collapse="-") df2
Output
If you execute all the above given snippets as a single program, it generates the following Output −
Group Rate Group_Rate 1 First 8 First- 8 2 Second 4 Second- 4 3 First 5 First- 5 4 Second 7 Second- 7 5 Second 4 Second- 4 6 Third 7 Third- 7 7 Second 9 Second- 9 8 Second 7 Second- 7 9 First 7 First- 7 10 Second 3 Second- 3 11 First 10 First-10 12 Second 9 Second- 9 13 First 7 First- 7 14 First 8 First- 8 15 Second 1 Second- 1 16 Second 8 Second- 8 17 Second 5 Second- 5 18 Third 10 Third- 10 19 Second 4 Second- 4 20 First 5 First- 5
- Related Articles
- Combine two columns in R data frame with comma separation.
- How to change a data frame with comma separated values in columns to multiple columns in R?
- Combine two columns by ignoring missing values if exists in one column in R data frame.
- How to display the values of two columns of an R data frame separately in a plot?
- How to create table of two factor columns in an R data frame?
- How to combine two rows in R data frame by addition?
- How to concatenate string vectors separated with hyphen in R?
- How to multiply vector values in sequence with columns of a data frame in R?
- How to subset an R data frame based on string values of a columns with OR condition?
- Extract columns with a string in column name of an R data frame.
- How to find the minimum and maximum of columns values in an R data frame?
- How to compare two columns in an R data frame for an exact match?
- How to standardize columns in an R data frame?
- How to convert two columns of an R data frame to a named vector?
- How to remove rows that contains NA values in certain columns of an R data frame?
