1

I have a table which contains the names of data frames in TSV format, like so:

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

These .tsv files I have them in the R environment. What I want to do is rbind them, and regarding this function, the inside should look like:

df2 <- rbind(`18-1829.tsv`, `19-0193.tsv`, `14-381.tsv`, `19-940.tsv`)

Note that I need the special quotes `` in order for this to function.

So what I want to do is print out a text where the output looks like:

dfX <- `18-1829.tsv`, `19-0193.tsv`, `14-381.tsv`, `19-940.tsv`

So I could simply do rbind(dfX) and bind them all.

So far I tried:

> paste(as.character(noquote(df1)), collapse="`, `")
[1] "18-1829.tsv`, `19-0193.tsv`, `14-381.tsv`, `19-940.tsv"

But this is rather wrong since it doesn't output `` at the beggining nor at the end, plus there's the [1] at the beggining which can mess up the thing going inside rbind. Also, the "" quotes at the beggining and end may mess it up aswell.

Maybe there is a better way to do this?

Sahira Mena
  • 411
  • 4
  • 14

2 Answers2

1

As df1 is a matrix, we can use mget on it. No need to insert special quotes.

do.call(rbind, mget(df1))

We can also use bind_rows from dplyr

dplyr::bind_rows(mget(df1))

Or data.table rbindlist

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

Using a reproducible example

`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
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

The main problem here is that after pasting the values, rbind will still view this as a string and will not compile it as object names.

Accordingly you can use mget which takes regex (regular expression) and returns objects with that name. There are multiple ways to bind data after.

Before binding

# 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 = "|")
Binding Method 1
df2 <- do.call(rbind, mget(ls(pattern = dfX)))
Binding Method 2
library(dplyr)
df2 <- mget(ls(pattern = dfX)) %>% bind_rows()
Binding Method 3
library(data.table)
df2 <- rbindlist(mget(ls(pattern = dfX)))
Nareman Darwish
  • 1,251
  • 7
  • 14