To concatenate column values into new column in R, you can use two different function i.e. paste() function and unite() function.

The following methods show how you can use it with syntax.

Method 1: Use paste() Function

paste(df$column1, df$column2, df$column3,..... , sep = "_")

Method 2: Use unite() Function

unite(df, new_column, column1, column2, column3,...., sep = "_")

Let’s create data frame first to apply this function on it:

# Create data frame
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
                 Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
                 Value = c(108,120,135,95,98,105),Reading= c(110,97,91,89,80,85))

# Print data frame 
print(df)

Output:

  Start_date Machine_name Value Reading
1 2000-05-21     Machine1   108     110
2 1998-03-28     Machine2   120      97
3 2001-01-19     Machine3   135      91
4 2003-04-27     Machine4    95      89
5 2004-11-26     Machine5    98      80
6 2008-11-25     Machine6   105      85

Let’s apply this both function on this data frame.

The following examples shows how to concatenate column values into new column of data frame.

Concatenate Column Values into New Column of Data Frame Using paste() Function in R

Let’s see how we can use paste() to concatenate column values into new column of data frame:

# Create data frame
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
                 Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
                 Value = c(108,120,135,95,98,105),Reading= c(110,97,91,89,80,85))

# Concate all columns
df$all_data <- paste(df$Start_date, df$Machine_name, df$Value, df$Reading, sep = "_")

# Print updated data frame 
print(df)

Output:

  Start_date Machine_name Value Reading                    all_data
1 2000-05-21     Machine1   108     110 2000-05-21_Machine1_108_110
2 1998-03-28     Machine2   120      97  1998-03-28_Machine2_120_97
3 2001-01-19     Machine3   135      91  2001-01-19_Machine3_135_91
4 2003-04-27     Machine4    95      89   2003-04-27_Machine4_95_89
5 2004-11-26     Machine5    98      80   2004-11-26_Machine5_98_80
6 2008-11-25     Machine6   105      85  2008-11-25_Machine6_105_85

As the output shows updated data frame which having values of all column into all_data column.

Concatenate Column Values into New Column of Data Frame Using unite() Function in R

Let’s see how to use unite() function in R:

# Create data frame
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
                 Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
                 Value = c(108,120,135,95,98,105),Reading= c(110,97,91,89,80,85))

# Concate all columns
df$all_data <- unite(df, all_data, Start_date, Machine_name, Value, Reading, sep = "_")

# Print updated data frame
print(df)

Output:

  Start_date Machine_name Value Reading                    all_data
1 2000-05-21     Machine1   108     110 2000-05-21_Machine1_108_110
2 1998-03-28     Machine2   120      97  1998-03-28_Machine2_120_97
3 2001-01-19     Machine3   135      91  2001-01-19_Machine3_135_91
4 2003-04-27     Machine4    95      89   2003-04-27_Machine4_95_89
5 2004-11-26     Machine5    98      80   2004-11-26_Machine5_98_80
6 2008-11-25     Machine6   105      85  2008-11-25_Machine6_105_85

Here the output shows updated data frame with new column which having values from all columns.