Re: [R] Multiply

2023-08-04 Thread avi.e.gross
[See the end for an interesting twist on moving a column to row.names.]

Yes, many ways to do things exist but it may make sense to ask for what the 
user/OP really wants. Sometimes the effort to make a brief example obscures 
things.

Was there actually any need to read in a file containing comma-separated 
values? Did it have to include one, or perhaps more, non-numeric columns? Was 
the ID column guaranteed to exist and be named ID or just be the first column? 
Is any error checking needed?

So assuming we read in two data structures into data.frames called A and B to 
be unoriginal. A wider approach might be to split A into A.text and A.numeric 
by checking which columns test as numeric (meaning is.numeric() return TRUE) 
and which can be text or logical or anything else. Note that complex is not 
considered numeric if that matters.

You might then count the number of rows and columns of A.numeric and set A.text 
Aside for now. 

You then get B and it seems you can throw away any non-numeric columns. The 
resulting numeric columns can be in B.numeric.
The number of rows and columns of what remains need to conform to the 
dimensions of A in the sense that if a is M rows by N columns, then B must be N 
x anything and the result of the multiplication is M x anything. If the 
condition is not met, you need to fail gracefully.

You may also want to decide what to do with data that came with things like NA 
content. Or, if your design allows content that can be converted to numeric, 
check and make any conversions.

Then you can convert the data into matrices, perform the matrix multiplication 
and optionally restore any column names you want along with any of the 
non-numeric columns you held back and note there could possible be more than 
one. Obviously, getting multiple ones in the original order is harder.

I am not sure if you are interested in another tweak. For some purposes, 
rownames() and colnames() make sense instead of additional rows or columns.

A line of code like this applied to a data.frame will copy your id column as a 
rowname then remove the actual ID column.

> dat1
  ID  x  y  z
1  A 10 34 12
2  B 25 42 18
3  C 14 20  8
> rownames(dat1) <- dat1$ID
> dat1$ID <- NULL
> dat1
x  y  z
 A 10 34 12
 B 25 42 18
 C 14 20  8

> result <- as.matrix(dat1) %*% mat2
> result
   weight weiht2
 A  24.58  30.18
 B  35.59  44.09
 C  17.10  21.30

