function - Dynamically assigning calculation results in R -


i'm in first week programming in r , while i've made progress on solving specific issues, in need advice on larger scale.

i have directory full of data files in csv format. file names identify data source. need import data, condition data through various calculations, , keep results of each file's conditioning analysis , review. have learned open , extensive conditioning of data on individual file basis. conditioning results in multiple calculation output. need automate process , dynamically name results based on respective file name.

since data conditioning same each file, i've written function can called each file. understand functions operate in own environment disappears after function runs. can dynamically name variables using paste build names , assign assign results names. assignments lost when function closes.

i'm not of optimal way step through files , keep individual calculation results available in workspace. know i'm "supposed to" write function output single list can later index. however, have hundreds of calculation results , later indexing complicated. lets 2 of files contains air temperature measurements @ different locations. since dynamically name calculation results based on descriptive file names, can have results stored temperature.air.location1 , temperature.air.location2. prefer ability later calculate temperature delta typing temperature.air.location1 - temperature.air.location2 instead of having corresponding indices of large list.

i'm there elegant way of achieving that's staring me in face, i'm afraid i've gotten wrapped in learning functions, interpolation, , plotting in r i've lost sight of big picture. advice appreciated.

edit add example code in portion of function, i'm converting table x,y,z coordinates interpolating values.

calibrationimport.table <- function(filename, parametername, xmin, xmax, ymin, ymax){   path.file <- paste0(path.folder,filename)   assign(parametername, read.csv(path.file, header = false))    # extract x coordinates original table   assign(paste0(parametername,".x"), get(parametername)[1, ])   assign(paste0(parametername,".x"), unlist(get(paste0(parametername,".x"))[-1], use.names=false))   assign(paste0(parametername,".x"), c(t(replicate(nrow(get(parametername))-1, get(paste0(parametername,".x"))))))    # extract y coordinates original table   assign(paste0(parametername,".y"), get(parametername)[ ,1])   assign(paste0(parametername,".y"), unlist(get(paste0(parametername,".y"))[-1], use.names=false))   assign(paste0(parametername,".y"), c(replicate(ncol(get(parametername))-1, get(paste0(parametername,".y")))))    # extract data original table   assign(paste0(parametername,".z"), unlist(get(parametername)[-1, -1], use.names=false))    # interpolate 100x100 surface   assign(paste0(parametername,".i"), interp(get(paste0(parametername,".x")), get(paste0(parametername,".y")), get(paste0(parametername,".z")),                                         xo=seq(xmin, xmax, length=100), yo=seq(ymin, ymax, length=100))) } 

in general workflow works me use lapply. example:

file_names = list.files(pattern = "*csv") data_list = lapply(file_names, read.csv)  perform_interpolation = function(dataset) {    # perform interpolation on dataset    return(interpolated_dataset) } interpolated_data_list = lapply(data_list, perform_interpolation) 

here have lists of objects transform using functions (i.e. functional programming). crux have simple functions take few inputs, , generate 1 output object.

without more specifics you, hard provide more detailed advice.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -