Given a character vector, I would like to loop through a function with a name assignment.
uprop
is a "data.frame" (1000 observations and 20 columns), as listed in output below:
> class(uprop)
[1] "data.frame"
And Department, Source, Target, and WeightCount are all column names in uprop
Let us say we need to simplify this repetitive task:
CAST_uprop_data <- subset(uprop, Department == "CAST", select = c(Source, Target, WeightCount))
CHEG_uprop_data <- subset(uprop, Department == "CHEG", select = c(Source, Target, WeightCount))
PHYS_uprop_data <- subset(uprop, Department == "PHYS", select = c(Source, Target, WeightCount))
Here CAST_uprop_data
is also a data.frame. (100 observations and 3 columns)
I can create a vector variable cust_dept_list
with the character names:
cust_dept_list <- c('CAST', 'CHEG', 'PHYS')
but, I can not figure out how to loop through the names and have it run and assign each one?
Here is my attempt:
for (i in c(cust_dept_list)){
print(paste0(i,"_uprop_data")) <- subset(uprop, Department == i, select = c(Source, Target, WeightCount)), i
}
Thanks in advance for helping a novice.