There are functions (perhaps in packages, with names like column_to_rownames() 
in the tidyverse packages that can be used and you can also reverse the process.

Just some thoughts. The point is that it is often wiser to not mix text with 
numeric and rownames and colnames provide a way to include the text for the 
purposes you want and not for others where they are in the way. And here is an 
oddity I found:

> dat2
  ID weight weiht2
1  A   0.25   0.35
2  B   0.42   0.52
3  C   0.65   0.75
> temp <- data.frame(dat2, row.names=1)
> temp
   weight weiht2
 A   0.25   0.35
 B   0.42   0.52
 C   0.65   0.75

As shown, when you create a data.frame you can move any column by NUMBER into 
rownames. So consider your early code and note read.table supports the option 
row.names=1 and passes it on so in one step:

> ?read.table
> dat1 <-read.table(text="ID, x, y, z
+ A, 10,  34, 12
+ B, 25,  42, 18
+ C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F, row.names=1)
> dat1
   x  y  z
A 10 34 12
B 25 42 18
C 14 20  8

You can make it a matrix immediately:

mat1 <- as.matrix(read.table(
  text = text,
  sep = ",",
  header = TRUE,
  stringsAsFactors = F,
  row.names = 1
))


-Original Message-
From: Val  
Sent: Friday, August 4, 2023 2:03 PM
To: avi.e.gr...@gmail.com
Cc: r-help@r-project.org
Subject: Re: [R] Multiply

Thank you,  Avi and Ivan.  Worked for this particular Example.

Yes, I am looking for something with a more general purpose.
I think Ivan's suggestion works for this.

multiplication=as.matrix(dat1[,-1]) %*% as.matrix(dat2[match(dat1[,1],
dat2[,1]),-1])
Res=data.frame(ID = dat1[,1], Index = multiplication)

On Fri, Aug 4, 2023 at 10:59 AM  wrote:
>
> Val,
>
> A data.frame is not quite the same thing as a matrix.
>
> But as long as everything is numeric, you can convert both data.frames to
> matrices, perform the computations needed and, if you want, convert it back
> into a data.frame.
>
> BUT it must be all numeric and you violate that requirement by having a
> character column for ID. You need to eliminate that temporarily:
>
> dat1 <- read.table(text="ID, x, y, z
>  A, 10,  34, 12
>  B, 25,  42, 18
>  C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
>
> mat1 <- as.matrix(dat1[,2:4])
>
> The result is:
>
> > mat1
>   x  y  z
> [1,] 10 34 12
> [2,] 25 42 18
> [3,] 14 20  8
>
> Now do the second matrix, perhaps in one step:
>
> m

Re: [R] Multiply

2023-08-04 Thread Rui Barradas

Às 19:03 de 04/08/2023, Val escreveu:

Thank you,  Avi and Ivan.  Worked for this particular Example.

Yes, I am looking for something with a more general purpose.
I think Ivan's suggestion works for this.

multiplication=as.matrix(dat1[,-1]) %*% as.matrix(dat2[match(dat1[,1],
dat2[,1]),-1])
Res=data.frame(ID = dat1[,1], Index = multiplication)

On Fri, Aug 4, 2023 at 10:59 AM  wrote:


Val,

A data.frame is not quite the same thing as a matrix.

But as long as everything is numeric, you can convert both data.frames to
matrices, perform the computations needed and, if you want, convert it back
into a data.frame.

BUT it must be all numeric and you violate that requirement by having a
character column for ID. You need to eliminate that temporarily:

dat1 <- read.table(text="ID, x, y, z
  A, 10,  34, 12
  B, 25,  42, 18
  C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)

mat1 <- as.matrix(dat1[,2:4])

The result is:


mat1

   x  y  z
[1,] 10 34 12
[2,] 25 42 18
[3,] 14 20  8

Now do the second matrix, perhaps in one step:

mat2 <- as.matrix(read.table(text="ID, weight, weiht2
  A,  0.25, 0.35
  B,  0.42, 0.52
  C,  0.65, 0.75",sep=",",header=TRUE,stringsAsFactors=F)[,2:3])


Do note some people use read.csv() instead of read.table, albeit it simply
calls read.table after setting some parameters like the comma.

The result is what you asked for, including spelling weight wrong once.:


mat2

  weight weiht2
[1,]   0.25   0.35
[2,]   0.42   0.52
[3,]   0.65   0.75

Now you wanted to multiply as in matrix multiplication.


mat1 %*% mat2

  weight weiht2
[1,]  24.58  30.18
[2,]  35.59  44.09
[3,]  17.10  21.30

Of course, you wanted different names for the columns and you can do that
easily enough:

result <- mat1 %*% mat2

colnames(result) <- c("index1", "index2")


But this is missing something:


result

  index1 index2
[1,]  24.58  30.18
[2,]  35.59  44.09
[3,]  17.10  21.30

Do you want a column of ID numbers on the left? If numeric, you can keep it
in a matrix in one of many ways but if you want to go back to the data.frame
format and re-use the ID numbers, there are again MANY ways. But note mixing
characters and numbers can inadvertently convert everything to characters.

Here is one solution. Not the only one nor the best one but reasonable:

recombined <- data.frame(index=dat1$ID,
  index1=result[,1],
  index2=result[,2])



recombined

   index index1 index2
1 A  24.58  30.18
2 B  35.59  44.09
3 C  17.10  21.30

If for some reason you need a more general purpose way to do this for
arbitrary conformant matrices, you can write a function that does this in a
more general way but perhaps a better idea might be a way to store your
matrices in files in a way that can be read back in directly or to not
include indices as character columns but as row names.






-Original Message-
From: R-help  On Behalf Of Val
Sent: Friday, August 4, 2023 10:54 AM
To: r-help@R-project.org (r-help@r-project.org) 
Subject: [R] Multiply

Hi all,

I want to multiply two  data frames as shown below,

dat1 <-read.table(text="ID, x, y, z
  A, 10,  34, 12
  B, 25,  42, 18
  C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)

dat2 <-read.table(text="ID, weight, weiht2
  A,  0.25, 0.35
  B,  0.42, 0.52
  C,  0.65, 0.75",sep=",",header=TRUE,stringsAsFactors=F)

Desired result

ID  Index1 Index2
1  A 24.58 30.18
2  B 35.59 44.09
3  C 17.10 21.30

Here is my attempt,  but did not work

dat3 <- data.frame(ID = dat1[,1], Index = apply(dat1[,-1], 1, FUN=
function(x) {sum(x*dat2[,2:ncol(dat2)])} ), stringsAsFactors=F)


Any help?

Thank you,

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Hello,

Slightly simpler:



multiplication <- as.matrix(dat1[,-1]) %*% 
as.matrix(dat2[match(dat1[,1], dat2[,1]),-1])

Res <- data.frame(ID = dat1[,1], Index = multiplication)

# this is what I find simpler
# the method being called is cbind.data.frame
Res2 <- cbind(dat1[1], Index = multiplication)

identical(Res, Res2)
#> [1] TRUE


Hope this helps,

Rui Barradas

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Multiply

2023-08-04 Thread Val
Thank you,  Avi and Ivan.  Worked for this particular Example.

Yes, I am looking for something with a more general purpose.
I think Ivan's suggestion works for this.

multiplication=as.matrix(dat1[,-1]) %*% as.matrix(dat2[match(dat1[,1],
dat2[,1]),-1])
Res=data.frame(ID = dat1[,1], Index = multiplication)

On Fri, Aug 4, 2023 at 10:59 AM  wrote:
>
> Val,
>
> A data.frame is not quite the same thing as a matrix.
>
> But as long as everything is numeric, you can convert both data.frames to
> matrices, perform the computations needed and, if you want, convert it back
> into a data.frame.
>
> BUT it must be all numeric and you violate that requirement by having a
> character column for ID. You need to eliminate that temporarily:
>
> dat1 <- read.table(text="ID, x, y, z
>  A, 10,  34, 12
>  B, 25,  42, 18
>  C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
>
> mat1 <- as.matrix(dat1[,2:4])
>
> The result is:
>
> > mat1
>   x  y  z
> [1,] 10 34 12
> [2,] 25 42 18
> [3,] 14 20  8
>
> Now do the second matrix, perhaps in one step:
>
> mat2 <- as.matrix(read.table(text="ID, weight, weiht2
>  A,  0.25, 0.35
>  B,  0.42, 0.52
>  C,  0.65, 0.75",sep=",",header=TRUE,stringsAsFactors=F)[,2:3])
>
>
> Do note some people use read.csv() instead of read.table, albeit it simply
> calls read.table after setting some parameters like the comma.
>
> The result is what you asked for, including spelling weight wrong once.:
>
> > mat2
>  weight weiht2
> [1,]   0.25   0.35
> [2,]   0.42   0.52
> [3,]   0.65   0.75
>
> Now you wanted to multiply as in matrix multiplication.
>
> > mat1 %*% mat2
>  weight weiht2
> [1,]  24.58  30.18
> [2,]  35.59  44.09
> [3,]  17.10  21.30
>
> Of course, you wanted different names for the columns and you can do that
> easily enough:
>
> result <- mat1 %*% mat2
>
> colnames(result) <- c("index1", "index2")
>
>
> But this is missing something:
>
> > result
>  index1 index2
> [1,]  24.58  30.18
> [2,]  35.59  44.09
> [3,]  17.10  21.30
>
> Do you want a column of ID numbers on the left? If numeric, you can keep it
> in a matrix in one of many ways but if you want to go back to the data.frame
> format and re-use the ID numbers, there are again MANY ways. But note mixing
> characters and numbers can inadvertently convert everything to characters.
>
> Here is one solution. Not the only one nor the best one but reasonable:
>
> recombined <- data.frame(index=dat1$ID,
>  index1=result[,1],
>  index2=result[,2])
>
>
> > recombined
>   index index1 index2
> 1 A  24.58  30.18
> 2 B  35.59  44.09
> 3 C  17.10  21.30
>
> If for some reason you need a more general purpose way to do this for
> arbitrary conformant matrices, you can write a function that does this in a
> more general way but perhaps a better idea might be a way to store your
> matrices in files in a way that can be read back in directly or to not
> include indices as character columns but as row names.
>
>
>
>
>
>
> -Original Message-
> From: R-help  On Behalf Of Val
> Sent: Friday, August 4, 2023 10:54 AM
> To: r-help@R-project.org (r-help@r-project.org) 
> Subject: [R] Multiply
>
> Hi all,
>
> I want to multiply two  data frames as shown below,
>
> dat1 <-read.table(text="ID, x, y, z
>  A, 10,  34, 12
>  B, 25,  42, 18
>  C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
>
> dat2 <-read.table(text="ID, weight, weiht2
>  A,  0.25, 0.35
>  B,  0.42, 0.52
>  C,  0.65, 0.75",sep=",",header=TRUE,stringsAsFactors=F)
>
> Desired result
>
> ID  Index1 Index2
> 1  A 24.58 30.18
> 2  B 35.59 44.09
> 3  C 17.10 21.30
>
> Here is my attempt,  but did not work
>
> dat3 <- data.frame(ID = dat1[,1], Index = apply(dat1[,-1], 1, FUN=
> function(x) {sum(x*dat2[,2:ncol(dat2)])} ), stringsAsFactors=F)
>
>
> Any help?
>
> Thank you,
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Multiply

2023-08-04 Thread avi.e.gross
Val,

A data.frame is not quite the same thing as a matrix.

But as long as everything is numeric, you can convert both data.frames to
matrices, perform the computations needed and, if you want, convert it back
into a data.frame.

BUT it must be all numeric and you violate that requirement by having a
character column for ID. You need to eliminate that temporarily:

dat1 <- read.table(text="ID, x, y, z
 A, 10,  34, 12
 B, 25,  42, 18
 C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)

mat1 <- as.matrix(dat1[,2:4])

The result is:

> mat1
  x  y  z
[1,] 10 34 12
[2,] 25 42 18
[3,] 14 20  8

Now do the second matrix, perhaps in one step:

mat2 <- as.matrix(read.table(text="ID, weight, weiht2
 A,  0.25, 0.35
 B,  0.42, 0.52
 C,  0.65, 0.75",sep=",",header=TRUE,stringsAsFactors=F)[,2:3])


Do note some people use read.csv() instead of read.table, albeit it simply
calls read.table after setting some parameters like the comma.

The result is what you asked for, including spelling weight wrong once.:

> mat2
 weight weiht2
[1,]   0.25   0.35
[2,]   0.42   0.52
[3,]   0.65   0.75

Now you wanted to multiply as in matrix multiplication.

> mat1 %*% mat2
 weight weiht2
[1,]  24.58  30.18
[2,]  35.59  44.09
[3,]  17.10  21.30

Of course, you wanted different names for the columns and you can do that
easily enough:

result <- mat1 %*% mat2

colnames(result) <- c("index1", "index2")


But this is missing something:

> result
 index1 index2
[1,]  24.58  30.18
[2,]  35.59  44.09
[3,]  17.10  21.30

Do you want a column of ID numbers on the left? If numeric, you can keep it
in a matrix in one of many ways but if you want to go back to the data.frame
format and re-use the ID numbers, there are again MANY ways. But note mixing
characters and numbers can inadvertently convert everything to characters.

Here is one solution. Not the only one nor the best one but reasonable:

recombined <- data.frame(index=dat1$ID, 
 index1=result[,1], 
 index2=result[,2])


> recombined
  index index1 index2
1 A  24.58  30.18
2 B  35.59  44.09
3 C  17.10  21.30

If for some reason you need a more general purpose way to do this for
arbitrary conformant matrices, you can write a function that does this in a
more general way but perhaps a better idea might be a way to store your
matrices in files in a way that can be read back in directly or to not
include indices as character columns but as row names.






-Original Message-
From: R-help  On Behalf Of Val
Sent: Friday, August 4, 2023 10:54 AM
To: r-help@R-project.org (r-help@r-project.org) 
Subject: [R] Multiply

Hi all,

I want to multiply two  data frames as shown below,

dat1 <-read.table(text="ID, x, y, z
 A, 10,  34, 12
 B, 25,  42, 18
 C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)

dat2 <-read.table(text="ID, weight, weiht2
 A,  0.25, 0.35
 B,  0.42, 0.52
 C,  0.65, 0.75",sep=",",header=TRUE,stringsAsFactors=F)

Desired result

ID  Index1 Index2
1  A 24.58 30.18
2  B 35.59 44.09
3  C 17.10 21.30

Here is my attempt,  but did not work

dat3 <- data.frame(ID = dat1[,1], Index = apply(dat1[,-1], 1, FUN=
function(x) {sum(x*dat2[,2:ncol(dat2)])} ), stringsAsFactors=F)


Any help?

Thank you,

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Multiply

2023-08-04 Thread Ivan Krylov
В Fri, 4 Aug 2023 09:54:07 -0500
Val  пишет:

> I want to multiply two  data frames as shown below,
> 
> dat1 <-read.table(text="ID, x, y, z
>  A, 10,  34, 12
>  B, 25,  42, 18
>  C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)
> 
> dat2 <-read.table(text="ID, weight, weiht2
>  A,  0.25, 0.35
>  B,  0.42, 0.52
>  C,  0.65, 0.75",sep=",",header=TRUE,stringsAsFactors=F)
> 
> Desired result
> 
> ID  Index1 Index2
> 1  A 24.58 30.18
> 2  B 35.59 44.09
> 3  C 17.10 21.30

You mean as in matrix multiplication? Nice of you to show the desired
result, otherwise I wouldn't be sure what you meant. You can employ the
matrix multiplication operator %*% if you use as.matrix and omit the
non-numeric parts:

as.matrix(dat1[,-1]) %*% as.matrix(dat2[match(dat1[,1], dat2[,1]),-1])
#  weight weiht2
# [1,]  24.58  30.18
# [2,]  35.59  44.09
# [3,]  17.10  21.30

The [,-1] part drops the 'ID' column and the match() on the first
columns of both data frames ensures the correct row order. Omit the
match(...) if you have a guarantee that the samples appear in the same
order (A, B, C) in both dat1 and dat2.

Once the calculation is done, you can construct a data frame again
using something like data.frame(ID = dat1[,1], Index = multiplication).

-- 
Best regards,
Ivan

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


[R] Multiply

2023-08-04 Thread Val
Hi all,

I want to multiply two  data frames as shown below,

dat1 <-read.table(text="ID, x, y, z
 A, 10,  34, 12
 B, 25,  42, 18
 C, 14,  20,  8 ",sep=",",header=TRUE,stringsAsFactors=F)

dat2 <-read.table(text="ID, weight, weiht2
 A,  0.25, 0.35
 B,  0.42, 0.52
 C,  0.65, 0.75",sep=",",header=TRUE,stringsAsFactors=F)

Desired result

ID  Index1 Index2
1  A 24.58 30.18
2  B 35.59 44.09
3  C 17.10 21.30

Here is my attempt,  but did not work

dat3 <- data.frame(ID = dat1[,1], Index = apply(dat1[,-1], 1, FUN=
function(x) {sum(x*dat2[,2:ncol(dat2)])} ), stringsAsFactors=F)


Any help?

Thank you,

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Multiply row of a matrix

2017-02-21 Thread Jeff Newmiller
Why this works does not become clear until you actually pay attention to how 
matrices are laid out in memory as a vector, and how vector replication works.  
Those ideas are not that difficult to learn, but they feel different than in 
other languages (e.g. matlab) and they make a huge difference when you want to 
speed things up. Read the appropriate section in the Introduction to R document 
that comes with R until you see what is happening, and ask for clarification if 
you are still lost. 
-- 
Sent from my phone. Please excuse my brevity.

On February 21, 2017 9:00:59 AM PST, William Dunlap via R-help 
 wrote:
>> Mat * c(3, 1, 0.5)
> [,1] [,2] [,3]
>[1,]  3.06  9.0
>[2,]  4.05  6.0
>[3,]  3.54  4.5
>
>
>Bill Dunlap
>TIBCO Software
>wdunlap tibco.com
>
>
>On Tue, Feb 21, 2017 at 8:23 AM,   wrote:
>> If we have the following matrix:
>>
>> Mat<-matrix(1:9, byrow=TRUE, nrow=3)
>> Mat
>>
>>  [,1] [,2] [,3]
>> [1,]123
>> [2,]456
>> [3,]789
>>
>>  I would like to have each row multiplied by a different number.
>> So I would like row 1 multiplied by 3, row2 by 1 and row3 by 0.5
>> Which would give:
>>
>>  [,1] [,2] [,3]
>> [1,]369
>> [2,]456
>> [3,]3.5  44.5
>>
>>
>> Whatever I try, I always obtain
>>  [,1] [,2] [,3]
>> [1,]32  1.5
>> [2,]   125  3.0
>> [3,]   218  4.5
>>
>> Which corresponds to the columns multiplied by the factors
>>
>> Could anyone help we to get the proper result ?
>> Best, Anne-Christine da Silva
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Multiply row of a matrix

2017-02-21 Thread William Dunlap via R-help
> Mat * c(3, 1, 0.5)
 [,1] [,2] [,3]
[1,]  3.06  9.0
[2,]  4.05  6.0
[3,]  3.54  4.5


Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Tue, Feb 21, 2017 at 8:23 AM,   wrote:
> If we have the following matrix:
>
> Mat<-matrix(1:9, byrow=TRUE, nrow=3)
> Mat
>
>  [,1] [,2] [,3]
> [1,]123
> [2,]456
> [3,]789
>
>  I would like to have each row multiplied by a different number.
> So I would like row 1 multiplied by 3, row2 by 1 and row3 by 0.5
> Which would give:
>
>  [,1] [,2] [,3]
> [1,]369
> [2,]456
> [3,]3.5  44.5
>
>
> Whatever I try, I always obtain
>  [,1] [,2] [,3]
> [1,]32  1.5
> [2,]   125  3.0
> [3,]   218  4.5
>
> Which corresponds to the columns multiplied by the factors
>
> Could anyone help we to get the proper result ?
> Best, Anne-Christine da Silva
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


[R] Multiply row of a matrix

2017-02-21 Thread ac . dasilva
If we have the following matrix: 

Mat<-matrix(1:9, byrow=TRUE, nrow=3)
Mat

 [,1] [,2] [,3]
[1,]123
[2,]456
[3,]789

 I would like to have each row multiplied by a different number. 
So I would like row 1 multiplied by 3, row2 by 1 and row3 by 0.5
Which would give: 

 [,1] [,2] [,3]
[1,]369
[2,]456
[3,]3.5  44.5


Whatever I try, I always obtain 
 [,1] [,2] [,3]
[1,]32  1.5
[2,]   125  3.0
[3,]   218  4.5

Which corresponds to the columns multiplied by the factors 

Could anyone help we to get the proper result ?
Best, Anne-Christine da Silva

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] multiply of two expressions

2014-05-06 Thread Ben Bolker
Niloofar.Javanrouh javanrouh_n at yahoo.com writes:

 
 
  hello,
 i want to differentiate of L with respect to b
 when:
 
 L= k*ln (k/(k+mu)) + sum(y) * ln (1-(k/mu+k))   
#(negative binomial ln likelihood)
 and 
 ln(mu/(mu+k)) = a+bx   #link function
 
 how can i do it in R?
 thank you.
 

  R has a couple of functions for differentiation, D() and deriv(),
but this is probably a case where it would actually be simpler to
do it yourself by hand.  If you insist on doing it in R, you can
either use the chain rule (i.e. compute d(mu)/d(b)*d(L)/d(mu)) or
you can make a big ugly expression where you substitute the expression
for mu(b) into the expression for L.  In either case you'll have to
solve your link expression for mu.

  Wolfram Alpha might help too.

  good luck
Ben Bolker

__
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.


[R] multiply of two expressions

2014-05-05 Thread Niloofar.Javanrouh


 hello,
i want to differentiate of L with respect to b
when:

L= k*ln (k/(k+mu)) + sum(y) * ln (1-(k/mu+k))   #(negative binomial ln 
likelihood)
and 
ln(mu/(mu+k)) = a+bx   #link function

how can i do it in R?
thank you.


_ 
Best Regards 

Niloofar.Javanrouh 
Ph.D Student of BioStatistics 

[[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.


Re: [R] multiply of two expressions

2014-05-05 Thread Gabor Grothendieck
On Mon, May 5, 2014 at 9:43 AM, Niloofar.Javanrouh
javanrou...@yahoo.com wrote:


  hello,
 i want to differentiate of L with respect to b
 when:

 L= k*ln (k/(k+mu)) + sum(y) * ln (1-(k/mu+k))   #(negative binomial ln 
 likelihood)
 and
 ln(mu/(mu+k)) = a+bx   #link function

 how can i do it in R?

Try this.  First we solve for 'mu' in terms of the other variables
using the link equation:

 library(Ryacas)
 k - Sym(k)
 mu - Sym(mu)
 y - Sym(y)
 L - Sym(L)
 a - Sym(a)
 b - Sym(b)
 x - Sym(x)
 sumy - Sym(sumy)
 Solve(log(mu/(mu+k)) == a+b*x, mu)
expression(list(mu == k * exp(a + b * x)/(1 - exp(a + b * x


Now in 'k*log(k/(k+mu)) + sumy * log(1-(k/mu+k))' substitute 'k *
exp(a + b * x)/(1 - exp(a + b * x)))' for 'mu' using 'Subst' and take
the derivative with respect to 'a' using 'deriv':

 s - Subst(k*log(k/(k+mu)) + sumy * log(1-(k/mu+k)), mu,
+   k * exp(a + b * x)/(1 - exp(a + b * x)))
 deriv(s, a)
expression(sumy * (k * ((1 - exp(a + b * x)) * (k * exp(a + b *
x)) + k * exp(a + b * x)^2))/((1 - exp(a + b * x))^2 * (k *
exp(a + b * x)/(1 - exp(a + b * x)))^2 * (1 - (k * (1 - exp(a +
b * x))/(k * exp(a + b * x)) + k))) - k * ((k + k * exp(a +
b * x)/(1 - exp(a + b * x))) * (k * ((1 - exp(a + b * x)) *
(k * exp(a + b * x)) + k * exp(a + b * x)^2)))/((1 - exp(a +
b * x))^2 * ((k + k * exp(a + b * x)/(1 - exp(a + b * x)))^2 *
k)))

__
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.


Re: [R] multiply of two expressions

2014-05-05 Thread David Winsemius

On May 5, 2014, at 6:05 PM, Gabor Grothendieck wrote:

 On Mon, May 5, 2014 at 9:43 AM, Niloofar.Javanrouh
 javanrou...@yahoo.com wrote:
 
 
 hello,
 i want to differentiate of L with respect to b
 when:
 
 L= k*ln (k/(k+mu)) + sum(y) * ln (1-(k/mu+k))   #(negative binomial ln 
 likelihood)
 and
 ln(mu/(mu+k)) = a+bx   #link function
 
 how can i do it in R?
 
 Try this.  First we solve for 'mu' in terms of the other variables
 using the link equation:
 
 library(Ryacas)
 k - Sym(k)
 mu - Sym(mu)
 y - Sym(y)
 L - Sym(L)
 a - Sym(a)
 b - Sym(b)
 x - Sym(x)
 sumy - Sym(sumy)
 Solve(log(mu/(mu+k)) == a+b*x, mu)
 expression(list(mu == k * exp(a + b * x)/(1 - exp(a + b * x
 
 
 Now in 'k*log(k/(k+mu)) + sumy * log(1-(k/mu+k))' substitute 'k *
 exp(a + b * x)/(1 - exp(a + b * x)))' for 'mu' using 'Subst' and take
 the derivative with respect to 'a' using 'deriv':
 
 s - Subst(k*log(k/(k+mu)) + sumy * log(1-(k/mu+k)), mu,
 +   k * exp(a + b * x)/(1 - exp(a + b * x)))
 deriv(s, a)
 expression(sumy * (k * ((1 - exp(a + b * x)) * (k * exp(a + b *
x)) + k * exp(a + b * x)^2))/((1 - exp(a + b * x))^2 * (k *
exp(a + b * x)/(1 - exp(a + b * x)))^2 * (1 - (k * (1 - exp(a +
b * x))/(k * exp(a + b * x)) + k))) - k * ((k + k * exp(a +
b * x)/(1 - exp(a + b * x))) * (k * ((1 - exp(a + b * x)) *
(k * exp(a + b * x)) + k * exp(a + b * x)^2)))/((1 - exp(a +
b * x))^2 * ((k + k * exp(a + b * x)/(1 - exp(a + b * x)))^2 *
k)))
 

But can we maybe get the Taylor series approximation to first or second order?

-- 

David Winsemius
Alameda, CA, USA

__
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.


Re: [R] multiply of two expressions

2014-05-05 Thread Gabor Grothendieck
On Mon, May 5, 2014 at 10:16 PM, David Winsemius dwinsem...@comcast.net wrote:

 On May 5, 2014, at 6:05 PM, Gabor Grothendieck wrote:

 On Mon, May 5, 2014 at 9:43 AM, Niloofar.Javanrouh
 javanrou...@yahoo.com wrote:


 hello,
 i want to differentiate of L with respect to b
 when:

 L= k*ln (k/(k+mu)) + sum(y) * ln (1-(k/mu+k))   #(negative binomial ln 
 likelihood)
 and
 ln(mu/(mu+k)) = a+bx   #link function

 how can i do it in R?

 Try this.  First we solve for 'mu' in terms of the other variables
 using the link equation:

 library(Ryacas)
 k - Sym(k)
 mu - Sym(mu)
 y - Sym(y)
 L - Sym(L)
 a - Sym(a)
 b - Sym(b)
 x - Sym(x)
 sumy - Sym(sumy)
 Solve(log(mu/(mu+k)) == a+b*x, mu)
 expression(list(mu == k * exp(a + b * x)/(1 - exp(a + b * x


 Now in 'k*log(k/(k+mu)) + sumy * log(1-(k/mu+k))' substitute 'k *
 exp(a + b * x)/(1 - exp(a + b * x)))' for 'mu' using 'Subst' and take
 the derivative with respect to 'a' using 'deriv':

 s - Subst(k*log(k/(k+mu)) + sumy * log(1-(k/mu+k)), mu,
 +   k * exp(a + b * x)/(1 - exp(a + b * x)))
 deriv(s, a)
 expression(sumy * (k * ((1 - exp(a + b * x)) * (k * exp(a + b *
x)) + k * exp(a + b * x)^2))/((1 - exp(a + b * x))^2 * (k *
exp(a + b * x)/(1 - exp(a + b * x)))^2 * (1 - (k * (1 - exp(a +
b * x))/(k * exp(a + b * x)) + k))) - k * ((k + k * exp(a +
b * x)/(1 - exp(a + b * x))) * (k * ((1 - exp(a + b * x)) *
(k * exp(a + b * x)) + k * exp(a + b * x)^2)))/((1 - exp(a +
b * x))^2 * ((k + k * exp(a + b * x)/(1 - exp(a + b * x)))^2 *
k)))


 But can we maybe get the Taylor series approximation to first or second order?




 Taylor(d, a, 0, 2)
expression(sumy * (k * ((1 - exp(b * x)) * (k * exp(b * x)) +
k * exp(b * x)^2))/((1 - exp(b * x))^2 * (k * exp(b * x)/(1 -
exp(b * x)))^2 * (1 - (k * (1 - exp(b * x))/(k * exp(b *
x)) + k))) - k * ((k + k * exp(b * x)/(1 - exp(b * x))) *
(k * ((1 - exp(b * x)) * (k * exp(b * x)) + k * exp(b * x)^2)))/((1 -
exp(b * x))^2 * ((k + k * exp(b * x)/(1 - exp(b * x)))^2 *
k)) + (((1 - exp(b * x))^2 * (k * exp(b * x)/(1 - exp(b *
x)))^2 * (1 - (k * (1 - exp(b * x))/(k * exp(b * x)) + k)) *
(sumy * (k * ((1 - exp(b * x)) * (k * exp(b * x)) - k * exp(b *
x)^2 + k * (2 * exp(b * x)^2 - sumy * (k * ((1 -
exp(b * x)) * (k * exp(b * x)) + k * exp(b * x)^2)) * ((1 -
exp(b * x))^2 * (k * exp(b * x)/(1 - exp(b * x)))^2 * (k *
((1 - exp(b * x)) * (k * exp(b * x)) + k * exp(b * x)^2))/((1 -
exp(b * x))^2 * (k * exp(b * x)/(1 - exp(b * x)))^2) + ((1 -
exp(b * x))^2 * (k * exp(b * x) * (2 * ((1 - exp(b * x)) *
(k * exp(b * x)) + k * exp(b * x)^2)))/(1 - exp(b * x))^3 +
-2 * exp(b * x) * (1 - exp(b * x)) * (k * exp(b * x)/(1 -
exp(b * x)))^2) * (1 - (k * (1 - exp(b * x))/(k * exp(b *
x)) + k/((1 - exp(b * x))^2 * (k * exp(b * x)/(1 - exp(b *
x)))^2 * (1 - (k * (1 - exp(b * x))/(k * exp(b * x)) + k)))^2 -
((1 - exp(b * x))^2 * ((k + k * exp(b * x)/(1 - exp(b * x)))^2 *
k) * (k * ((k + k * exp(b * x)/(1 - exp(b * x))) * (k *
((1 - exp(b * x)) * (k * exp(b * x)) - k * exp(b * x)^2 +
k * (2 * exp(b * x)^2))) + k * ((1 - exp(b * x)) *
(k * exp(b * x)) + k * exp(b * x)^2)^2/(1 - exp(b * x))^2)) -
k * ((k + k * exp(b * x)/(1 - exp(b * x))) * (k * ((1 -
exp(b * x)) * (k * exp(b * x)) + k * exp(b * x)^2))) *
((1 - exp(b * x))^2 * (k * ((k + k * exp(b * x)/(1 -
exp(b * x))) * (2 * ((1 - exp(b * x)) * (k *
exp(b * x)) + k * exp(b * x)^2/(1 - exp(b *
x))^2 + -2 * exp(b * x) * (1 - exp(b * x)) *
((k + k * exp(b * x)/(1 - exp(b * x)))^2 * k)))/((1 -
exp(b * x))^2 * ((k + k * exp(b * x)/(1 - exp(b * x)))^2 *
k))^2) * a + a^2 * 1 - exp(b * x))^2 * (k * exp(b *
x)/(1 - exp(b * x)))^2 * (1 - (k * (1 - exp(b * x))/(k *
exp(b * x)) + k)))^2 * ((1 - exp(b * x))^2 * (k * exp(b *
x)/(1 - exp(b * x)))^2 * (1 - (k * (1 - exp(b * x))/(k *
exp(b * x)) + k)) * (sumy * (k * ((1 - exp(b * x)) * (k *
exp(b * x)) - k * exp(b * x)^2 - k * (2 * exp(b * x)^2) +
k * (4 * exp(b * x)^2 + ((1 - exp(b * x))^2 * (k * exp(b *
x)/(1 - exp(b * x)))^2 * (k * ((1 - exp(b * x)) * (k * exp(b *
x)) + k * exp(b * x)^2))/((1 - exp(b * x))^2 * (k * exp(b *
x)/(1 - exp(b * x)))^2) + ((1 - exp(b * x))^2 * (k * exp(b *
x) * (2 * ((1 - exp(b * x)) * (k * exp(b * x)) + k * exp(b *
x)^2)))/(1 - exp(b * x))^3 + -2 * exp(b * x) * (1 - exp(b *
x)) * (k * exp(b * x)/(1 - exp(b * x)))^2) * (1 - (k * (1 -
exp(b * x))/(k * exp(b * x)) + k))) * (sumy * (k * ((1 -
exp(b * x)) * (k * exp(b * x)) - k * exp(b * x)^2 + k * (2 *
exp(b * x)^2 - (sumy * (k * ((1 - exp(b * x)) * (k *
exp(b * x)) + k * exp(b * x)^2)) * (((1 - exp(b * x))^2 *
(k * exp(b * x)/(1 - exp(b * x)))^2 * ((1 - exp(b * x))^2 *
(k * exp(b * x)/(1 - exp(b * x)))^2 * (k * ((1 - exp(b *
x)) * 

[R] multiply each row in a matrix with the help of the for loop

2012-11-13 Thread Haris Rhrlp
Dear R users,

I have this program
aa-array(rep(0,27),dim=c(3,3,3))
a-matrix(rep(1,9),ncol=3)
n-0

for (i in 1:3) {
    
      a[i,]-a[i,]*(-1)
      n-n+1
      aa[,,n]-a[i,]

}

but i real want to multiply each row  with -1 according to for loop and after 
that to put it in the array. 

I will give an example for what excaclty want

-1 -1 -1
 1  1  1
 1  1  1

-1 -1 -1
-1 -1 -1
 1  1  1

-1 -1 -1
-1 -1 -1
-1 -1 -1
[[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.


Re: [R] multiply each row in a matrix with the help of the for loop

2012-11-13 Thread arun
HI,

May be this helps:
list1-lapply(lapply(1:3,function(i) {aa[1:i,,i]-a[1:i,]*-1
 return(aa[,,i])}),function(x) apply(x,2,function(i) ifelse(i==0,1,x)))
res-array(unlist(list1),dim=c(nrow(list1[[1]]),ncol(list1[[1]]),length(list1)))


 res
#, , 1
#
 #    [,1] [,2] [,3]
#[1,]   -1   -1   -1
#[2,]    1    1    1
#[3,]    1    1    1

#, , 2
#
# [,1] [,2] [,3]
#[1,]   -1   -1   -1
#[2,]   -1   -1   -1
#[3,]    1    1    1
#
#, , 3

  #   [,1] [,2] [,3]
#[1,]   -1   -1   -1
#[2,]   -1   -1   -1
#[3,]   -1   -1   -1


A.K.

- Original Message -
From: Haris Rhrlp haris_r_h...@yahoo.com
To: R-help@r-project.org R-help@r-project.org
Cc: 
Sent: Tuesday, November 13, 2012 7:41 AM
Subject: [R] multiply each row in a matrix with the help of the for loop

Dear R users,

I have this program
aa-array(rep(0,27),dim=c(3,3,3))
a-matrix(rep(1,9),ncol=3)
n-0

for (i in 1:3) {
    
      a[i,]-a[i,]*(-1)
      n-n+1
      aa[,,n]-a[i,]

}

but i real want to multiply each row  with -1 according to for loop and after 
that to put it in the array. 

I will give an example for what excaclty want

-1 -1 -1
 1  1  1
 1  1  1

-1 -1 -1
-1 -1 -1
 1  1  1

-1 -1 -1
-1 -1 -1
-1 -1 -1
    [[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.


__
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.


Re: [R] multiply each row in a matrix with the help of the for loop

2012-11-13 Thread D. Rizopoulos
Another alternative is:

aa - array(0, dim = c(3,3,3))
a - matrix(1, 3, )

for (i in 1:3) {
  a[i, ] - -a[i, ]
  aa[, , i] - a
}

aa

I hope it helps.

Best,
Dimitris



From: r-help-boun...@r-project.org [r-help-boun...@r-project.org] on behalf of 
arun [smartpink...@yahoo.com]
Sent: Tuesday, November 13, 2012 15:25
To: Haris Rhrlp
Cc: R help
Subject: Re: [R] multiply each row in a matrix with the help of the for loop

HI,

May be this helps:
list1-lapply(lapply(1:3,function(i) {aa[1:i,,i]-a[1:i,]*-1
 return(aa[,,i])}),function(x) apply(x,2,function(i) ifelse(i==0,1,x)))
res-array(unlist(list1),dim=c(nrow(list1[[1]]),ncol(list1[[1]]),length(list1)))


 res
#, , 1
#
 #[,1] [,2] [,3]
#[1,]   -1   -1   -1
#[2,]111
#[3,]111

#, , 2
#
# [,1] [,2] [,3]
#[1,]   -1   -1   -1
#[2,]   -1   -1   -1
#[3,]111
#
#, , 3

  #   [,1] [,2] [,3]
#[1,]   -1   -1   -1
#[2,]   -1   -1   -1
#[3,]   -1   -1   -1


A.K.

- Original Message -
From: Haris Rhrlp haris_r_h...@yahoo.com
To: R-help@r-project.org R-help@r-project.org
Cc:
Sent: Tuesday, November 13, 2012 7:41 AM
Subject: [R] multiply each row in a matrix with the help of the for loop

Dear R users,

I have this program
aa-array(rep(0,27),dim=c(3,3,3))
a-matrix(rep(1,9),ncol=3)
n-0

for (i in 1:3) {

  a[i,]-a[i,]*(-1)
  n-n+1
  aa[,,n]-a[i,]

}

but i real want to multiply each row  with -1 according to for loop and after 
that to put it in the array.

I will give an example for what excaclty want

-1 -1 -1
 1  1  1
 1  1  1

-1 -1 -1
-1 -1 -1
 1  1  1

-1 -1 -1
-1 -1 -1
-1 -1 -1
[[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.


__
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.
__
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.


[R] Multiply variable by condition

2012-06-01 Thread jfca283
Hi
I need to do something very simple. I have 2 variables, Y and M. I need to
multiply Y by 1 if M=1, by 2 if M=3 and by 3.6678 if M=9. How do i make it?
Thanks for your time and interest

--
View this message in context: 
http://r.789695.n4.nabble.com/Multiply-variable-by-condition-tp4632086.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] Multiply variable by condition

2012-06-01 Thread Sarah Goslee
There are several ways. The easiest to understand is probably using
if() statements: see ?if for help and examples.

Sarah

On Fri, Jun 1, 2012 at 11:34 AM, jfca283 jfca...@gmail.com wrote:
 Hi
 I need to do something very simple. I have 2 variables, Y and M. I need to
 multiply Y by 1 if M=1, by 2 if M=3 and by 3.6678 if M=9. How do i make it?
 Thanks for your time and interest


-- 
Sarah Goslee
http://www.functionaldiversity.org

__
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.


Re: [R] Multiply variable by condition

2012-06-01 Thread David Winsemius


On Jun 1, 2012, at 2:37 PM, Sarah Goslee wrote:


There are several ways. The easiest to understand is probably using
if() statements: see ?if for help and examples.

Sarah

I would have thought ifelse() to be the necessary function, but for  
such simple cases I find boolean math to be clearer. (I understand  
indivdiual preferences vary in this area.)


 dft -data.frame(Y=1, M=sample(c(1,3,9),20,repl=TRUE))

 dft$res - with(dft, Y*( (M==1) *1 + (M==3)*2 +(M==9)*3.6678) )
 dft
   Y Mres
1  1 1 1.
2  1 9 3.6678
3  1 3 2.
4  1 1 1.
5  1 1 1.
6  1 9 3.6678
7  1 9 3.6678
8  1 9 3.6678
9  1 3 2.
10 1 9 3.6678
11 1 9 3.6678
12 1 9 3.6678
13 1 1 1.
14 1 3 2.
15 1 3 2.
16 1 1 1.
17 1 1 1.
18 1 3 2.
19 1 9 3.6678
20 1 9 3.6678

--
David

On Fri, Jun 1, 2012 at 11:34 AM, jfca283 jfca...@gmail.com wrote:

Hi
I need to do something very simple. I have 2 variables, Y and M. I  
need to
multiply Y by 1 if M=1, by 2 if M=3 and by 3.6678 if M=9. How do i  
make it?

Thanks for your time and interest




--

David Winsemius, MD
West Hartford, CT

__
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.


Re: [R] Multiply variable by condition

2012-06-01 Thread Sarah Goslee
On Fri, Jun 1, 2012 at 5:23 PM, David Winsemius dwinsem...@comcast.net wrote:

 On Jun 1, 2012, at 2:37 PM, Sarah Goslee wrote:

 There are several ways. The easiest to understand is probably using
 if() statements: see ?if for help and examples.

 Sarah

 I would have thought ifelse() to be the necessary function, but for such
 simple cases I find boolean math to be clearer. (I understand indivdiual
 preferences vary in this area.)

That depends on whether you interpret two variables as scalars or
vectors. Since the original poster did not provide any example
whatsoever, I went with one options, while David chose the other.

This clearly illustrates the importance of following the posting
guide, as if we needed any more such examples.

Sarah


 dft -data.frame(Y=1, M=sample(c(1,3,9),20,repl=TRUE))

 dft$res - with(dft, Y*( (M==1) *1 + (M==3)*2 +(M==9)*3.6678) )
 dft
   Y M    res
 1  1 1 1.
 2  1 9 3.6678
 3  1 3 2.
 4  1 1 1.
 5  1 1 1.
 6  1 9 3.6678
 7  1 9 3.6678
 8  1 9 3.6678
 9  1 3 2.
 10 1 9 3.6678
 11 1 9 3.6678
 12 1 9 3.6678
 13 1 1 1.
 14 1 3 2.
 15 1 3 2.
 16 1 1 1.
 17 1 1 1.
 18 1 3 2.
 19 1 9 3.6678
 20 1 9 3.6678

 --
 David

 On Fri, Jun 1, 2012 at 11:34 AM, jfca283 jfca...@gmail.com wrote:

 Hi
 I need to do something very simple. I have 2 variables, Y and M. I need
 to
 multiply Y by 1 if M=1, by 2 if M=3 and by 3.6678 if M=9. How do i make
 it?
 Thanks for your time and interest


 --

 David Winsemius, MD
 West Hartford, CT




-- 
Sarah Goslee
http://www.functionaldiversity.org

__
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.


[R] Multiply Iterated Measurements and Pairwise Comparison

2011-08-26 Thread Leif Kirschenbaum
I am familiar with pairwise t-tests, corrections for multiple testing, etc. 
however I have a problem whose answer I have not found after extensive R-help 
archive and Google searching.

What I have done in the past:
I have N items which are measured, exposed to a condition, and then measured 
again.  I wish to know if the condition changes the items so I can perform a 
t-test.  Better yet I can perform a pairwise t-test because the items are 
individually identifiable.
Sometimes the N items measures do not appear normal and I have used tests other 
than the t-test (chi-square, etc.)
Sometimes the N items are measured after each one of several exposures and I 
used pairwise.t.test().


I have a situation where N is limited and I cannot increase it, therefore I 
arranged for M measurements of the N items to be made before and after exposure 
to the condition.  Thus I have N x M measurements before the condition and N x 
M measurements after the condition.  I assumed that performing M measures 
instead of 1 measure I would be able to provide more statistical power, however 
I don't know what method to use.

I could:
(1) average the M measurements to obtain just N measures for the N items and 
perform pairwise t-testing.
This seems to lose statistical weight.

(2) make believe that I actually have N x M items and perform pairwise 
t-testing.
This will give me a more powerful result than (1), however I feel like I am 
losing something by saying I have N x M independent items instead of M measures 
a piece of N items.

Any suggestions how to statistically test whether the condition changed the N 
items?
I am looking for a p-value to indicate whether there was a significant change 
or not.

P.S. Yes, I actually have more than one condition, so the N items are measured 
M times, exposed to condition 1, measured M times, exposed to condition 2, ... 
and I plan to set a stringent enough confidence level to avoid Bonferroni 
problems.  I have also tried pairwise.t.test() with the Holm method for p-value 
adjustment, however I don't see that pairwise.t.test() has a functionality to 
accommodate my M measurements arrangement.

Thank you for any suggestions.


Leif S. Kirschenbaum, Ph.D., PMP, CRE
Design Reliability
Product Reliability
Space Systems/Loral
3825 Fabian Way M/S H-21
Palo Alto, CA 94303
650-852-6580

__
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.


Re: [R] Multiply list objects

2011-06-17 Thread Uwe Ligges
Since this example is not reproducible (and you have not quuoted any 
former code) I can only give advice in principle:


1. Never use 1:length(x) since this will seriously fail if x is a length 
0 object. Instead, use seq_along(x)
2. If k is a list, then you probably want to use doubled brackets in 
k[[year]] for a scalar valued year.


Uwe Ligges



On 16.06.2011 23:49, mdvaan wrote:

I am still thinking about this problem. The solution could look something
like this (it's net yet working):

k-lapply(h, function (x) x*0) # I keep the same format as h, but set all
values to 0
years-c(1997:1999) # I define the years
for (t in 1:length(years))
{
year = as.character(years[t])
ids = rownames(h[year][[1]])
}
for (m in 1:length(relevant_firms))
{
k[year][[m]]-lapply(k[year], function (col) k[year][[1]][,m] =
h[year][[1]][,m]  k[year][[1]][m,] = h[year][[1]][m,])
} # I am creating new list objects that should look like this
k$'1999'$'8029' and I replace the values in the 8029 column and row by the
original ones in h

Any takes on this problem? Thank you very much!

Best


--
View this message in context: 
http://r.789695.n4.nabble.com/Multiply-list-objects-tp3595719p3603871.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


__
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.


Re: [R] Multiply list objects

2011-06-17 Thread Mathijs de Vaan
Sorry, forgot to quote:

Hi,

I am trying to use the objects from the list below to create more objects.
For each year in h I am trying to create as many objects as there are B's
keeping only the values of B. Example for 1999:

$`1999`$`8025`
  B
B  8025 8026 8027 8028 8029
  802511100
  802610000
  802710000
  802800000
  802900000

$`1999`$`8026`
  B
B  8025 8026 8027 8028 8029
  802501000
  802611101
  802701000
  802800000
  802901000

$`1999`$`8027`
  B
B  8025 8026 8027 8028 8029
  802500100
  802600100
  802711101
  802800000
  802900100

$`1999`$`8028`
  B
B  8025 8026 8027 8028 8029
  802500000
  802600000
  802700000
  802800011
  802900010

$`1999`$`8029`
  B
B  8025 8026 8027 8028 8029
  802500000
  802600001
  802700001
  802800001
  802901111

Any suggestions? You help is very much appreciated!

DF = data.frame(read.table(textConnection(  A  B  C
80  8025  1995
80  8026  1995
80  8029  1995
81  8026  1996
82  8025  1997
82  8026  1997
83  8025  1997
83  8027  1997
90  8026  1998
90  8027  1998
90  8029  1998
84  8026  1999
84  8027  1999
85  8028  1999
85  8029  1999),head=TRUE,stringsAsFactors=FALSE))

e - function(y) crossprod(table(DF[DF$C %in% y, 1:2]))
years - sort(unique(DF$C))
f - as.data.frame(embed(years, 3))
g-lapply(split(f, f[, 1]), e)
h-lapply(g, function (x) ifelse(x0,1,0))



2011/6/17 Uwe Ligges lig...@statistik.tu-dortmund.de

 Since this example is not reproducible (and you have not quuoted any former
 code) I can only give advice in principle:

 1. Never use 1:length(x) since this will seriously fail if x is a length 0
 object. Instead, use seq_along(x)
 2. If k is a list, then you probably want to use doubled brackets in
 k[[year]] for a scalar valued year.

 Uwe Ligges




 On 16.06.2011 23:49, mdvaan wrote:

 I am still thinking about this problem. The solution could look something
 like this (it's net yet working):

 k-lapply(h, function (x) x*0) # I keep the same format as h, but set all
 values to 0
 years-c(1997:1999) # I define the years
 for (t in 1:length(years))
{
year = as.character(years[t])
ids = rownames(h[year][[1]])
}
for (m in 1:length(relevant_firms))
{
k[year][[m]]-lapply(k[year], function (col)
 k[year][[1]][,m] =
 h[year][[1]][,m]  k[year][[1]][m,] = h[year][[1]][m,])
} # I am creating new list objects that should look like
 this
 k$'1999'$'8029' and I replace the values in the 8029 column and row by the
 original ones in h

 Any takes on this problem? Thank you very much!

 Best


 --
 View this message in context: http://r.789695.n4.nabble.com/**
 Multiply-list-objects-**tp3595719p3603871.htmlhttp://r.789695.n4.nabble.com/Multiply-list-objects-tp3595719p3603871.html
 Sent from the R help mailing list archive at Nabble.com.

 __**
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/**
 posting-guide.html http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.



[[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.


Re: [R] Multiply list objects

2011-06-17 Thread Uwe Ligges
I still do not get the point for what task this expansion of data may be 
useful, by I guess you want (in this case probably very inefficient, but 
other can work out how to improve if interested) to insert after


k - lapply(h, function (x) x*0)

the lines:

for(i in seq_along(k)){
temp - rep(k[i], ncol(k[[i]]))
names(temp) - colnames(k[[i]])
k[i] - list(temp)
for(j in seq_along(colnames(h[[i]]))){
k[[i]][[j]][j,] - k[[i]][[j]][,j] - h[[i]][,j]
}
}


Uwe Ligges




On 17.06.2011 13:48, Mathijs de Vaan wrote:

Sorry, forgot to quote:

Hi,

I am trying to use the objects from the list below to create more objects.
For each year in h I am trying to create as many objects as there are B's
keeping only the values of B. Example for 1999:

$`1999`$`8025`
   B
B  8025 8026 8027 8028 8029
   802511100
   802610000
   802710000
   802800000
   802900000

$`1999`$`8026`
   B
B  8025 8026 8027 8028 8029
   802501000
   802611101
   802701000
   802800000
   802901000

$`1999`$`8027`
   B
B  8025 8026 8027 8028 8029
   802500100
   802600100
   802711101
   802800000
   802900100

$`1999`$`8028`
   B
B  8025 8026 8027 8028 8029
   802500000
   802600000
   802700000
   802800011
   802900010

$`1999`$`8029`
   B
B  8025 8026 8027 8028 8029
   802500000
   802600001
   802700001
   802800001
   802901111

Any suggestions? You help is very much appreciated!

DF = data.frame(read.table(textConnection(  A  B  C
80  8025  1995
80  8026  1995
80  8029  1995
81  8026  1996
82  8025  1997
82  8026  1997
83  8025  1997
83  8027  1997
90  8026  1998
90  8027  1998
90  8029  1998
84  8026  1999
84  8027  1999
85  8028  1999
85  8029  1999),head=TRUE,stringsAsFactors=FALSE))

e- function(y) crossprod(table(DF[DF$C %in% y, 1:2]))
years- sort(unique(DF$C))
f- as.data.frame(embed(years, 3))
g-lapply(split(f, f[, 1]), e)
h-lapply(g, function (x) ifelse(x0,1,0))



2011/6/17 Uwe Liggeslig...@statistik.tu-dortmund.de


Since this example is not reproducible (and you have not quuoted any former
code) I can only give advice in principle:

1. Never use 1:length(x) since this will seriously fail if x is a length 0
object. Instead, use seq_along(x)
2. If k is a list, then you probably want to use doubled brackets in
k[[year]] for a scalar valued year.

Uwe Ligges




On 16.06.2011 23:49, mdvaan wrote:


I am still thinking about this problem. The solution could look something
like this (it's net yet working):

k-lapply(h, function (x) x*0) # I keep the same format as h, but set all
values to 0
years-c(1997:1999) # I define the years
for (t in 1:length(years))
{
year = as.character(years[t])
ids = rownames(h[year][[1]])
}
for (m in 1:length(relevant_firms))
{
k[year][[m]]-lapply(k[year], function (col)
k[year][[1]][,m] =
h[year][[1]][,m]   k[year][[1]][m,] = h[year][[1]][m,])
} # I am creating new list objects that should look like
this
k$'1999'$'8029' and I replace the values in the 8029 column and row by the
original ones in h

Any takes on this problem? Thank you very much!

Best


--
View this message in context: http://r.789695.n4.nabble.com/**
Multiply-list-objects-**tp3595719p3603871.htmlhttp://r.789695.n4.nabble.com/Multiply-list-objects-tp3595719p3603871.html
Sent from the R help mailing list archive at Nabble.com.

__**
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/**
posting-guide.htmlhttp://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.







__
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.


Re: [R] Multiply list objects

2011-06-16 Thread mdvaan
I am still thinking about this problem. The solution could look something
like this (it's net yet working):

k-lapply(h, function (x) x*0) # I keep the same format as h, but set all
values to 0
years-c(1997:1999) # I define the years
for (t in 1:length(years))
{
year = as.character(years[t])
ids = rownames(h[year][[1]])
}
for (m in 1:length(relevant_firms))
{
k[year][[m]]-lapply(k[year], function (col) k[year][[1]][,m] =
h[year][[1]][,m]  k[year][[1]][m,] = h[year][[1]][m,])
} # I am creating new list objects that should look like this
k$'1999'$'8029' and I replace the values in the 8029 column and row by the
original ones in h

Any takes on this problem? Thank you very much!

Best


--
View this message in context: 
http://r.789695.n4.nabble.com/Multiply-list-objects-tp3595719p3603871.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


[R] Multiply list objects

2011-06-14 Thread mdvaan
Hi,

I am trying to use the objects from the list below to create more objects.
For each year in h I am trying to create as many objects as there are B's
keeping only the values of B. Example for 1999: 

$`1999`$`8025`
  B
B  8025 8026 8027 8028 8029
  802511100
  802610000
  802710000
  802800000
  802900000

$`1999`$`8026`
  B
B  8025 8026 8027 8028 8029
  802501000
  802611101
  802701000
  802800000
  802901000

$`1999`$`8027`
  B
B  8025 8026 8027 8028 8029
  802500100
  802600100
  802711101
  802800000
  802900100

$`1999`$`8028`
  B
B  8025 8026 8027 8028 8029
  802500000
  802600000
  802700000
  802800011
  802900010

$`1999`$`8029`
  B
B  8025 8026 8027 8028 8029
  802500000
  802600001
  802700001
  802800001
  802901111   

Any suggestions? You help is very much appreciated!

DF = data.frame(read.table(textConnection(  A  B  C
80  8025  1995
80  8026  1995
80  8029  1995
81  8026  1996
82  8025  1997
82  8026  1997
83  8025  1997
83  8027  1997
90  8026  1998
90  8027  1998
90  8029  1998
84  8026  1999
84  8027  1999
85  8028  1999
85  8029  1999),head=TRUE,stringsAsFactors=FALSE))

e - function(y) crossprod(table(DF[DF$C %in% y, 1:2])) 
years - sort(unique(DF$C)) 
f - as.data.frame(embed(years, 3)) 
g-lapply(split(f, f[, 1]), e)
h-lapply(g, function (x) ifelse(x0,1,0))

--
View this message in context: 
http://r.789695.n4.nabble.com/Multiply-list-objects-tp3595719p3595719.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] Multiply each depth level of an array with another vector element

2010-08-06 Thread Greg Snow
?sweep

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
 project.org] On Behalf Of Maurits Aben
 Sent: Thursday, August 05, 2010 2:00 PM
 To: r-help@r-project.org
 Subject: [R] Multiply each depth level of an array with another vector
 element
 
 Suppose
 
 x - array(c(1,1,1,1,2,2,2,2), dim = c(2,2,2))
 
 y - c(5, 10)
 
 
 
 Now I would like to multiply x[, , 1] with y[1] and x[, , 2] with y[2].
 
 
 
 Possible solution is a for-loop:
 
 for (i in 1:2) {
 
 x[, , i] * y[i]
 
 }
 
 
 
 Another possible solution is this construction:
 
 as.vector(t(replicate(nrow(x) * ncol(x), y))) * x
 
 
 
 I find that these two solutions have a relatively large computation
 time (on
 larger arrays) and I suspect there should be solution with less
 computation
 time. Is there a faster method to perform this calculation (thus to
 multiply
 the depth of an array with respective vector elements)?
 
 
 
 Best regards,
 
 Maurits Aben
 
   [[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.

__
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.


Re: [R] Multiply each depth level of an array with another vector element

2010-08-06 Thread Wu Gong

It's interesting that sweep is the slowest one comparing to replicate and rep
:)

-
A R learner.
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Multiply-each-depth-level-of-an-array-with-another-vector-element-tp2315537p2316586.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


[R] Multiply each depth level of an array with another vector element

2010-08-05 Thread Maurits Aben
Suppose

x - array(c(1,1,1,1,2,2,2,2), dim = c(2,2,2))

y - c(5, 10)



Now I would like to multiply x[, , 1] with y[1] and x[, , 2] with y[2].



Possible solution is a for-loop:

for (i in 1:2) {

x[, , i] * y[i]

}



Another possible solution is this construction:

as.vector(t(replicate(nrow(x) * ncol(x), y))) * x



I find that these two solutions have a relatively large computation time (on
larger arrays) and I suspect there should be solution with less computation
time. Is there a faster method to perform this calculation (thus to multiply
the depth of an array with respective vector elements)?



Best regards,

Maurits Aben

[[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.


Re: [R] Multiply each depth level of an array with another vector element

2010-08-05 Thread Wu Gong

I have only achieved a half improvement.

x - array(1:2400*1, dim = c(200,300,400))
y - 1:400*1
ptm - proc.time()
z - x*as.vector(t(replicate(dim(x)[1]*dim(x)[2], y[1:dim(x)[3]]))) 
replicate:
proc.time() - ptm

x - array(1:2400*1, dim = c(200,300,400))
y - 1:400*1
ptm - proc.time()
z - x*rep(y[1:dim(x)[3]],each=dim(x)[1]*dim(x)[2])
rep:
proc.time() - ptm

-
A R learner.
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Multiply-each-depth-level-of-an-array-with-another-vector-element-tp2315537p2315860.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


[R] Multiply Two Dataframes

2010-06-07 Thread Bert Jacobs
 

 

Hi,

I have the following two dataframes (Df1 and Df2):

Df1 has a layout like

 

ID Location  Var1 Var2 Var3 . Var20

A  Loc2  1   11.   1

A  Loc1  0   10.   1

B  Loc1  0   00.   0   

A  Loc3  1   01.   0

C  Loc3  1   01.   0

B  Loc2  0   00.   1   

D .

..

 

 

Df2 has a layout like

ID Var1 Var2 Var3 ..  Var20

A   0.2 0.30.2      0.15

B   0.1 0.1 0.1  .0.1   

C00   0 .   0.50

D .

.

 

And I like to have the following result

ID Location  Var1  Var2   Var3 .Var20

A  Loc2  0.2 0.3  0.2   . 0.15

A  Loc1  00.3  0  . 0.15

B  Loc1  00 0  . 0   

A  Loc3  0.2 00.2   .  0

C  Loc3  0   00  .   0

B  Loc2  0   00  .   0.1   

D .

..

 

Is this easy doable in R? I've tried to figure it out with mapply but
without result.

Thanks for helping me out.

Bert

 

 


[[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.


Re: [R] Multiply Two Dataframes

2010-06-07 Thread ONKELINX, Thierry
Dear Bert,

The easiest thing would be to merge both datasets and then multiply the 
corresponding columns.

both - merge(df1, df2)
both[, 3:22] * both[, 23:42]

HTH,

Thierry

-Oorspronkelijk bericht-
Van: r-help-boun...@r-project.org namens Bert Jacobs
Verzonden: ma 7-6-2010 20:29
Aan: r-help@r-project.org
Onderwerp: [R] Multiply Two Dataframes
 
 

 

Hi,

I have the following two dataframes (Df1 and Df2):

Df1 has a layout like

 

ID Location  Var1 Var2 Var3 . Var20

A  Loc2  1   11.   1

A  Loc1  0   10.   1

B  Loc1  0   00.   0   

A  Loc3  1   01.   0

C  Loc3  1   01.   0

B  Loc2  0   00.   1   

D .

..

 

 

Df2 has a layout like

ID Var1 Var2 Var3 ..  Var20

A   0.2 0.30.2      0.15

B   0.1 0.1 0.1  .0.1   

C00   0 .   0.50

D .

.

 

And I like to have the following result

ID Location  Var1  Var2   Var3 .Var20

A  Loc2  0.2 0.3  0.2   . 0.15

A  Loc1  00.3  0  . 0.15

B  Loc1  00 0  . 0   

A  Loc3  0.2 00.2   .  0

C  Loc3  0   00  .   0

B  Loc2  0   00  .   0.1   

D .

..

 

Is this easy doable in R? I've tried to figure it out with mapply but
without result.

Thanks for helping me out.

Bert

 

 


[[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.


Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.

[[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.


Re: [R] Multiply Normal Curves

2009-09-24 Thread KABELI MEFANE
R -helpers
 
i have been trying to do this problem without must success,i managed to do a 
graph for x, but it is not what i want to define,(i want to specify number of 
observations as well). I have also been able to do simple rendom sample.
 
data.frame(ID=c(1,2,3),mu=c(1,34000,5),sigma=c(2000,3000,5000))
curve(dnorm(x,mean=parms$mu[1],sd=parms$sigma[1]),from=2000,
to=8, ylab=density, col=red)
curve(dnorm(x,mean=parms$mu[2],sd=parms$sigma[2]),from=1000,
to=8, ylab=density, col=blue, add=TRUE)
curve(dnorm(x,mean=parms$mu[3],sd=parms$sigma[3]),from=1000,
to=8, ylab=density, col=forestgreen, add=TRUE)
###

R-helpers
 
I have been learning a little bit of R. I am simulating and i want to draw a 
normal curve for all my variables so that i will see the overlaps and reduce 
them, after that i want to draw a gragh of all the values that are in the data 
frame to see if it follows a normal distribution also. Lastly i will try to 
sample from this data. Please help and make suggestions.
 
#My  original codes

#code for dataframe
Hypermarket - matrix(rnorm(100, mean=5, sd=5000))
Supermarket - matrix(rnorm(400, mean=34000, sd=3000))
Minimarket  - matrix(rnorm(1000, mean=1,sd=2000))
Cornershop  - matrix(rnorm(1500, mean=2500, sd=500))
Spazashop   - matrix(rnorm(2000, mean=1000, sd=250))
dat=data.frame(type=c(rep(Hypermarket,100), rep(Supermarket,400),
rep(Minimarket,1000),rep(Cornershop,1500), rep(Spazashop,2000)),
value=c(Hypermarket, Supermarket, Minimarket, Cornershop,Spazashop))
dat
#code for histogram of Hypermarket(Please suggest something simple )
hist(Hypermarket, breaks=seq(3, 65000, 1000), freq=F)
x- seq(3, 65000, 500)
lines(x, dnorm(x, 5, 5000))



 
[[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.


[R] Multiply List by a Numeric

2009-08-24 Thread Brigid Mooney
I apologize for what seems like it should be a straighforward query.

I am trying to multiply a list by a numeric and thought there would be a
straightforward way to do this, but the best solution I found so far has a
for loop.
Everything else I try seems to throw an error non-numeric argument to
binary operator

Consider the example:

a - 1
b - 1:2
c - 1:3
abc - list(a,b,c)
To multiply every element of abc by a numeric, say 3, I wrote a for-loop:

for (i in 1:length(abc))
{
abc[[i]] - 3*abc[[i]]
}

Is this really the simplest way or am I missing something?

Thanks!

[[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.


Re: [R] Multiply List by a Numeric

2009-08-24 Thread Marc Schwartz

On Aug 24, 2009, at 10:58 AM, Brigid Mooney wrote:


I apologize for what seems like it should be a straighforward query.

I am trying to multiply a list by a numeric and thought there would  
be a
straightforward way to do this, but the best solution I found so far  
has a

for loop.
Everything else I try seems to throw an error non-numeric argument to
binary operator

Consider the example:

a - 1
b - 1:2
c - 1:3
abc - list(a,b,c)
To multiply every element of abc by a numeric, say 3, I wrote a for- 
loop:


for (i in 1:length(abc))
{
abc[[i]] - 3*abc[[i]]
}

Is this really the simplest way or am I missing something?

Thanks!


Try:

 abc
[[1]]
[1] 1

[[2]]
[1] 1 2

[[3]]
[1] 1 2 3


 lapply(abc, *, 3)
[[1]]
[1] 3

[[2]]
[1] 3 6

[[3]]
[1] 3 6 9


See ?lapply for more information.

HTH,

Marc Schwartz

__
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.


Re: [R] Multiply List by a Numeric

2009-08-24 Thread Peter Ehlers

Try

lapply(abc, function(x) x*3)

Peter Ehlers

Brigid Mooney wrote:

I apologize for what seems like it should be a straighforward query.

I am trying to multiply a list by a numeric and thought there would be a
straightforward way to do this, but the best solution I found so far has a
for loop.
Everything else I try seems to throw an error non-numeric argument to
binary operator

Consider the example:

a - 1
b - 1:2
c - 1:3
abc - list(a,b,c)
To multiply every element of abc by a numeric, say 3, I wrote a for-loop:

for (i in 1:length(abc))
{
abc[[i]] - 3*abc[[i]]
}

Is this really the simplest way or am I missing something?

Thanks!

[[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.




__
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.


[R] Multiply each column of array by vector component

2007-11-15 Thread M . T . Charemza
Hi,

I've got an array, say with i,jth entry = A_ij, and a vector, say with jth
entry= v_j. I would like to multiply each column of the array by the
corresponding vector component, i,e. find the array with i,jth entry

A_ij * v_j

This seems so basic but I can't figure out how to do it without a loop.
Any suggestions?

Michal.

__
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.


Re: [R] Multiply each column of array by vector component

2007-11-15 Thread Gabor Grothendieck
On Nov 15, 2007 12:50 PM,  [EMAIL PROTECTED] wrote:
 Hi,

 I've got an array, say with i,jth entry = A_ij, and a vector, say with jth
 entry= v_j. I would like to multiply each column of the array by the
 corresponding vector component, i,e. find the array with i,jth entry

 A_ij * v_j

 This seems so basic but I can't figure out how to do it without a loop.
 Any suggestions?


Here are 4 ways:


A - matrix(1:6, 3)
v - 10:11

A %*% diag(v)

A * rep(1, nrow(A)) %o% v

t(t(A) * v)

A * replace(A, TRUE, v[col(A)])

__
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.


Re: [R] Multiply each column of array by vector component

2007-11-15 Thread Benilton Carvalho
?sweep

b

On Nov 15, 2007, at 12:50 PM, [EMAIL PROTECTED] wrote:

 Hi,

 I've got an array, say with i,jth entry = A_ij, and a vector, say with 
 jth
 entry= v_j. I would like to multiply each column of the array by the
 corresponding vector component, i,e. find the array with i,jth entry

 A_ij * v_j

 This seems so basic but I can't figure out how to do it without a loop.
 Any suggestions?

 Michal.

 __
 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.

__
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.


Re: [R] Multiply each column of array by vector component

2007-11-15 Thread Henrique Dallazuanna
If i understand your question, you can do:

x - matrix(1:10, 2)
y - sample(10,5)
apply(x, 1, function(.x)mapply(y, .x, FUN=*))


On 15/11/2007, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 Hi,

 I've got an array, say with i,jth entry = A_ij, and a vector, say with jth
 entry= v_j. I would like to multiply each column of the array by the
 corresponding vector component, i,e. find the array with i,jth entry

 A_ij * v_j

 This seems so basic but I can't figure out how to do it without a loop.
 Any suggestions?

 Michal.

 __
 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.



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

__
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.


Re: [R] Multiply each column of array by vector component

2007-11-15 Thread Marc Schwartz

On Thu, 2007-11-15 at 17:50 +, [EMAIL PROTECTED] wrote:
 Hi,
 
 I've got an array, say with i,jth entry = A_ij, and a vector, say with jth
 entry= v_j. I would like to multiply each column of the array by the
 corresponding vector component, i,e. find the array with i,jth entry
 
 A_ij * v_j
 
 This seems so basic but I can't figure out how to do it without a loop.
 Any suggestions?
 
 Michal.

If I understand you correctly:

  t(t(A) * v)


set.seed(1)

A - matrix(sample(20), ncol = 2)
v - c(5, 2)

 A
  [,1] [,2]
 [1,]63
 [2,]82
 [3,]   11   20
 [4,]   16   10
 [5,]45
 [6,]   147
 [7,]   15   12
 [8,]9   17
 [9,]   19   18
[10,]1   13


 t(t(A) * v)
  [,1] [,2]
 [1,]   306
 [2,]   404
 [3,]   55   40
 [4,]   80   20
 [5,]   20   10
 [6,]   70   14
 [7,]   75   24
 [8,]   45   34
 [9,]   95   36
[10,]5   26

HTH,

Marc Schwartz

__
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.


Re: [R] Multiply each column of array by vector component

2007-11-15 Thread Greg Snow
?sweep

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[EMAIL PROTECTED]
(801) 408-8111
 
 

 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of 
 [EMAIL PROTECTED]
 Sent: Thursday, November 15, 2007 10:50 AM
 To: r-help@r-project.org
 Subject: [R] Multiply each column of array by vector component
 
 Hi,
 
 I've got an array, say with i,jth entry = A_ij, and a vector, 
 say with jth entry= v_j. I would like to multiply each column 
 of the array by the corresponding vector component, i,e. find 
 the array with i,jth entry
 
 A_ij * v_j
 
 This seems so basic but I can't figure out how to do it 
 without a loop.
 Any suggestions?
 
 Michal.
 
 __
 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.
 

__
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.


[R] multiply for vector in R

2007-10-24 Thread Guolian Kang
Dear All,

Is there a conmand to calculate the multiplication of the elements in a vector? 
For example:

a=c(1,2,3,4)
I want to get 1*2*3*4=24. 
Because the dimension of the vector is high, I want to know there is a command 
for this task in R?

Thank you so much!

Kang

__



[[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.


Re: [R] multiply for vector in R

2007-10-24 Thread ecatchpole
Guolian Kang wrote on 10/24/2007 01:37 PM:
 Dear All,

 Is there a conmand to calculate the multiplication of the elements in a 
 vector? For example:

 a=c(1,2,3,4)
 I want to get 1*2*3*4=24. 
 Because the dimension of the vector is high, I want to know there is a 
 command for this task in R?

 Thank you so much!

 Kang

   

?prod

Ted.

-- 
 Dr E.A. Catchpole  
 Visiting Fellow
 Univ of New South Wales at ADFA, Canberra, Australia
_ and University of Kent, Canterbury, England
   'v'- www.pems.adfa.edu.au/~ecatchpole  
  /   \   - fax: +61 2 6268 8786   
   m m- ph:  +61 2 6268 8895

__
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.


[R] Multiply a 3D-array by a vector (weighted combination of matrices)

2007-10-17 Thread Yvonnick NOEL
Hello,

I would like to compute a weighted combination of matrices.

I have a number of matrices, arranged in a 3D-array, say:

z = array(rep(1:3,c(9,9,9)),c(3,3,3))

so that z[,,1] is my first matrix, and z[,,2] and z[,,3] the second and 
third one, and a vector of coefficients:

w = rep(1/3,3)

I would like to compute:

w[1]* z[,,1] + w[2]*z[,,2] + w[3]*z[,,3]

I could of course do this using a for() loop, but would like to know if 
there is a way to do it in a vectorized manner, or any other way that 
is likely to result in faster computation.

Any hint ?

Thank you very much in advance,

YNOEL

__
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.