hi all: Here's a dataframe(dat) and a vector(z): dat: x1 x2 x3 0.2 1.2 2.5 0.5 2 5 0.8 3 6.2> z[1] 10 100 100 I wanna do the following: 10*x1,100*x2,1000*x3 My solution is using the loop for z and dat(since the length of z is the same as ncol of dat),which is tedious. I wanna an efficient solution to do it . Any help? Many thanks! My best [[alternative HTML version deleted]]
Andrius Druzinis
2012-Dec-30 16:08 UTC
[R] How to multiple the vector and variables from dataframe
Hi Meng, A one-liner would be dat*rep(z, each=nrow(dat)) Cheers, Andrius 2012/12/30 meng <laomeng_3@163.com>> hi all: > Here's a dataframe(dat) and a vector(z): > > dat: > x1 x2 x3 > 0.2 1.2 2.5 > 0.5 2 5 > 0.8 3 6.2 > > > z > [1] 10 100 100 > > I wanna do the following: > 10*x1,100*x2,1000*x3 > > My solution is using the loop for z and dat(since the length of z is the > same as ncol of dat),which is tedious. > I wanna an efficient solution to do it . > > Any help? > > Many thanks! > > My best > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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]]
Neal H. Walfield
2012-Dec-30 16:13 UTC
[R] How to multiple the vector and variables from dataframe
At Sun, 30 Dec 2012 18:26:45 +0800 (CST), meng wrote:> > hi all: > Here's a dataframe(dat) and a vector(z): > > dat: > x1 x2 x3 > 0.2 1.2 2.5 > 0.5 2 5 > 0.8 3 6.2 > > > z > [1] 10 100 100 > > I wanna do the following: > 10*x1,100*x2,1000*x3 > > My solution is using the loop for z and dat(since the length of z is the same as ncol of dat),which is tedious. > I wanna an efficient solution to do it .You could convert the data frame to a matrix:> dat=data.frame(x1=1:3, x2=11:13) > datx1 x2 1 1 11 2 2 12 3 3 13> as.matrix(dat) * c(3, 2)x1 x2 [1,] 3 22 [2,] 4 36 [3,] 9 26 Neal
Berend Hasselman
2012-Dec-30 16:31 UTC
[R] How to multiple the vector and variables from dataframe
On 30-12-2012, at 11:26, meng <laomeng_3 at 163.com> wrote:> hi all: > Here's a dataframe(dat) and a vector(z): > > dat: > x1 x2 x3 > 0.2 1.2 2.5 > 0.5 2 5 > 0.8 3 6.2 > >> z > [1] 10 100 100 > > I wanna do the following: > 10*x1,100*x2,1000*x3 > > My solution is using the loop for z and dat(since the length of z is the same as ncol of dat),which is tedious. > I wanna an efficient solution to do it .Data: xdat <- data.frame(x1=c(.2,.5,.8),x2=c(1.2,2,3),x3=c(2.5,5,6.2)) z <- c(10,100,1000) Assuming you want a dataframe as result you can do one of the following Option 1: ------------ as.data.frame(sapply(1:length(z),function(k)xdat[k]*z[k])) Option 2: ------------ as.data.frame(as.matrix(xdat)*rep(z,each=length(z))) Option 3: ------------ as.data.frame(t(t(as.matrix(xdat))*z)) Option 4: ------------ as.data.frame(as.matrix(xdat)%*%diag(z)) The suggested solution as.matrix(xdat)*z results in x1 x2 x3 [1,] 2 12 25 [2,] 50 200 500 [3,] 800 3000 6200 and that doesn't seem to be what you want. Berend
HI, Its not clear esp " I wanna do the following: 10*x1,100*x2,1000*x3" Did you mean 10* dat[,1], 100*dat[,2], 1000*dat[,3]? dat<-read.table(text=" x1??? x2??? x3 0.2? 1.2? 2.5 0.5? 2????? 5 0.8? 3????? 6.2 ",sep="",header=TRUE) z<-c(10,100,1000) # 3rd element in your z is 100, which is confusing. ?t(t(dat)*z) #??? x1? x2?? x3 #[1,]? 2 120 2500 #[2,]? 5 200 5000 #[3,]? 8 300 6200 A.K. ----- Original Message ----- From: meng <laomeng_3 at 163.com> To: R help <r-help at r-project.org> Cc: Sent: Sunday, December 30, 2012 5:26 AM Subject: [R] How to multiple the vector and variables from dataframe hi all: Here's a dataframe(dat) and a vector(z): dat: x1? ? x2? ? x3 0.2? 1.2? 2.5 0.5? 2? ? ? 5 0.8? 3? ? ? 6.2> z[1]? 10 100 100 I wanna do the following: 10*x1,100*x2,1000*x3 My solution is using the loop for z and dat(since the length of z is the same as ncol? of dat),which is tedious. I wanna an efficient solution to do it . Any help? Many thanks! My best ??? [[alternative HTML version deleted]] ______________________________________________ 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.