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-
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(x[(1):(i)])) - i
}
e<- n*(prod(d))
2nd loop
LD<-list()
for (i in
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
I have data that looks like this:
> df1
group id
1 red A
2 red B
3 red C
4 blue D
5 blue E
6 blue F
I want a list of the groups containing vectors with the ids.I am
avoiding subset(), as it is
only recommended for interactive use. Here's what I have so far:
df1 <- data.fra
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
Hi,
I'm trying to normalize some data.
My data is organized by groups. I want to normalize PER GROUP as
opposed to over the entire data set.
The current double loop that I'm using takes almost an hour to run on
about 30,000 rows of data in 2,500 groups.
I'm currently doing this:
-
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
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:
> x
[,1] [,2] [,3]
[1,]4 111
[2,]192
[3,]733
[4,]364
[5,]685
> xk
Var1 Var2 Var3
1 -0.2
14 matches
Mail list logo