Re: [R] Multiply

2023-08-04 Thread avi.e.gross
,", 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 lo

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=d

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 = multipl

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 havin

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

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

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] >

Re: [R] multiply of two expressions

2014-05-06 Thread Ben Bolker
Niloofar.Javanrouh 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. >

Re: [R] multiply of two expressions

2014-05-05 Thread Gabor Grothendieck
On Mon, May 5, 2014 at 10:16 PM, David Winsemius wrote: > > On May 5, 2014, at 6:05 PM, Gabor Grothendieck wrote: > >> On Mon, May 5, 2014 at 9:43 AM, Niloofar.Javanrouh >> wrote: >>> >>> >>> hello, >>> i want to differentiate of L with respect to b >>> when: >>> >>> L= k*ln (k/(k+mu)) + sum(y) *

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

Re: [R] multiply of two expressions

2014-05-05 Thread Gabor Grothendieck
On Mon, May 5, 2014 at 9:43 AM, Niloofar.Javanrouh 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 th

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

2012-11-13 Thread D. Rizopoulos
g] 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])})

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

Re: [R] Multiply variable by condition

2012-06-01 Thread Sarah Goslee
On Fri, Jun 1, 2012 at 5:23 PM, David Winsemius 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

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.

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

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 <- re

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 802511

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

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

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 a

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

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

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

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

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.

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 e

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-

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

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

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 >

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

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

Re: [R] multiply for vector in R

2007-10-24 Thread Ingmar Visser
?prod On 24 Oct 2007, at 05:37, Guolian Kang wrote: > 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 fo

Re: [R] multiply for vector in R

2007-10-24 Thread Dimitris Rizopoulos
look at ?prod(), e.g., prod(a) I hope it helps. Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be

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

2007-10-17 Thread Robin Hankin
Hi you need the tensor library: > library(tensor) > z <- array(runif(27),rep(3,3)) > w <- runif(3) > w[1]* z[,,1] + w[2]*z[,,2] + w[3]*z[,,3] [,1] [,2] [,3] [1,] 1.2700333 1.1920113 0.8015904 [2,] 0.5175217 0.7808569 0.6306053 [3,] 0.8386015 0.6143882 0.6382314 > tens