I'm having trouble getting a function to save separate data frames. It keeps overwriting the name of a variable in the function. The code I have is
LFStable = function(LFS_number, vector_number, titles_number){
LFS_number <- get_cansim_vector(vector_number,
start_time = as.Date(startdate),
end_time = today(),
refresh = FALSE)
Temp <- data.frame(get_cansim_vector_info(vector_number))
Temp <- Temp %>% select(title, VECTOR, table) %>% separate(title,
titles_number,";")
LFS_number <<- left_join(LFS_number,Temp, by = "VECTOR")
rm(Temp)
}
LFStable(LFS0063, V0063, title0063)
LFS_number is the number of the table I'm pulling from. vector_number is the list of vectors from Stats Canada I need from that table. titles_number is the name of the columns I need from the table.
It is making the table in the correct format but instead of naming the table "LFS0063" it names it "LFS_number" and then overwrites it when I run the function again for another table.
How can I get the table to be saved to the global environment with the name I gave it?
Thanks for reading and trying to help!
Edit: based on the comments from @MrFlick and @r2evans , I changed the code to
LFStable = function(LFS_number, vector_number, titles_number){
Temp_table <- get_cansim_vector(vector_number,
start_time = as.Date(startdate),
end_time = today(),
refresh = FALSE)
Temp <- data.frame(get_cansim_vector_info(vector_number))
Temp <- Temp %>% select(title, VECTOR, table) %>% separate(title,
titles_number,";")
LFS_number <<- left_join(Temp_table,Temp, by = "VECTOR")
rm(Temp, vector_number)
}
LFStable(LFS0063, V0063, title0063)
Which produces the same problem as before. OR
Temp_table <- left_join(Temp_table,Temp, by = "VECTOR")
assign(LFS_number,Temp_table,envir=.GlobalEnv)
rm(Temp, vector_number)
}
LFStable(LFS0063, V0063, title0063)
which gives an error saying "invalid first argument". I created empty data frames with the LFS_number names to assign them to before running the function.