Combine two or more columns in a dataframe into a new column with a new name

r combine two columns into one
stack two columns in r
concatenate columns in r dplyr
combine two data frames in r with different columns
r combine two columns into one vector
r merge multiple data frames by column names
r combine columns with same name
combine multiple variables into one in r

For example if I have this:

n = c(2, 3, 5) 
s = c("aa", "bb", "cc") 
b = c(TRUE, FALSE, TRUE) 
df = data.frame(n, s, b)

  n  s     b
1 2 aa  TRUE
2 3 bb FALSE
3 5 cc  TRUE

Then how do I combine the two columns n and s into a new column named x such that it looks like this:

  n  s     b     x
1 2 aa  TRUE  2 aa
2 3 bb FALSE  3 bb
3 5 cc  TRUE  5 cc

Use paste.

 df$x <- paste(df$n,df$s)
 df
#   n  s     b    x
# 1 2 aa  TRUE 2 aa
# 2 3 bb FALSE 3 bb
# 3 5 cc  TRUE 5 cc

Combine two columns of text in dataframe in pandas/python, How do I add a column from one DataFrame to another? df = data.frame(n, s, b) n s b. 1 2 aa TRUE. 2 3 bb FALSE. 3 5 cc TRUE. Then how do I combine the two columns n and s into a new column named x such that it looks like this: n s b x. 1 2 aa TRUE 2 aa. 2 3 bb FALSE 3 bb. 3 5 cc TRUE 5 cc

For inserting a separator:

df$x <- paste(df$n, "-", df$s)

r, Combine two or more columns in a dataframe into a new column with a new "​cc") b = c(TRUE, FALSE, TRUE) df = data.frame(n, s, b) n s b 1 2 aa TRUE be a one-liner: tidyr::unite(df, x, n, s, sep = " ", remove = FALSE)[, c(names(df), "x")​] . I have the following dataframe: pp b pp b 5 0.001464 6 0.001853 5 0.001459 6 0.001843 Is there a way to combine columns with the same name? I just want this as output: pp b 5 0.001464 5 0.001459 6 0.001853 6 0.001843

As already mentioned in comments by Uwe and UseR, a general solution in the tidyverse format would be to use the command unite:

library(tidyverse)

n = c(2, 3, 5) 
s = c("aa", "bb", "cc") 
b = c(TRUE, FALSE, TRUE) 

df = data.frame(n, s, b) %>% 
  unite(x, c(n, s), sep = " ", remove = FALSE)

Combining DataFrames with Pandas – Data Analysis and , Combine two or more columns in a dataframe into a new column with a new name. n = c(2, 3, 5) s = c("aa", "bb", "cc") b = c(TRUE, FALSE, TRUE) df = data.frame(n, s, b) n s b. 1 2 aa TRUE. 2 3 bb FALSE. 3 5 cc TRUE. A consolidated column that had all the items in other columns of lists, but without repeating items. So an entry with [A, B] in column 1 and [B, C] in column 2 will result in a cosolidated entry of [A, B, C]. Change all the NaN values to empty lists. Generate a list of dummy columns from all items in the lists (as per the below column).

Some examples with NAs and their removal using apply

n = c(2, NA, NA) 
s = c("aa", "bb", NA) 
b = c(TRUE, FALSE, NA) 
c = c(2, 3, 5) 
d = c("aa", NA, "cc") 
e = c(TRUE, NA, TRUE) 
df = data.frame(n, s, b, c, d, e)

paste_noNA <- function(x,sep=", ") {
gsub(", " ,sep, toString(x[!is.na(x) & x!="" & x!="NA"] ) ) }

sep=" "
df$x <- apply( df[ , c(1:6) ] , 1 , paste_noNA , sep=sep)
df

How to Join Two Text Columns into a Single Column in Pandas , Combine data from multiple files into a single DataFrame using merge and concat. Combine two DataFrames using a unique ID found in both DataFrames. Join DataFrames using common fields (join keys). and taxa names that we want to join with the data in survey_sub to produce a new DataFrame that contains all of  I want to merge them together to get a final data frame, joining on the org and name values, and then prefixing all other columns with an appropriate prefix. org,name,presents_items,presents_spend,trees_items,trees_spend 12A,Clerkenwell,151,435,0,0, 12B,Liverpool Street,37,212,2,92, I've been reading the documentation on merging and

Using dplyr::mutate:

library(dplyr)
df <- mutate(df, x = paste(n, s)) 

df 
> df
  n  s     b    x
1 2 aa  TRUE 2 aa
2 3 bb FALSE 3 bb
3 5 cc  TRUE 5 cc

Merge, join, and concatenate, How to Combine Two Text Columns in to One Column in Pandas? Here, we made a toy data frame with three columns and last name and first Now we have created a new column combining the first and last names. Give the two columns the same name >>> df.columns = df.columns.str.replace('.*?station', 'station') >>> df Duration station station 0 1407 NaN 14th & V St NW 1 509 NaN 21st & I St NW 2 638 15th & P St NW.

Merge, join, and concatenate, Before diving into all of the details of concat and what it can do, here is a simple the column names when creating a new DataFrame based on existing Series . joining an index (unique) to one or more columns in a different DataFrame . I want to concatenate three columns instead of concatenating two columns: Here is the combining two columns: df = DataFrame({'foo':['a','b','c'], 'ba Stack Overflow Products

Join two text columns into a single column in Pandas, The Series will be transformed to DataFrames with the column name as the by passing a Series or dict to append , which returns a new DataFrame as above. to one or more columns in a DataFrame; many-to-many joins: joining columns  Joining multiple columns is just a matter of passing either a list of series or a dataframe containing all but the first column as a parameter to str.cat() invoked on the first column (Series): >>> df = pd.DataFrame(

Data Wrangling in R: Combining, Merging and Reshaping Data, Let's see the different methods to join two text columns into a single column. DataFrame · Change Data Type for one or more columns in Pandas Dataframe  Concatenating two columns of the dataframe in pandas can be easily achieved by using simple ‘+’ operator. Concatenate or join of two string column in pandas python is accomplished by cat() function. we can also concatenate or join numeric and string column.

Comments
  • .@thelatemail - How to add a special character between data points using paste()? For above example, x column should have data as 2-aa, then 3-bb and 5-cc.
  • .@thelatemail - This worked for me: paste(df$n,df$s,sep="-")
  • how can you omit NA if column s has NA value? (I don't like to see 3 NA if df$s[2]=NA)
  • .@LittleBee - This adds a space between two data. Final output for example is like: A - B instead of A-B. Is it possible to remove this extra space?
  • .@LittleBee - This worked for me: paste(df$n,df$s,sep="-")
  • use paste0 instead of paste
  • This won't give the desired output : OP asks for a space in between the elements, not another separator (which, by the way, would be better put as the sep argument...). The other answer, posted almost 4 years prior to yours, is however perfectly answering the question.
  • What is x in this example?
  • If you want to use the tidyr package to reproduce the expected answer of the original question this would be a one-liner: tidyr::unite(df, x, n, s, sep = " ", remove = FALSE)[, c(names(df), "x")]. However, I don't see a reason to do so as df$x <- paste(df$n,df$s) is much simpler.
  • @Ferroao Well, answers shouldn't be too general either, otherwise every question would just have one giant answer incorporating everything. The removal of NA's was not part of OP's simple question, so I don't see how that extra complexity adds value to a simple paste or tidyr::unite.