以逗号分隔打印,删除引号并在R中添加特殊引号

以逗号分隔打印,删除引号并在R中添加特殊引号,r,printing,paste,rbind,R,Printing,Paste,Rbind,我有一个表,其中包含TSV格式的数据帧名称,如下所示: df1 <- t(c('18-1829.tsv', '19-0193.tsv', '14-381.tsv', '19-940.tsv')) df1 V1 V2 V3 V4 1 18-1829.tsv 19-0193.tsv 14-381.tsv 19-940.tsv 请注意,我需要特殊的引号`,以使其正常工作 所以我要做的是打印一个文本,输出如下: dfX <- `18

我有一个表,其中包含TSV格式的数据帧名称,如下所示:

df1 <- t(c('18-1829.tsv', '19-0193.tsv', '14-381.tsv', '19-940.tsv'))
df1

  V1          V2          V3         V4
1 18-1829.tsv 19-0193.tsv 14-381.tsv 19-940.tsv
请注意,我需要特殊的引号
`
,以使其正常工作

所以我要做的是打印一个文本,输出如下:

dfX <- `18-1829.tsv`, `19-0193.tsv`, `14-381.tsv`, `19-940.tsv`
但这是相当错误的,因为它在行乞时和行乞结束时都不输出
`
,加上行乞时有
[1]
,这会把里面的东西搞得一团糟。此外,begging和end的
引号也可能会把事情搞砸


也许有更好的方法吗?

因为
df1
是一个矩阵,我们可以在它上面使用
mget
。无需插入特殊报价

do.call(rbind, mget(df1))
我们还可以从
dplyr

dplyr::bind_rows(mget(df1))
data.table
rbindlist

data.table::rbindlist(mget(df1))

使用可复制的示例

`18-1829.tsv` <- head(mtcars)
`19-0193.tsv` <- head(mtcars)
df1 <- t(c('18-1829.tsv', '19-0193.tsv'))
dplyr::bind_rows(mget(df1))

#    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#1  21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#2  21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#3  22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#4  21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#5  18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#6  18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
#7  21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#8  21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#9  22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#10 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#11 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#12 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

`18-1829.tsv`这里的主要问题是,在粘贴值之后,rbind仍然会将其视为字符串,而不会将其编译为对象名

因此,您可以使用mget,它接受regex(正则表达式)并返回具有该名称的对象。有多种方法可以在发布后绑定数据

装订前
#创建包含对象名称的数据框
df1
data.table::rbindlist(mget(df1))
`18-1829.tsv` <- head(mtcars)
`19-0193.tsv` <- head(mtcars)
df1 <- t(c('18-1829.tsv', '19-0193.tsv'))
dplyr::bind_rows(mget(df1))

#    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#1  21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#2  21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#3  22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#4  21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#5  18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#6  18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
#7  21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#8  21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#9  22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#10 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#11 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#12 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
# Creating the dataframe which contains the names of your objects
df1 <- t(c('18-1829.tsv', '19-0193.tsv', '14-381.tsv', '19-940.tsv'))

# Simulating the objects that you have in your enviroment.
`18-1829.tsv` <- data.frame(a = 0)
`19-0193.tsv` <- data.frame(a = 0)
`14-381.tsv` <- data.frame(a = 0)
`19-940.tsv` <- data.frame(a = 0)

# Pasting the names of the objects and collapsing them using | meaning OR in regex
dfX <- paste0(noquote(df1), collapse = "|")
df2 <- do.call(rbind, mget(ls(pattern = dfX)))
library(dplyr)
df2 <- mget(ls(pattern = dfX)) %>% bind_rows()
library(data.table)
df2 <- rbindlist(mget(ls(pattern = dfX)))