我如何连接(合并,组合)两个值? 例如,我有:
tmp = cbind("GAD", "AB")
tmp
# [,1] [,2]
# [1,] "GAD" "AB"
我的目标是将“tmp”中的两个值连接到一个字符串:
tmp_new = "GAD,AB"
哪个函数可以帮我做这个?
我如何连接(合并,组合)两个值? 例如,我有:
tmp = cbind("GAD", "AB")
tmp
# [,1] [,2]
# [1,] "GAD" "AB"
我的目标是将“tmp”中的两个值连接到一个字符串:
tmp_new = "GAD,AB"
哪个函数可以帮我做这个?
当前回答
Glue是作为tidyverse的一部分开发的一个新函数、数据类和包,具有许多扩展功能。它结合了paste、sprintf和前面其他答案的特性。
tmp <- tibble::tibble(firststring = "GAD", secondstring = "AB")
(tmp_new <- glue::glue_data(tmp, "{firststring},{secondstring}"))
#> GAD,AB
由reprex包于2019-03-06创建(v0.2.1)
是的,对于这个问题中的简单例子来说,它有点过头了,但在许多情况下都很强大。(参见https://glue.tidyverse.org/)
快速的例子比较粘贴与下面。胶水代码更容易输入,看起来也更容易阅读。
tmp <- tibble::tibble(firststring = c("GAD", "GAD2", "GAD3"), secondstring = c("AB1", "AB2", "AB3"))
(tmp_new <- glue::glue_data(tmp, "{firststring} and {secondstring} went to the park for a walk. {firststring} forgot his keys."))
#> GAD and AB1 went to the park for a walk. GAD forgot his keys.
#> GAD2 and AB2 went to the park for a walk. GAD2 forgot his keys.
#> GAD3 and AB3 went to the park for a walk. GAD3 forgot his keys.
(with(tmp, paste(firststring, "and", secondstring, "went to the park for a walk.", firststring, "forgot his keys.")))
#> [1] "GAD and AB1 went to the park for a walk. GAD forgot his keys."
#> [2] "GAD2 and AB2 went to the park for a walk. GAD2 forgot his keys."
#> [3] "GAD3 and AB3 went to the park for a walk. GAD3 forgot his keys."
由reprex包于2019-03-06创建(v0.2.1)
其他回答
考虑这样一种情况,字符串是列,结果应该是一个新列:
df <- data.frame(a = letters[1:5], b = LETTERS[1:5], c = 1:5)
df$new_col <- do.call(paste, c(df[c("a", "b")], sep = ", "))
df
# a b c new_col
#1 a A 1 a, A
#2 b B 2 b, B
#3 c C 3 c, C
#4 d D 4 d, D
#5 e E 5 e, E
如果需要粘贴所有列,可以跳过[c("a", "b")]子设置。
# you can also try str_c from stringr package as mentioned by other users too!
do.call(str_c, c(df[c("a", "b")], sep = ", "))
正如其他人所指出的,paste()是正确的方法。但是每次需要使用非默认分隔符时,都必须输入paste(str1, str2, str3, sep= "),这很烦人。
您可以非常容易地创建包装器函数,使工作变得更加简单。例如,如果你发现自己经常连接没有分隔符的字符串,你可以这样做:
p <- function(..., sep='') {
paste(..., sep=sep, collapse=sep)
}
或者如果你经常想从一个向量连接字符串(比如PHP中的implode()):
implode <- function(..., sep='') {
paste(..., collapse=sep)
}
允许你这样做:
p('a', 'b', 'c')
#[1] "abc"
vec <- c('a', 'b', 'c')
implode(vec)
#[1] "abc"
implode(vec, sep=', ')
#[1] "a, b, c"
此外,还有内置的paste0,它做的事情与我的implode相同,但不允许自定义分隔符。它比paste()稍微高效一些。
给定你创建的矩阵tmp:
paste(tmp[1,], collapse = ",")
我假设有一些原因,为什么你创建一个矩阵使用cbind,而不是简单地:
tmp <- "GAD,AB"
Glue是作为tidyverse的一部分开发的一个新函数、数据类和包,具有许多扩展功能。它结合了paste、sprintf和前面其他答案的特性。
tmp <- tibble::tibble(firststring = "GAD", secondstring = "AB")
(tmp_new <- glue::glue_data(tmp, "{firststring},{secondstring}"))
#> GAD,AB
由reprex包于2019-03-06创建(v0.2.1)
是的,对于这个问题中的简单例子来说,它有点过头了,但在许多情况下都很强大。(参见https://glue.tidyverse.org/)
快速的例子比较粘贴与下面。胶水代码更容易输入,看起来也更容易阅读。
tmp <- tibble::tibble(firststring = c("GAD", "GAD2", "GAD3"), secondstring = c("AB1", "AB2", "AB3"))
(tmp_new <- glue::glue_data(tmp, "{firststring} and {secondstring} went to the park for a walk. {firststring} forgot his keys."))
#> GAD and AB1 went to the park for a walk. GAD forgot his keys.
#> GAD2 and AB2 went to the park for a walk. GAD2 forgot his keys.
#> GAD3 and AB3 went to the park for a walk. GAD3 forgot his keys.
(with(tmp, paste(firststring, "and", secondstring, "went to the park for a walk.", firststring, "forgot his keys.")))
#> [1] "GAD and AB1 went to the park for a walk. GAD forgot his keys."
#> [2] "GAD2 and AB2 went to the park for a walk. GAD2 forgot his keys."
#> [3] "GAD3 and AB3 went to the park for a walk. GAD3 forgot his keys."
由reprex包于2019-03-06创建(v0.2.1)
你可以创建自己的操作符:
'%&%' <- function(x, y)paste0(x,y)
"new" %&% "operator"
[1] newoperator`
你也可以重新定义'and'(&)操作符:
'&' <- function(x, y)paste0(x,y)
"dirty" & "trick"
"dirtytrick"
混淆基线语法是丑陋的,但是使用paste()/paste0()也是如此,如果你只使用自己的代码,你可以(几乎总是)将逻辑&和操作符替换为*,并执行逻辑值的乘法,而不是使用逻辑'and '