Message-ID: <CABdHhvG2OqpMXQETq4T7=YYbUNrnCoGggHVsh+fyJRSzoKn-sA@mail.gmail.com>
Date: 2011-08-16T21:12:38Z
From: Hadley Wickham
Subject: Utilizing column names to multiply over all columns
In-Reply-To: <CADkXsV2qQ9fHJHvh0M2B-KJGqMekpwH6Cc66ARbnTQOY1eOBXA@mail.gmail.com>
>> 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]) ?)
>
> This suits my purposes well with a couple slight modifications:
>
> ## I made this into a data.frame so I could append it to the other one (mydf)
> mydf.vd <- as.data.frame(sapply(1:length(mydf), function(i)
> mydf[[i]]*as.numeric(names(mydf)[i]) ))
You can simplify this a little with the sweep function:
sweep(mydf[, 1:3], 2, as.numeric(names(mydf)[1:3]), "*")
Hadley
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/