Combine two columns by ignoring missing values if exists in one column in R data frame.

R ProgrammingServer Side ProgrammingProgramming

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
raja
Updated on 08-Nov-2021 10:17:34

Advertisements