0

This is a duplicated question.

How do I achieve from this dataframe:

Treatment = c("HS","C","H","S","TR")
BlockID = c(1,1,1,1,1)
PlotID = c(1,2,4,5,6)
Data = c(2003,2003,2003,2003,2003)

df = data.frame(Treatment,BlockID,PlotID,Data)

A new column which has in it the string values:

New Column
HS12003
C12003
H12003
S12003
TR12003

I cannot figure out how to "sum" string characters. Thanks in advance!

Afke
  • 899
  • 2
  • 10
  • 21

2 Answers2

1

You can use do.call to evaluate paste0, i.e.

do.call(paste0, df[-3])
#[1] "HS12003" "C12003"  "H12003"  "S12003"  "TR12003"
Sotos
  • 51,121
  • 6
  • 32
  • 66
0

You need to use paste0()

df$pasted <- paste0(df$Treatment, df$BlockID, df$Data)

#   Treatment BlockID PlotID Data  pasted
# 1        HS       1      1 2003 HS12003
# 2         C       1      2 2003  C12003
# 3         H       1      4 2003  H12003
# 4         S       1      5 2003  S12003
# 5        TR       1      6 2003 TR12003

dplyr solution:

library(dplyr)
df %>% 
  mutate(pasted = paste0(Treatment, BlockID, Data))
RLave
  • 8,144
  • 3
  • 21
  • 37