Home > Software design >  How can i set a "key" argument in a gather function using a variable?
How can i set a "key" argument in a gather function using a variable?

Time:11-14

How can i set a "key" argument in a gather function using a variable ? as.character() and get() doesnt work either

library(tidyr)
library(dplyr)
path = "C:/Users/lukas/Documents/Projekt/Data/"
files <- list.files(path = path, pattern = "*.csv")

dane <- list()
for (file in files){
  temp_name <- file
 file <- tibble(read.csv(paste(path,file,sep = ""))) %>%
   gather("year", key = temp_name , -country)
 dane <- append(dane,list(file))
}

Error in `ensym2()`:
! Must supply a symbol or a string as argument

CodePudding user response:

We may escape with !!

dane <- list()
for (file in files){
  temp_name <- file
 file <- tibble(read.csv(paste(path,file,sep = ""))) %>%
   gather("year", key = !!temp_name , -country)
 dane <- append(dane,list(file))
}
  •  Tags:  
  • r
  • Related