Skip to content

Loops

4 messages · Rui Barradas, arun, Francesca PANCOTTO

#
Hello,

I think there is an error in the expression

100-(100*abs(fa1[i]/sum(fa1[i])-(1/3)))

Note that fa1[i]/sum(fa1[i]) is always 1. If it's fa1[i]/sum(fa1), try 
the following, using lists to hold the results.


# Make up some data
set.seed(6628)
p <- matrix(runif(300), nrow = 100)

idx <- seq(1, 100, by = 25)
fa <- lapply(idx, function(i) colSums(p[i:(i + 24), ]))
fa[[5]] <- colSums(p)

fab <- lapply(fa, function(x) 100 - 100*abs(x/sum(x) - 1/3))
fab

You can give names to the lists elements, if you want to.


names(fa) <- paste0("fa", 1:5)
names(fab) <- paste0("fa", 1:5, "b")


Hope this helps,

Rui Barradas

Em 27-01-2013 08:02, Francesca escreveu:
#
Hi,

You could use library(plyr) as well
library(plyr)
pnew<-colSums(aaply(laply(split(as.data.frame(p),((1:nrow(as.data.frame(p))-1)%/% 25)+1),as.matrix),c(2,3),function(x) x))
res<-rbind(t(pnew),colSums(p))
row.names(res)<-1:nrow(res)
res<- 100-100*abs(res/rowSums(res)-(1/3))
A.K.


----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: Francesca <francesca.pancotto at gmail.com>
Cc: r-help at r-project.org
Sent: Sunday, January 27, 2013 6:17 AM
Subject: Re: [R] Loops

Hello,

I think there is an error in the expression

100-(100*abs(fa1[i]/sum(fa1[i])-(1/3)))

Note that fa1[i]/sum(fa1[i]) is always 1. If it's fa1[i]/sum(fa1), try 
the following, using lists to hold the results.


# Make up some data
set.seed(6628)
p <- matrix(runif(300), nrow = 100)

idx <- seq(1, 100, by = 25)
fa <- lapply(idx, function(i) colSums(p[i:(i + 24), ]))
fa[[5]] <- colSums(p)

fab <- lapply(fa, function(x) 100 - 100*abs(x/sum(x) - 1/3))
fab

You can give names to the lists elements, if you want to.


names(fa) <- paste0("fa", 1:5)
names(fab) <- paste0("fa", 1:5, "b")


Hope this helps,

Rui Barradas

Em 27-01-2013 08:02, Francesca escreveu:
______________________________________________
R-help at 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.