djbanana writes:
> Hi,
>
> I think I solved it myself by writing loops.
>
> What I meant is: are there in-built functions in R that calculate the
> following:
>
> a1(b2+...+b190) + a2(b1+b3+...+b190) + ...
Following Rainer's setup:
x <- data.frame( a = sample( 1:10, 4 ), b = sample( 11:20, 4 )
Hi,
I think I solved it myself by writing loops.
What I meant is: are there in-built functions in R that calculate the
following:
a1(b2+...+b190) + a2(b1+b3+...+b190) + ...
I managed to solve it, quite similar to what you just emailed.
Thanks anyway!
--
View this message in context:
http:
On 19-10-2012, at 10:38, Berend Hasselman wrote:
>
> On 18-10-2012, at 21:33, djbanana wrote:
>
>> I would like to code the following in R: a1(b1+b2+b3) + a2(b1+b3+b4) +
>> a3(b1+b2+b4) + a4(b1+b2+b3)
>>
>> or in summation notation: sum_{i=1, j\neq i}^{4} a_i * b_i
>>
>> I realise this is the
On 18-10-2012, at 21:33, djbanana wrote:
> I would like to code the following in R: a1(b1+b2+b3) + a2(b1+b3+b4) +
> a3(b1+b2+b4) + a4(b1+b2+b3)
>
> or in summation notation: sum_{i=1, j\neq i}^{4} a_i * b_i
>
> I realise this is the same as: sum_{i=1, j=1}^{4} a_i * b_i - sum_{i=j} a_i
> * b_i
You asked for a loop, you got one...
Vectorized is easier and faster:
x$c <- x$a * ( sum( x$b ) - x$b )
Rgds,
Rainer
On Friday 19 October 2012 09:38:35 you wrote:
> Hi,
>
> I think I solved it myself by writing loops.
>
> What I meant is: are there in-built functions in R that calculate the
Assuming that you actually mean
a1(b2+b3+b4) + a2(b1+b3+b4) + a3(b1+b2+b4) + a4(b1+b2+b3)
^ ^ ^
this might give you what you want:
x <- data.frame( a = sample( 1:10, 4 ), b = sample( 11:20, 4 ) )
x
I am aware of the most basic stuff, especially vectorization and
subscripting.
I only gave a simple example with length 4. I need to do that for vectors of
length 190.
Are there any in-built commands?
Or should I write the loops myself?
Thank you.
--
View this message in context:
http://r.7
Have you read the Introduction to R tutorial? You seem unaware of even
the most basic stuff, especially vectorization and subscripting.
Also, this smells like homework, and we don't do homework here.
-- Bert
On Thu, Oct 18, 2012 at 12:33 PM, djbanana wrote:
> I would like to code the following
8 matches
Mail list logo