1

Below I have two columns of data (column 6 and 7) of genus and species names. I would like to combine those two columns with character string data into a new column with the names combined.

I am quite new to R and the code below does not work! Thank you for the help wonderful people of stack overflow!

#TRYING TO MIX GENUS & SPECIES COLUMN 
accepted_genus <- merged_subsets_2[6]
accepted_species <- merged_subsets_2[7]

accepted_genus
accepted_species

merged_subsets_2%>%
  bind_cols(accepted_genus, accepted_species)
merged_subsets_2

2 Answers2

1

Please take a look at this if this doesn't answer your question.

df <- data.frame(Col1 = letters[1:2], Col2=LETTERS[1:2])  # Sample data
> df
  Col1 Col2
1    a    A
2    b    B
df$Col3 <- paste0(df$Col1, df$Col2)  # Without spacing
> df
  Col1 Col2 Col3
1    a    A   aA
2    b    B   bB
df$Col3 <- paste(df$Col1, df$Col2)
> df
  Col1 Col2 Col3
1    a    A  a A
2    b    B  b B
Trusky
  • 483
  • 2
  • 13
1

We can use str_c from stringr

library(dplyr)
library(stringr)
df %>%
     mutate(Col3 = str_c(Col1, Col2))

Or with unite

library(tidyr)
df %>%
    unite(Col3, Col1, Col2, sep="", remove = FALSE)
akrun
  • 874,273
  • 37
  • 540
  • 662