You're using the assign operator (<<-) to store object 'xfile' in the global environment as an object named 'filtered_xfile'. This process is repeated in every iteration of the for loop in multiple_zscore, each time overwriting the previous result. That's one of the hazards you'll need to watch out for when working with assign. Since your call to zscore_misfit happens within the function multiple_zscore, you could just do something like
multiple_zscore <- function(xxx){
# Allocate an empty list to store results
result_list <- list()
for (i in seq_len(length(xxx))){
data <- zscore_misfit(data, xfile, xxx[i], "sidtp")
# Note: data and xfile are not provided as arguments in the function call.
# Perhaps because they can be assumed to exist in the global environment
# during runtime. However, I'd recommend adding these as arguments to the
# function definition of multiple_zscore.
# e.g. multiple_zscore <- function(xxx, data, xfile, sid = "sidtp"){...}
# Store result from zscore_misfit in result_list in ith entry.
result_list[[i]] <- data
}
# Return list of results.
return(result_list)
}
If you really do need to have each of these objects in the global environment (following your naming convention filtered_xfile1, filtered_xfile2, ...), you could do
multiple_zscore <- function(xxx){
for (i in seq_len(length(xxx))){
data <- zscore_misfit(data, xfile, xxx[i], "sidtp")
new_name <- paste0("filtered_xfile",i)
# Store object 'data' under the name 'new_name' in the global environment.
assign(new_name, data)
}
}