- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 two columns by ignoring missing values if exists in one column in R data frame.
To combine two columns by ignoring missing values if exists in one column in R data frame, we can use paste function and is.na function.
For Example, if we have a data frame called df that contains two columns say C1 and C2 where C2 contains some missing values then we can use the below mentioned command to combine C1 and C2 by ignoring missing values in C2 −
cbind(df,Combined=paste(df[,1],replace(df[,2],is.na(df[,2]),"")))
Example 1
Following snippet creates a sample data frame −
x1<-rpois(20,5) y1<-sample(c(NA,2,5),20,replace=TRUE) df1<-data.frame(x1,y1) df1
The following dataframe is created
x1 y1 1 3 2 2 7 2 3 6 5 4 9 5 5 6 NA 6 2 2 7 3 NA 8 5 2 9 6 5 10 7 5 11 6 NA 12 2 5 13 2 2 14 7 5 15 6 5 16 7 2 17 7 NA 18 3 NA 19 4 5 20 6 5
To combine x1 and y1 values on the above created data frame, add the following code to the above snippet −
x1<-rpois(20,5) y1<-sample(c(NA,2,5),20,replace=TRUE) df1<-data.frame(x1,y1) cbind(df1,Combined=paste(df1[,1],replace(df1[,2],is.na(df1[,2]),"")))
Output
If you execute all the above given snippets as a single program, it generates the following Output −
x1 y1 Combined 1 3 2 3 2 2 7 2 7 2 3 6 5 6 5 4 9 5 9 5 5 6 NA 6 6 2 2 2 2 7 3 NA 3 8 5 2 5 2 9 6 5 6 5 10 7 5 7 5 11 6 NA 6 12 2 5 2 5 13 2 2 2 2 14 7 5 7 5 15 6 5 6 5 16 7 2 7 2 17 7 NA 7 18 3 NA 3 19 4 5 4 5 20 6 5 6 5
Example 2
Following snippet creates a sample data frame −
x2<-sample(c(NA,500,450),20,replace=TRUE) y2<-sample(1:1000,20) df2<-data.frame(x2,y2) df2
The following dataframe is created
x2 y2 1 500 389 2 500 164 3 NA 267 4 NA 68 5 NA 294 6 500 26 7 500 740 8 450 913 9 NA 556 10 NA 800 11 450 80 12 500 236 13 500 65 14 NA 316 15 450 248 16 NA 654 17 NA 113 18 500 691 19 NA 496 20 450 391
To combine x2 and y2 values on the above created data frame, add the following code to the above snippet −
x2<-sample(c(NA,500,450),20,replace=TRUE) y2<-sample(1:1000,20) df2<-data.frame(x2,y2) cbind(df2,Combined=paste(df2[,2],replace(df2[,1],is.na(df2[,1]),"")))
Output
If you execute all the above given snippets as a single program, it generates the following Output −
x2 y2 Combined 1 500 389 389 500 2 500 164 164 500 3 NA 267 267 4 NA 68 68 5 NA 294 294 6 500 26 26 500 7 500 740 740 500 8 450 913 913 450 9 NA 556 556 10 NA 800 800 11 450 80 80 450 12 500 236 236 500 13 500 65 65 500 14 NA 316 316 15 450 248 248 450 16 NA 654 654 17 NA 113 113 18 500 691 691 500 19 NA 496 496 20 450 391 391 450
- Related Articles
- How to find the row mean for columns in an R data frame by ignoring missing values?
- Roll up R data frame columns for summation by group if missing values exist in the data frame.
- How to combine columns by excluding missing values in R?
- Combine values of two columns separated with hyphen in an R data frame.
- Combine two columns in R data frame with comma separation.
- How to combine multiple columns into one in R data frame without using column names?
- How to find the sum of every n values if missing values exists in the R data frame?
- How to subset an R data frame by ignoring a value in one of the columns?
- How to remove duplicate rows in an R data frame if exists in two columns?
- How to combine two rows in R data frame by addition?
- How to replace missing values with median in an R data frame column?
- How to repeat column values in R data frame by values in another column?
- Find the number of non-missing values in each column by group in an R data frame.\n
- How to add two columns if both contains missing values in R?
- How to replace missing values in a column with corresponding values in other column of an R data frame?
