Skip to content
Prev 268769 / 398502 Next

Utilizing column names to multiply over all columns

On Aug 16, 2011, at 3:37 PM, Sam Albers wrote:

            
> mydf
         2.72      3.2      3.78 yy
1   31.07874 74.48555 89.306591  a
2  123.68290 74.30030 11.943576  b
3   89.64024 68.26378 97.627211  a
4   81.46604 59.79607 91.005217  b
You will get the warning that last last column is not "going right"  
but otherwise this returns what you asked for:

sapply(1:length(mydf), function(i) mydf[[i]]* as.numeric(names(mydf) 
[i])  )
            [,1]     [,2]      [,3] [,4]
  [1,]  84.53416 238.3538 337.57891   NA
  [2,] 336.41748 237.7610  45.14672   NA
  [3,] 243.82145 218.4441 369.03086   NA
  [4,] 221.58762 191.3474 343.99972   NA
  [5,]  81.78911 213.0770  97.90072   NA
snipped remainder