I've just started working with R to do my data manipulation and analysis after years of using IgorPro, which no one in their right mind would spend as much time writing scripts in as I have. There's clearly a conceptual disconnect between the two that's causing me trouble, though.
I want to write a function that will take whatever column in a dataframe I feed it and scale it from 0 to 1. The critical thing here is that I want the rescaled data to wind up IN the dataframe. In my IgorPro frame of mind, this is easy:
normalize<-function(col){
col<-col/min(col)
}
If I put in testdf$testcol
, and print the result, this has worked, but the results are not incorporated into the dataframe. A little research suggests that this is because my function exists in a local environment, and in order to modify things outside the local environment, it needs to be connected to the global environment.
Modified:
normalize<-function(col){
col<-col/min(col)
assign("col",col,envir=.GlobalEnv)
}
But of course this just spits out a new vector named col
and doesn't help me in my endeavor to overwrite the non-scaled data.
Short of reassigning the column name to the rescaled data, which defeats the point of writing a function to do this, how can I use the arguments in the function to assign the function output to actual dataframes?
Final note: I appreciate any input that involves using packages that would do this for me, but I have a lot more data manipulation to do, and I'd like to be able to write my own functions rather than having to find packages for everything, so bonus points if you can help me understand how to write the function myself rather than pointing me to built-in functions elsewhere.