这是一个重复的问题。
如何从此数据框中实现
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)
其中包含字符串值的新列:
New Column
HS12003
C12003
H12003
S12003
TR12003
我不知道如何“加和”字符串字符。预先感谢!
答案 0 :(得分:1)
您可以使用do.call
来评估paste0
,即
do.call(paste0, df[-3])
#[1] "HS12003" "C12003" "H12003" "S12003" "TR12003"
答案 1 :(得分:0)
您需要使用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
解决方案:
library(dplyr)
df %>%
mutate(pasted = paste0(Treatment, BlockID, Data))