Thanks a lot Boris and Berend.
I'll consider the brackets ((m-1) in every loop). In addition, I'll read
more on profiling my code. In fact,I'm using the apply () in another part
of my code.
Thanks again for helping.
Maram Salem
On 25 October 2015 at 14:26, Berend Hasselman wrote:
>
> > On 25
> On 25 Oct 2015, at 11:42, Maram SAlem wrote:
>
> Hi All,
>
> I wonder if I can avoid the for() loop in any of the following loops.These
> loops are a part of a larger code which I'm trying to accelerate.
>
> n=6
> m=4
> x<-c(0,1,1)
>
> 1st loop
>
> for (i in 1:m-1)
> {
> d[i]<- n- (sum
Sorry - I just noticed you actually have an error in your code: you had
parentheses everywhere they were not needed and I overlooked you had not put
them where they actually are needed. It has to be for (i in 1:(m-1)) ..., not
as you wrote. I'm sure you'll understand the difference.
d <- numeri
If this code is slow it is not because you are using loops, but because you are
dynamically building your vectors and lists and their size needs to change with
each iteration causing significant unnecessary computational overhead. If you
simply do something like
d <- numeric(m-1)
for (i in 1:m-
Usually you can just use cor() and it will do all the possibilities directly:
x <- matrix(rnorm(100), ncol = 10)
cor(x)
But that works on the columns, so you'll need to transpose things if
you want all possible row combinations: cor(t(x))
Hope this helps,
Michael
On Fri, Apr 6, 2012 at 9:57 AM,
> df1 <- data.frame(group=c("red", "red", "red", "blue", "blue",
"blue"), id=c("A", "B", "C", "D", "E", "F"))
df1 <- data.frame(group=c("red", "red", "red", "blue", "blue",
+ "blue"), id=c("A", "B", "C", "D", "E", "F"))
> df1
group id
1 red A
2 red B
3 red C
4 blue D
5 blue E
6 blue
Wow,
That's nice.
Should work well, but I just realized that I missed something in
explaining my code.
I need to calculate the exp function on X
so it should be
exp(x) / sum(exp(x)) for each group
I tried this with:
foo <- ave(rawdata$foo,rawdata$code,FUN=function(x) exp(x) / sum(exp(x)))
Dimitris Rizopoulos wrote:
> you could try something along these lines:
>
> data <- data.frame(y = rnorm(100), group = rep(1:10, each = 10))
>
> data$sum <- ave(data$y, data$group, FUN = sum)
> data$norm.y <- data$y / data$sum
> data
.. or even
transform(data, norm=ave(y, group, FUN = function(
you could try something along these lines:
data <- data.frame(y = rnorm(100), group = rep(1:10, each = 10))
data$sum <- ave(data$y, data$group, FUN = sum)
data$norm.y <- data$y / data$sum
data
I hope it helps.
Best,
Dimitris
Noah Silverman wrote:
Hi,
I'm trying to normalize some data.
My
On Jan 6, 2008, at 7:55 PM, dxc13 wrote:
>
> useR's,
>
> I would like to know if there is a way to avoid using FOR loops to
> perform
> the below calculation.
>
> Consider the following data:
>
snip
> Here, X is a matrix of 3 variables in which each is of size 5 and
> XK are
> some values that
10 matches
Mail list logo