Using returned dataframe functions in a dplyr sequence

I would like to use the return data.frame function from dplyr to read data from Excel files whose location I want to customize easily. Here I present my problem using a simplified get_table () function and two generated data.frames. In fact, the get_table () function retrieves data from the server and parses it.

When calling a function from dplyr, all data.frame data must be concatenated. Here's a simplified code:

files <- read.table(header=T, text='
  type      filename
  A         A_table
  B         B_table
')

A_table <- read.table(header=T, text='
  area      observations
  K1        5
  K2        9
')

B_table <- read.table(header=T, text='
  area      observations
  K1        23
  K2        28
  K3        1
')

get_table <- function(name) {
  return(get(name))
}

      

I can read files using lapply:

list <- as.vector(files[,2])
t <- lapply(list, get_table)
do.call("rbind", t)

      

And combine the results into:

  area observations
1   K1            5
2   K2            9
3   K1           23
4   K2           28
5   K3            1

      

I would like, however, to try and do the same in dplyr style by doing something like this (but working it doesn't):

files %>%
  select(filename) %>%
    rowwise() %>% 
      get_table()

      

+3


source to share


1 answer


As @Richard Scriven pointed out filename

should be a symbol.

files <- read.table(header=T, stringsAsFactors=FALSE, text='
  type      filename
  A         A_table
  B         B_table
')

      



Applying do

to the last line of your code achieves the same result as lapply(files[ ,2], get) %>% rbind_all

.

files %>%
  rowwise() %>% 
  do(get_table(.$filename))

#Groups: <by row>

#  area observations
#1   K1            5
#2   K2            9
#3   K1           23
#4   K2           28
#5   K3            1

      

+2


source







All Articles