获取带引号而不带引号的输出

时间:2019-05-25 12:31:47

标签: r paste quotes factors

必需的输出

c('A'=5,'B'=5,'C'=5)

发生了什么

df1 <- factor(LETTERS[1:3])
paste(levels(df1), 5, sep = "=")
#> [1] "A=5" "B=5" "C=5"

noquote(capture.output(cat(paste(shQuote(levels(df1)), 5, sep = "="), sep = ",")))
#> [1] 'A'=5,'B'=5,'C'=5

请获得所需输出的任何帮助。

3 个答案:

答案 0 :(得分:2)

我们可以使用setNames来命名向量,并使用rep来创建数字向量。

setNames(rep(5, length(df1)), df1)

#A B C 
#5 5 5 

答案 1 :(得分:1)

onclick

答案 2 :(得分:1)

我们可以使用replicate

setNames(replicate(length(df1), 5), df1)
# A B C      
# 5 5 5 

或更紧凑

library(tibble)
deframe(tibble(df1, 5))
#   A B C 
#  5 5 5