Combine values of two columns separated with hyphen in an R data frame.

R ProgrammingServer Side ProgrammingProgramming

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
raja
Updated on 28-Oct-2021 09:29:59

Advertisements