Hi,
My english isn't brilliant and my problem is very difficult to describe but
I try ;)
My first question is: May I write loop "for" like this or similar -
for (i
in sth : sth[length(sth)], k in sth_else : length(sth_else) ) - I'd like
to have two independent conditions in the same loop "for".
My secound question depend on program below. I'd like to write every result
in matrix but I also want to call my function "odleg" using vector
"e3" -
exactly using values which are inside.
Can anyone please guide me, how to do that?
Thanks
## Function
odleg <- function (Xa,Xb,Ya,Yb){
d <- ((Xa-Xb)^2+(Ya-Yb)^2)^(1/2)
return (d)
}
# Database
ma=matrix(c(0.51139630,-0.12937287, 0.19530080,
0.02273691,-0.43634186,-0.01717149,-0.27597035,-0.41732933,-0.15476464,-0.15692965),nrow
= 5, ncol=2)
e3<- c(1,2,4)
for (i in e3[1] : e3[length(e3)]; (k in 1 : length(e3))){
for (j in e3[1] : e3[length(e3)]; (l in 1 : length(e3))){
me1[k,l] = odleg
(nepitabds$points[i,1],nepitabds$points[j,1],nepitabds$points[i,2],nepitabds$points[j,2])
}}
--
View this message in context:
http://www.nabble.com/difficult-%22for%22-tp25107157p25107157.html
Sent from the R help mailing list archive at Nabble.com.
Hi gregorio,
I think you can have nested for() without problem, like:
for (i in sth : sth[length(sth)])
{
for (k in A:B)
{
....
}
}
or you can build conditional for's like:
rangeNegative<-1:10
rangePositive<-1:20
if (rnorm(1)>=0) MYRANGE<-rangePositive
if (rnorm(1)<0) MYRANGE<-rangeNegative
MYRANGE
for (i in MYRANGE)
{
}
I not played with the second question.
Good luck
milton
On Sun, Aug 23, 2009 at 4:34 PM, Grzes <gregorio99@gmail.com> wrote:
>
> Hi,
> My english isn't briliant and my problem is very dificult to descripe
but I
> try ;)
> My first question is: May I write loop "for" like this or similar
- for (i
> in sth : sth[length(sth)], k in sth else : length(sth else) ) - I'd
like
> to have two independent conditions in the same loop "for".
>
> My secound question depend on program below. I'd like to write every
result
> in matrix but I also want to call my function "odleg" use vector
"e3" -
> exactly using values which are inside.
>
> Can anyone please guide me, how to do that?
> Thanks
>
> ## Function
> odleg <- function (Xa,Xb,Ya,Yb){
> d <- ((Xa-Xb)^2+(Ya-Yb)^2)^(1/2)
> return (d)
> }
>
> # Database
> ma=matrix(c(0.51139630,-0.12937287, 0.19530080,
>
>
0.02273691,-0.43634186,-0.01717149,-0.27597035,-0.41732933,-0.15476464,-0.15692965),nrow
> = 5, ncol=2)
>
> e3<- c(1,2,4)
> for (i in e3[1] : e3[length(e3)]; (k in 1 : length(e3))){
> for (j in e3[1] : e3[length(e3)]; (l in 1 : length(e3))){
>
> me1[k,l] = odleg
>
>
(nepitabds$points[i,1],nepitabds$points[j,1],nepitabds$points[i,2],nepitabds$points[j,2])
> }}
> --
> View this message in context:
> http://www.nabble.com/dificult-loop-%22for%22-tp25107157p25107157.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
On Aug 23, 2009, at 4:34 PM, Grzes wrote:> > Hi, > My english isn't briliant and my problem is very dificult to > descripe but I > try ;) > My first question is: May I write loop "for" like this or similar - > for (i > in sth : sth[length(sth)], k in sth else : length(sth else) ) - > I'd like > to have two independent conditions in the same loop "for".Pretty sure the language has not accommodated you for that wish yet. You are going to need extra inner "{for"'s and another closing "}"'s. That does not seem like a very large barrier to surmount. Side question/whinge: where did the convention arise to use "sth" as an abbreviation? I am a native English-speaker and it still confuses me every time I see it. Why not just use "obj" or "item" or .....? I keep thinking it has to do with a sequence of s-numbered things. (And of course "sth else" got R-parsed as a logical construction.) In R you probably want to use seq_along(e3) anyway, rather than obj[1]:obj[length(obj)] ?seq_along > e3 <- c(1,2,4) > for (i in seq_along(e3) ) print(letters[e3[i]]) [1] "a" [1] "b" [1] "d"> > My secound question depend on program below. I'd like to write every > result > in matrix but I also want to call my function "odleg" use vector > "e3" - > exactly using values which are inside. > > Can anyone please guide me, how to do that? > Thanks > > ## Function > odleg <- function (Xa,Xb,Ya,Yb){ > d <- ((Xa-Xb)^2+(Ya-Yb)^2)^(1/2) > return (d) > } > > # Database > ma=matrix(c(0.51139630,-0.12937287, 0.19530080, > 0.02273691 > ,-0.43634186 > ,-0.01717149,-0.27597035,-0.41732933,-0.15476464,-0.15692965),nrow > = 5, ncol=2)untested mods: presumably "me1" has already been defined somewhere else? e3<- c(1,2,4) for (i in seq_along(e3) { for (k in seq_along(e3) ) { for (j in seq_along(e3)] { for (l in seq_along(e3) ){ me1[k,l] = odleg (nepitabds$points[e3[i],1], nepitabds$points[e3[j],1], nepitabds $points[e3[i],2], nepitabds$points[e3[j],2]) }}}} The concern I see with the last part of above code is that nepithabds $points will need to be a matrix or list that responds properly to [n,m] extraction. Is it? That would be possible as I understand the rules of data.frames and lists, but it is a bit unusual. David Winsemius, MD Heritage Laboratories West Hartford, CT