Tribo Laboy
2008-Mar-22 14:21 UTC
[R] More elegant multiplication or division of a data frame with a vector
Hello, I am importing some raw voltage multichannel measurements into an R data frame. I need to scale each column with the respective sensitivity for that channel. I figured how to do it, but I am curious if there isn't a more elegant way. Now I start with something like this: rawdata <- data.frame(rbind(c(1,2,3), c(4,5,6))) sens <- c(2,4,6) and I do this: data <- as.data.frame(t(t(rawdata)/sens)) Any suggestions? Cheers, TL
Gabor Grothendieck
2008-Mar-22 14:59 UTC
[R] More elegant multiplication or division of a data frame with a vector
Not sure if these are any better but here are a few alternatives:
rawdata / rep(sens, each = nrow(rawdata))
as.data.frame(mapply("/", rawdata, sens))
as.data.frame(as.matrix(rawdata) %*% diag(1/sens))
On Sat, Mar 22, 2008 at 10:21 AM, Tribo Laboy <tribolaboy at gmail.com>
wrote:> Hello,
>
> I am importing some raw voltage multichannel measurements into an R
> data frame. I need to scale each column with the respective
> sensitivity for that channel. I figured how to do it, but I am curious
> if there isn't a more elegant way.
>
> Now I start with something like this:
>
> rawdata <- data.frame(rbind(c(1,2,3), c(4,5,6)))
> sens <- c(2,4,6)
>
>
> and I do this:
> data <- as.data.frame(t(t(rawdata)/sens))
>
> Any suggestions?
>
> Cheers,
>
> TL
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Greg Snow
2008-Mar-22 15:06 UTC
[R] More elegant multiplication or division of a data frame with avector
> sweep( rawdata, 2, sens, '/' )hope this helps, ________________________________ From: r-help-bounces@r-project.org on behalf of Tribo Laboy Sent: Sat 3/22/2008 8:21 AM To: r-help@r-project.org Subject: [R] More elegant multiplication or division of a data frame with avector Hello, I am importing some raw voltage multichannel measurements into an R data frame. I need to scale each column with the respective sensitivity for that channel. I figured how to do it, but I am curious if there isn't a more elegant way. Now I start with something like this: rawdata <- data.frame(rbind(c(1,2,3), c(4,5,6))) sens <- c(2,4,6) and I do this: data <- as.data.frame(t(t(rawdata)/sens)) Any suggestions? Cheers, TL ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]