Skip to content

How to multiple the vector and variables from dataframe

17 messages · Andrius Druzinis, Berend Hasselman, Neal H. Walfield +2 more

#
At Sun, 30 Dec 2012 18:26:45 +0800 (CST),
meng wrote:
You could convert the data frame to a matrix:
x1 x2
1  1 11
2  2 12
3  3 13
x1 x2
[1,]  3 22
[2,]  4 36
[3,]  9 26

Neal
#
On 30-12-2012, at 11:26, meng <laomeng_3 at 163.com> wrote:

            
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
#
At Sun, 30 Dec 2012 16:28:44 +0000,
Andrius Druzinis wrote:
I think you mean multiplied by element.  

Here's a better solution using t to transpose the matrix:

dat=data.frame(x1=1:3, x2=11:13)
 as.matrix(dat)
     x1 x2
[1,]  1 11
[2,]  2 12
[3,]  3 13
t(as.matrix(dat)) * c(2, 3)
   [,1] [,2] [,3]
x1    2    4    6
x2   33   36   39
#
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
[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.
#
HI Meng,

NO problem.
#In fact, 
sweep(dat,2,z,"*") #will be data.frame
?# x1? x2?? x3
#1? 2 120 2500
#2? 5 200 5000
#3? 8 300 6200
?str(sweep(dat,2,z,"*"))
#'data.frame':??? 3 obs. of? 3 variables:
# $ x1: num? 2 5 8
# $ x2: num? 120 200 300
# $ x3: num? 2500 5000 6200
A.K.
#
HI Meng,


Just try:
rep(z,rach=nrow(dat))
#[1]?? 0.1? 10.0 100.0
?rep(z,chair=nrow(dat))
#[1]?? 0.1? 10.0 100.0
rep(z,times=nrow(dat))
#[1]?? 0.1? 10.0 100.0?? 0.1? 10.0 100.0?? 0.1? 10.0 100.0
?rep(z,each=nrow(dat))
#[1]?? 0.1?? 0.1?? 0.1? 10.0? 10.0? 10.0 100.0 100.0 100.0
?rep(z,nrow(dat))
#[1]?? 0.1? 10.0 100.0?? 0.1? 10.0 100.0?? 0.1? 10.0 100.0

#and
dat*rep(z,times=nrow(dat))
?# ?? x1??? x2??? x3
#1?? 0.1?? 0.4?? 0.7
#2? 20.0? 50.0? 80.0
#3 300.0 600.0 900.0
?dat*rep(z,nrow(dat))
#???? x1??? x2??? x3
#1?? 0.1?? 0.4?? 0.7
#2? 20.0? 50.0? 80.0
#3 300.0 600.0 900.0
dat*rep(z,each=nrow(dat))
#?? x1 x2? x3
#1 0.1 40 700
#2 0.2 50 800
#3 0.3 60 900

A.K.




----- Original Message -----
From: meng <laomeng_3 at 163.com>
To: Andrius Druzinis <andrius.druzinis at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Sunday, December 30, 2012 9:24 PM
Subject: Re: [R] How to multiple the vector and variables from dataframe

Hi Andrius:
Thanks for your reply.
Your answer: dat*rep(z,each=nrow(dat)) works well.

But a strange thing happened:
dat<-data.frame(x1=1:3,x2=4:6,x3=7:9)
z<-c(0.1,10,100)
#I wanna 0.1*x1,10*x2,100*x3

I type:
dat*rep(z,rach=nrow(dat))
"rach" is "each" indeed,but I type "rach" mistakenly.
What's strange to me is :No error reply appears,but show me the result:
? ?  x1? ? x2? ? x3
1?  0.1?  0.4?  0.7
2? 20.0? 50.0? 80.0
3 300.0 600.0 900.0
Why does it happen,and what "rach" means?

Many thanks.
At 2012-12-31 00:08:26,"Andrius Druzinis" <andrius.druzinis at gmail.com> wrote:
Hi Meng, 


A one-liner would be
dat*rep(z, each=nrow(dat))


Cheers, 
Andrius



2012/12/30 meng <laomeng_3 at 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
[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.



??? [[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.
#
On 31-12-2012, at 03:24, meng <laomeng_3 at 163.com> wrote:

            
If you read the help for rep() with for example ?rep you will see that rep() has a ? argument which caters for "further arguments that are passed to or from other methods". rach is regarded as part of ? and is therefore not treated as an error. You could also use ZZZ=nrow(dat).

Berend