[R] building transformation macro

David Winsemius dwinsemius at comcast.net
Thu May 24 15:06:07 CEST 2012


On May 24, 2012, at 4:43 AM, davv.vikas wrote:

> Hi All,
>
> I am writing a macro which perform following functionality
> 1) take a file say excel or csv
> 2)read all the labels
> 3) dynamically genrate square,square-root,log  of each column with
> appropiate column name and write it to a new file,
>
> i am stuck at dynamically genrateing the column names like  
> Revenue_square
> etc.
>
> Exmaple " i have a excel with 3 columns Revenue, cost & profit
> Now my Macro should be able to  read the values and for each coulmn  
> and
> perform square,square root and log of   Revenue, cost & profit and  
> write to
> excel with column names as Revenue_square,Revenue_squareroot,  
> Revenue_log
> cost_square etc...
>
> Below is my code
>
> test$Rev_square = test[c(1)]^2
>
I would use test[[1]]^2. Generally one wants to pull the vector out of  
the list "container".
>
> data=read.delim2("//ARLMSAN01/CTRX_Data/vikasK.sharma/Desktop/ 
> balancesheet_example.csv",header=T,sep=",")
>
> headings = names(data)
> show (headings)
> HEADINGS = toupper(headings)
> # Perhaps replacing your for-loop body with this
for ( i in 1:length(HEADINGS))
> {
>
assign(paste(HEADINGS[i],  "Rev_Sqr", sep=""), data[[i]]^2)
>
> }

The generalization to additional transformations seems obvious.

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list