Reputation: 117
I am coding a for loop to import 6 dataframes and applying to them certain treatments.
b = 6
for (i in 1:b) {
a = as.numeric(20200518)
d = as.Date("2020-05-18")
assign(paste("vacantes_", a + i, sep = ""), read_excel(paste("Favorites/Tito/01. aaaaaa/aaaaaaa 20/05. Data/
01. aa/01. Módulo aaaaaaaaaaa/Vacante_inconsistencias_", a + i, ".xlsx", sep = ""),col_names = TRUE))
assign(paste("vacantes_", a + i, "_llenado", sep = ""), get(paste("vacantes_", a + i, sep = "")) %>%
select("ddd", "uuuu", Llenó", "cod") %>%
group_by(ddd, uuuu, `Llenó`) %>% summarise(Cantidad = n()))
assign(paste("vacantes_", a + i, "_llenado1", sep = ""),
dcast(get(paste("vacantes_", a + i, "_llenado", sep = "")), ddd + uuuu ~ `Llenó`, sum) %>%
mutate(Fecha = as.Date(d + i)) %>% select("Fecha", "ddd", "uuuu", "NO", "SÍ"))
if(i == b){
rm(a, b, i, d)
}
}
In the last process Cast
(third function), I wanted to rename two columns in each dataframe so I add the last line to that part of the code:
## Cast
assign(paste("vacantes_", a + i, "_llenado1", sep = ""),
dcast(get(paste("vacantes_", a + i, "_llenado", sep = "")), ddd + uuuu ~ `Llenó`, sum) %>%
mutate(Fecha = as.Date(d + i)) %>% select("Fecha", "ddd", "uuuu", "NO", "SÍ") %>%
rename(paste("NO_", a + 1, sep ="") = NO, paste("SI_", a + 1, sep ="") = SÍ))
The rename function causes the for loop to not work at all. I got:
Error: inesperado '=' in:
" i, "_llenado1", sep = ""), dcast(get(paste("vacantes_", a + i, "_llenado", sep = "")), ddd + uuuu ~ `Llenó`, sum) %>%
mutate(Fecha = as.Date(d + i)) %>% select("Fecha", "ddd", "uuuu", "NO", "SÍ")) %>% rename_(paste("NO_", a + 1, sep ="") ="
I tried adding rename(get(paste("NO_", a + 1, sep ="")) = NO, get(paste("SI_", a + 1, sep ="")) = SÍ)
but it doesn't work either.
Just to clarify, I removed the rename
line and the for loop worked as I wanted so far so that line of code is the problem. Thanks in advance.
Upvotes: 1
Views: 3066
Reputation: 887981
If we are using paste
, then an option is to assign (:=
) while evaluating (!!
)
library(dplyr)
head(mtcars) %>%
rename(!!paste0('NO_', 2) := mpg, 'SI' = carb)
# NO_2 cyl disp hp drat wt qsec vs am gear SI
#Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Upvotes: 4
Reputation: 1495
You should add a sample of your data e.g. with dput
. Would it not be easier to read all these data objects into a list:
path <- "./files"
files <- list.files(path = path,
full.names = TRUE,
all.files = FALSE)
files <- files[!file.info(files)$isdir]
data <- lapply(files,
function(x) {
read_excel(x, ,col_names = TRUE)
})
And then apply whatever data transformation you require? You can do this by using lapply
in combination with your transformation function. Or you can bind_rows
the lot together and use your dplyr
verbs to do the transformation.
Upvotes: -1