I have a recurrent situation where I set a value at the top of a long set of R code that's used in subsetting one or more data frames. Something like this:
city_code <- "202" At the end of the whole process I'd like to save the results in a data frame that's named appropriately, say, based on appending "city_code" to a common stub.
city_results <- paste("city_stats", city_code, sep = "") My problem is that I can't figure out how to rename the resulting data frame as the value of 'city_results'. Lots of info out there on how to rename the columns of a data frame, but not on how to rename the data frame itself. Based on a proposed answer, here's a clarification:
Thanks, @mike-wise. Helpful to study Hadley's Advanced R with a concrete problem in hand.
library(dplyr) gear_code <- 4 gear_subset <- paste("mtcars_", gear_code, sep = "") mtcars_subset <- mtcars %>% filter(gear == gear_code) head(mtcars_subset) write.csv(mtcars_subset, file = paste(gear_subset, ".csv", sep = "")) That lets me write the subset to an appropriately named csv file. However, your suggestion kind of works, but I can't, for example, reference the data.frame with the new name:
assign(gear_subset, mtcars_subset) head(gear_subset)
head(get(gear_subset)). The opposite ofassignisget.