[R] R loop problem

2011-08-04 Thread testrider
I have run into a speed issue, and given the size of the problem it feels
like there should be an easy solution. Here is the problem statement with
some arbitrary numbers added.

#p,q: vector with length(q)==length(p)==1 and length(levels(p))==3000
#y,z: vectors with length(levels(y))=length(y)==length(z)==5000

for (i in levels(p)){
q[i==p]-z[i==y]}

At first i used two for loops which was horrible, now i got rid of one but i
don't know how to lose the second one.


PS. I expect the solution to be available through google etc and I have
searched for a solution but i did not find any usefull websites, probably
because i cannot pinpoint the best search words.

--
View this message in context: 
http://r.789695.n4.nabble.com/R-loop-problem-tp3718103p3718103.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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] R loop problem

2011-08-04 Thread jim holtman
A subset of actual data and what you would expect as a result would be
very helpful.   All you say is that p.q are vectors, but it would
appear that they are character vectors, but the content is unknown.
Also will the expression q[i==p]-z[i==y] have the same length on
each side; the vectors appear to be of different lengths -- what
happens if recycling kicks in?  Or it is always a match of length 1

So a little more definition, and data and example, would help.

On Thu, Aug 4, 2011 at 5:27 AM, testrider testri...@gmail.com wrote:
 I have run into a speed issue, and given the size of the problem it feels
 like there should be an easy solution. Here is the problem statement with
 some arbitrary numbers added.

 #p,q: vector with length(q)==length(p)==1 and length(levels(p))==3000
 #y,z: vectors with length(levels(y))=length(y)==length(z)==5000

 for (i in levels(p)){
 q[i==p]-z[i==y]}

 At first i used two for loops which was horrible, now i got rid of one but i
 don't know how to lose the second one.


 PS. I expect the solution to be available through google etc and I have
 searched for a solution but i did not find any usefull websites, probably
 because i cannot pinpoint the best search words.

 --
 View this message in context: 
 http://r.789695.n4.nabble.com/R-loop-problem-tp3718103p3718103.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
 and provide commented, minimal, self-contained, reproducible code.




-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] R loop problem

2011-08-04 Thread Jean V Adams
Try this:

q - z[match(p, y)]

Jean


`·.,,  (((º   `·.,,  (((º   `·.,,  (((º

Jean V. Adams
Statistician
U.S. Geological Survey
Great Lakes Science Center
223 East Steinfest Road
Antigo, WI 54409  USA
715-627-4317, ext. 3125  (Office)
715-216-8014  (Cell)
715-623-6773  (FAX)
http://www.glsc.usgs.gov  (GLSC web site)
http://profile.usgs.gov/jvadams  (My homepage)
jvad...@usgs.gov  (E-mail)




From:
testrider testri...@gmail.com
To:
r-help@r-project.org
Date:
08/04/2011 07:21 AM
Subject:
[R] R loop problem
Sent by:
r-help-boun...@r-project.org



I have run into a speed issue, and given the size of the problem it feels
like there should be an easy solution. Here is the problem statement with
some arbitrary numbers added.

#p,q: vector with length(q)==length(p)==1 and length(levels(p))==3000
#y,z: vectors with length(levels(y))=length(y)==length(z)==5000

for (i in levels(p)){
q[i==p]-z[i==y]}

At first i used two for loops which was horrible, now i got rid of one but 
i
don't know how to lose the second one.


PS. I expect the solution to be available through google etc and I have
searched for a solution but i did not find any usefull websites, probably
because i cannot pinpoint the best search words.

--
View this message in context: 
http://r.789695.n4.nabble.com/R-loop-problem-tp3718103p3718103.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
and provide commented, minimal, self-contained, reproducible code.



[[alternative HTML version deleted]]

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] R loop problem

2011-08-04 Thread testrider
Worked like a charm! 
Thanks a lot Jean V Adams

--
View this message in context: 
http://r.789695.n4.nabble.com/R-loop-problem-tp3718449p3718670.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
and provide commented, minimal, self-contained, reproducible code.


[R] loop problem

2011-02-10 Thread Rocio Ponce
Hi there,
I'm new working with R and I am trying to find the unchosen set of islands
that are closer to all the possible combination (of 2 out of 13) of a set of
chosen islands.
In other words, if I have a set of 13 islands (1, 2, ...13) and choose all
the possible combination of 2 islands (1-2, 1-3,...12-13) for each chosen
pair of islands which of the unchosen islands are the closest.

My code is below, but the problem is the loops seem not to be  working

library(gtools)
library(session

n=length(a)


closeout=matrix(NA,nrow=78, ncol=2)

for (i in 2){
x-combinations(13,i) #considering these i islands

others=matrix(nrow=78, ncol=2)
for (j in 1:78){ #for all combinations of i islands
acopy=a #make a copy of a for manipulation
a-as.matrix(a)

#finding out which islands are selected and removing them from consideration
others=paste('(acopy==',x[j,1],sep='')

for(islands in 1:2){
others=paste(others,' |acopy==',x[j,islands],sep='')
others=paste(others,')',sep='')
}
acopy[eval(parse(text=others))]=NA

#loop through each island in this set of islands and get closest island, but
exclude islands already counted
for(k in 1:i){
closeout[i,1]=a[x[j,],is.finite(as.numeric(a[x[j,k],]))][1]
closeout[i,2]=a[x[j,k],is.finite(as.numeric(a[x[j,k],]))][1]

acopy[a==closeout[i,]]=NA
}
}
write.csv(closeout, paste('res_closest',i,sep='','.csv'))

}

#
The data of which island is closer to which is:
a
 V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12
 [1,]  8  2  4  9 13  3 11 12 10   5   6   7
 [2,]  1  8  4  9 13  3 11 12 10   5   6   7
 [3,] 13 11  9 12  5 10  4  6  7   8   1   2
 [4,]  9  8 13  3 11  1 12 10  5   2   6   7
 [5,] 12  6  3 10 11  7 13  9  4   8   1   2
 [6,]  7 10  5 12 11  3 13  9  4   8   1   2
 [7,]  6 10  5 12 11  3 13  9  4   8   1   2
 [8,]  1  4  2  9 13  3 11 12 10   5   6   7
 [9,] 13  3  4 11 12 10  5  8  6   1   7   2
[10,] 12 11 13  6  5  3  7  9  4   8   1   2
[11,] 13 12 10  3  9  5  6  4  7   8   1   2
[12,] 10 11 13  3  5  6  9  7  4   8   1   2
[13,]  9 11  3 12 10  4  5  6  7   8   1   2

Each row is an island and column V1 is the closest island to the islands, V2
the 2nd closest islandV12 is the farthest island.

Any help provided would be greatly appreciated,

Rocio





-- 
Rocio Ponce-Reyes
PhD Candidate
The Ecology Center
University of Queensland
St. Lucia QLD 4072

[[alternative HTML version deleted]]

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] loop problem

2009-10-02 Thread crenial30

Thanks a lot David for your answer. I am sorry for being so minimal.

I wanted to produce a list/vector/table consisting each vector produced from
this code

len-20
for (n1 in seq(0,(len-1),by=1)){

f - R_event[R_event  (rx[1]+ n1*300) R_event = (rx[1] + 300*(n1+1))]
//create list for each values of n1

R_ev_char - as.character(f)

lis01 - c(length(R_ev_char)) // here I want to store the values of lengths
of R_ev_char vector for successive values of n1(0,1,2,3,...20)

I already tried with vector notation but no help.

I just want to store Lengths on each successive  integer so that I can draw
the list on 300(secs) interval.

Any suggestion would be very much helpful.
crenial


crenial30 wrote:
 
 I have the following code which I wanted to convert using for loop
 
 previous code:
 R_ev1 - R_event[R_event  (rx[1] + 300*0) R_event = (rx[1] + 300*1)]
 R_ev2 - R_event[R_event  (rx[1] + 300*1) R_event = (rx[1] + 300*2)]
 R_ev3 - R_event[R_event  (rx[1] + 300*2) R_event = (rx[1] + 300*3)]
 R_ev4 - R_event[R_event  (rx[1] + 300*3) R_event = (rx[1] + 300*4)]
 
 R_ev5 - R_event[R_event  (rx[1] + 300*4) R_event = (rx[1] + 300*5)]
 R_ev6 - R_event[R_event  (rx[1] + 300*5) R_event = (rx[1] + 300*6)]
 R_ev7 - R_event[R_event  (rx[1] + 300*6) R_event = (rx[1] + 300*7)]
 R_ev8 - R_event[R_event  (rx[1] + 300*7) R_event = (rx[1] + 300*8)]
 
 R_ev9 - R_event[R_event   (rx[1] + 300*8) R_event = (rx[1] + 300*9)]
 R_ev10 - R_event[R_event  (rx[1] + 300*9) R_event = (rx[1] + 300*10)]
 R_ev11 - R_event[R_event  (rx[1] + 300*10) R_event = (rx[1] + 300*11)]
 R_ev12 - R_event[R_event  (rx[1] + 300*11) R_event = (rx[1] + 300*12)]
 
 R_ev13 - R_event[R_event  (rx[1] + 300*12) R_event = (rx[1] + 300*13)]
 R_ev14 - R_event[R_event  (rx[1] + 300*13) R_event = (rx[1] + 300*14)]
 R_ev15 - R_event[R_event  (rx[1] + 300*14) R_event = (rx[1] + 300*15)]
 R_ev16 - R_event[R_event  (rx[1] + 300*15) R_event = (rx[1] + 300*16)]
 
 
 R_ev1c - as.character(R_ev1)
 R_ev2c - as.character(R_ev2)
 R_ev3c - as.character(R_ev3)
 R_ev4c - as.character(R_ev4)
 R_ev5c - as.character(R_ev5)
 R_ev6c - as.character(R_ev6)
 R_ev7c - as.character(R_ev7)
 R_ev8c - as.character(R_ev8)
 
 R_ev9c  - as.character(R_ev9)
 R_ev10c - as.character(R_ev10)
 R_ev11c - as.character(R_ev11)
 R_ev12c - as.character(R_ev12)
 R_ev13c - as.character(R_ev13)
 R_ev14c - as.character(R_ev14)
 R_ev15c - as.character(R_ev15)
 R_ev16c - as.character(R_ev16)
 
 
 
 
 lis1 - c(
 length(R_ev1c),
 length(R_ev2c),
 length(R_ev3c),
 length(R_ev4c),
 length(R_ev5c),
 length(R_ev6c),
 length(R_ev7c),
 length(R_ev8c),
 length(R_ev9c),
 length(R_ev10c),
 length(R_ev11c),
 length(R_ev12c),
 length(R_ev13c),
 length(R_ev14c),
 length(R_ev15c),
 length(R_ev16c)
 )
 
 
 # R_ev1_mat - matrix(unlist(R_ev1),nrow=T)
 
 
 barplot(lis1, angle = 15+10*1:5, density = 20, col = black,
legend = rownames(lis1))
  title(main = list(Number of Occurred Received events in every 5 mins,
 font = 4))
 
 # dev.off()
 
 
 New code:
 
 f1- seq(rx[1],rx[2],by=300)
 f- as.character(f1)
 len - length(f)
 
 for (n1 in 0:(len-1)){
 for(n2 in 1:len){
 # R_ev_01 - R_event[R_event = (rx[1] + 300)]
 
 R_ev[n1] - R_event[R_event  (rx[1]+ n1*300) R_event = (rx[1] +
 300*n2)]
 R_ev_char - as.character(R_ev)
 
 lis01 - list(length(R_ev_char))
 barplot(lis01, angle = 15+10*1:5, density = 20, col = blue,
legend = rownames(lis01))
  title(main = list(Number of Occurred Received Events in every 5 mins,
 font = 4))
 }
 }
 
 Somehow it is not working and giving an error saying Fehler in -0.01 *
 height : nicht-numerisches Argument für binären Operator.
 
 Any help would be really appreciated.
 
 Thanks
 Crenial
 

-- 
View this message in context: 
http://www.nabble.com/loop-problem-tp25665329p25714305.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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] loop problem

2009-10-02 Thread David Winsemius


On Oct 2, 2009, at 7:34 AM, crenial30 wrote:



Thanks a lot David for your answer. I am sorry for being so minimal.

I wanted to produce a list/vector/table consisting each vector  
produced from

this code

len-20
for (n1 in seq(0,(len-1),by=1)){

f - R_event[R_event  (rx[1]+ n1*300) R_event = (rx[1] +  
300*(n1+1))]

//create list for each values of n1


That throws an error:
f - R_event[R_event  (rx[1]+ n1*300) R_event = (rx[1] + 300*(n1+1))]
+ //create list for each values of n1
Error: syntax error

Plus... even if it were syntactically correct, we are not going to  
know what R_event is.


Try posting code that creates a data situation and then express in  
English (not mangled R) what you want to happen.





R_ev_char - as.character(f)

lis01 - c(length(R_ev_char)) // here I want to store the values of  
lengths

of R_ev_char vector for successive values of n1(0,1,2,3,...20)

I already tried with vector notation but no help.

I just want to store Lengths on each successive  integer so that I  
can draw

the list on 300(secs) interval.

Any suggestion would be very much helpful.
crenial


crenial30 wrote:


I have the following code which I wanted to convert using for loop

previous code:
R_ev1 - R_event[R_event  (rx[1] + 300*0) R_event = (rx[1] +  
300*1)]
R_ev2 - R_event[R_event  (rx[1] + 300*1) R_event = (rx[1] +  
300*2)]
R_ev3 - R_event[R_event  (rx[1] + 300*2) R_event = (rx[1] +  
300*3)]
R_ev4 - R_event[R_event  (rx[1] + 300*3) R_event = (rx[1] +  
300*4)]


R_ev5 - R_event[R_event  (rx[1] + 300*4) R_event = (rx[1] +  
300*5)]
R_ev6 - R_event[R_event  (rx[1] + 300*5) R_event = (rx[1] +  
300*6)]
R_ev7 - R_event[R_event  (rx[1] + 300*6) R_event = (rx[1] +  
300*7)]
R_ev8 - R_event[R_event  (rx[1] + 300*7) R_event = (rx[1] +  
300*8)]


R_ev9 - R_event[R_event   (rx[1] + 300*8) R_event = (rx[1] +  
300*9)]
R_ev10 - R_event[R_event  (rx[1] + 300*9) R_event = (rx[1] +  
300*10)]
R_ev11 - R_event[R_event  (rx[1] + 300*10) R_event = (rx[1] +  
300*11)]
R_ev12 - R_event[R_event  (rx[1] + 300*11) R_event = (rx[1] +  
300*12)]


R_ev13 - R_event[R_event  (rx[1] + 300*12) R_event = (rx[1] +  
300*13)]
R_ev14 - R_event[R_event  (rx[1] + 300*13) R_event = (rx[1] +  
300*14)]
R_ev15 - R_event[R_event  (rx[1] + 300*14) R_event = (rx[1] +  
300*15)]
R_ev16 - R_event[R_event  (rx[1] + 300*15) R_event = (rx[1] +  
300*16)]



R_ev1c - as.character(R_ev1)
R_ev2c - as.character(R_ev2)
R_ev3c - as.character(R_ev3)
R_ev4c - as.character(R_ev4)
R_ev5c - as.character(R_ev5)
R_ev6c - as.character(R_ev6)
R_ev7c - as.character(R_ev7)
R_ev8c - as.character(R_ev8)

R_ev9c  - as.character(R_ev9)
R_ev10c - as.character(R_ev10)
R_ev11c - as.character(R_ev11)
R_ev12c - as.character(R_ev12)
R_ev13c - as.character(R_ev13)
R_ev14c - as.character(R_ev14)
R_ev15c - as.character(R_ev15)
R_ev16c - as.character(R_ev16)




lis1 - c(
length(R_ev1c),
length(R_ev2c),
length(R_ev3c),
length(R_ev4c),
length(R_ev5c),
length(R_ev6c),
length(R_ev7c),
length(R_ev8c),
length(R_ev9c),
length(R_ev10c),
length(R_ev11c),
length(R_ev12c),
length(R_ev13c),
length(R_ev14c),
length(R_ev15c),
length(R_ev16c)
)


# R_ev1_mat - matrix(unlist(R_ev1),nrow=T)


barplot(lis1, angle = 15+10*1:5, density = 20, col = black,
  legend = rownames(lis1))
title(main = list(Number of Occurred Received events in every 5  
mins,

font = 4))

# dev.off()


New code:

f1- seq(rx[1],rx[2],by=300)
f- as.character(f1)
len - length(f)

for (n1 in 0:(len-1)){
for(n2 in 1:len){
# R_ev_01 - R_event[R_event = (rx[1] + 300)]

R_ev[n1] - R_event[R_event  (rx[1]+ n1*300) R_event = (rx[1] +
300*n2)]
R_ev_char - as.character(R_ev)

lis01 - list(length(R_ev_char))
barplot(lis01, angle = 15+10*1:5, density = 20, col = blue,
  legend = rownames(lis01))
title(main = list(Number of Occurred Received Events in every 5  
mins,

font = 4))
}
}

Somehow it is not working and giving an error saying Fehler in  
-0.01 *

height : nicht-numerisches Argument für binären Operator.

Any help would be really appreciated.

Thanks
Crenial



--
View this message in context: 
http://www.nabble.com/loop-problem-tp25665329p25714305.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
and provide commented, minimal, self-contained, reproducible code.


__
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
and provide commented, minimal, self-contained, reproducible code.


[R] loop problem

2009-09-29 Thread crenial30

I have the following code which I wanted to convert using for loop

previous code:
R_ev1 - R_event[R_event  (rx[1] + 300*0) R_event = (rx[1] + 300*1)]
R_ev2 - R_event[R_event  (rx[1] + 300*1) R_event = (rx[1] + 300*2)]
R_ev3 - R_event[R_event  (rx[1] + 300*2) R_event = (rx[1] + 300*3)]
R_ev4 - R_event[R_event  (rx[1] + 300*3) R_event = (rx[1] + 300*4)]

R_ev5 - R_event[R_event  (rx[1] + 300*4) R_event = (rx[1] + 300*5)]
R_ev6 - R_event[R_event  (rx[1] + 300*5) R_event = (rx[1] + 300*6)]
R_ev7 - R_event[R_event  (rx[1] + 300*6) R_event = (rx[1] + 300*7)]
R_ev8 - R_event[R_event  (rx[1] + 300*7) R_event = (rx[1] + 300*8)]

R_ev9 - R_event[R_event   (rx[1] + 300*8) R_event = (rx[1] + 300*9)]
R_ev10 - R_event[R_event  (rx[1] + 300*9) R_event = (rx[1] + 300*10)]
R_ev11 - R_event[R_event  (rx[1] + 300*10) R_event = (rx[1] + 300*11)]
R_ev12 - R_event[R_event  (rx[1] + 300*11) R_event = (rx[1] + 300*12)]

R_ev13 - R_event[R_event  (rx[1] + 300*12) R_event = (rx[1] + 300*13)]
R_ev14 - R_event[R_event  (rx[1] + 300*13) R_event = (rx[1] + 300*14)]
R_ev15 - R_event[R_event  (rx[1] + 300*14) R_event = (rx[1] + 300*15)]
R_ev16 - R_event[R_event  (rx[1] + 300*15) R_event = (rx[1] + 300*16)]


R_ev1c - as.character(R_ev1)
R_ev2c - as.character(R_ev2)
R_ev3c - as.character(R_ev3)
R_ev4c - as.character(R_ev4)
R_ev5c - as.character(R_ev5)
R_ev6c - as.character(R_ev6)
R_ev7c - as.character(R_ev7)
R_ev8c - as.character(R_ev8)

R_ev9c  - as.character(R_ev9)
R_ev10c - as.character(R_ev10)
R_ev11c - as.character(R_ev11)
R_ev12c - as.character(R_ev12)
R_ev13c - as.character(R_ev13)
R_ev14c - as.character(R_ev14)
R_ev15c - as.character(R_ev15)
R_ev16c - as.character(R_ev16)




lis1 - c(
length(R_ev1c),
length(R_ev2c),
length(R_ev3c),
length(R_ev4c),
length(R_ev5c),
length(R_ev6c),
length(R_ev7c),
length(R_ev8c),
length(R_ev9c),
length(R_ev10c),
length(R_ev11c),
length(R_ev12c),
length(R_ev13c),
length(R_ev14c),
length(R_ev15c),
length(R_ev16c)
)


# R_ev1_mat - matrix(unlist(R_ev1),nrow=T)


barplot(lis1, angle = 15+10*1:5, density = 20, col = black,
   legend = rownames(lis1))
 title(main = list(Number of Occurred Received events in every 5 mins,
font = 4))

# dev.off()


New code:

f1- seq(rx[1],rx[2],by=300)
f- as.character(f1)
len - length(f)

for (n1 in 0:(len-1)){
for(n2 in 1:len){
# R_ev_01 - R_event[R_event = (rx[1] + 300)]

R_ev[n1] - R_event[R_event  (rx[1]+ n1*300) R_event = (rx[1] + 300*n2)]
R_ev_char - as.character(R_ev)

lis01 - list(length(R_ev_char))
barplot(lis01, angle = 15+10*1:5, density = 20, col = blue,
   legend = rownames(lis01))
 title(main = list(Number of Occurred Received Events in every 5 mins,
font = 4))
}
}

Somehow it is not working and giving an error saying Fehler in -0.01 *
height : nicht-numerisches Argument für binären Operator.

Any help would be really appreciated.

Thanks
Crenial
-- 
View this message in context: 
http://www.nabble.com/loop-problem-tp25665329p25665329.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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] loop problem

2009-09-29 Thread David Winsemius
That code is way more than a _minimal_ example, and its not  
_reproducible_ either, so just a comment:


Have you considered creating a vector rather than separate event  
variables?


R_ev[1:16] - R_event  (rx[1] + 300*(0:15) R_event = (rx[1] +  
300*(1:16)


?cut   # would also appear to be a function that might offer advantages

--
David

On Sep 29, 2009, at 11:34 AM, crenial30 wrote:



I have the following code which I wanted to convert using for loop

previous code:
R_ev1 - R_event[R_event  (rx[1] + 300*0) R_event = (rx[1] +  
300*1)]
R_ev2 - R_event[R_event  (rx[1] + 300*1) R_event = (rx[1] +  
300*2)]
R_ev3 - R_event[R_event  (rx[1] + 300*2) R_event = (rx[1] +  
300*3)]
R_ev4 - R_event[R_event  (rx[1] + 300*3) R_event = (rx[1] +  
300*4)]


R_ev5 - R_event[R_event  (rx[1] + 300*4) R_event = (rx[1] +  
300*5)]
R_ev6 - R_event[R_event  (rx[1] + 300*5) R_event = (rx[1] +  
300*6)]
R_ev7 - R_event[R_event  (rx[1] + 300*6) R_event = (rx[1] +  
300*7)]
R_ev8 - R_event[R_event  (rx[1] + 300*7) R_event = (rx[1] +  
300*8)]


R_ev9 - R_event[R_event   (rx[1] + 300*8) R_event = (rx[1] +  
300*9)]
R_ev10 - R_event[R_event  (rx[1] + 300*9) R_event = (rx[1] +  
300*10)]
R_ev11 - R_event[R_event  (rx[1] + 300*10) R_event = (rx[1] +  
300*11)]
R_ev12 - R_event[R_event  (rx[1] + 300*11) R_event = (rx[1] +  
300*12)]


R_ev13 - R_event[R_event  (rx[1] + 300*12) R_event = (rx[1] +  
300*13)]
R_ev14 - R_event[R_event  (rx[1] + 300*13) R_event = (rx[1] +  
300*14)]
R_ev15 - R_event[R_event  (rx[1] + 300*14) R_event = (rx[1] +  
300*15)]
R_ev16 - R_event[R_event  (rx[1] + 300*15) R_event = (rx[1] +  
300*16)]



R_ev1c - as.character(R_ev1)
R_ev2c - as.character(R_ev2)
R_ev3c - as.character(R_ev3)
R_ev4c - as.character(R_ev4)
R_ev5c - as.character(R_ev5)
R_ev6c - as.character(R_ev6)
R_ev7c - as.character(R_ev7)
R_ev8c - as.character(R_ev8)

R_ev9c  - as.character(R_ev9)
R_ev10c - as.character(R_ev10)
R_ev11c - as.character(R_ev11)
R_ev12c - as.character(R_ev12)
R_ev13c - as.character(R_ev13)
R_ev14c - as.character(R_ev14)
R_ev15c - as.character(R_ev15)
R_ev16c - as.character(R_ev16)




lis1 - c(
length(R_ev1c),
length(R_ev2c),
length(R_ev3c),
length(R_ev4c),
length(R_ev5c),
length(R_ev6c),
length(R_ev7c),
length(R_ev8c),
length(R_ev9c),
length(R_ev10c),
length(R_ev11c),
length(R_ev12c),
length(R_ev13c),
length(R_ev14c),
length(R_ev15c),
length(R_ev16c)
)


# R_ev1_mat - matrix(unlist(R_ev1),nrow=T)


barplot(lis1, angle = 15+10*1:5, density = 20, col = black,
  legend = rownames(lis1))
title(main = list(Number of Occurred Received events in every 5  
mins,

font = 4))

# dev.off()


New code:

f1- seq(rx[1],rx[2],by=300)
f- as.character(f1)
len - length(f)

for (n1 in 0:(len-1)){
for(n2 in 1:len){
# R_ev_01 - R_event[R_event = (rx[1] + 300)]

R_ev[n1] - R_event[R_event  (rx[1]+ n1*300) R_event = (rx[1] +  
300*n2)]

R_ev_char - as.character(R_ev)

lis01 - list(length(R_ev_char))
barplot(lis01, angle = 15+10*1:5, density = 20, col = blue,
  legend = rownames(lis01))
title(main = list(Number of Occurred Received Events in every 5  
mins,

font = 4))
}
}

Somehow it is not working and giving an error saying Fehler in  
-0.01 *

height : nicht-numerisches Argument für binären Operator.

Any help would be really appreciated.

Thanks
Crenial
--
View this message in context: 
http://www.nabble.com/loop-problem-tp25665329p25665329.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
and provide commented, minimal, self-contained, reproducible code.


David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] loop problem for extract coefficients

2009-04-07 Thread Arien Lam
Hi Alex,

On Sun, April 5, 2009 16:49, Alex Roy wrote:
 Dear R users,
   I have problem with extracting coefficients from a
 object. Here, X (predictor)and Y (response) are two matrix , I am
 regressing
 X ( dimensions 10 x 20) on each of columns of Y[,1] (10 x 1)  and want to
 store the coefficient values. I have performed a Elastic Net regression
 and
 I want to store the coeffcients in each iteration. I got an error message
 .
 I do not know where is the problem Please help me.

 Thanks



 *Code:*

 ---
 library(elasticnet)
 X-matrix(rnorm(200),ncol=20)
 Y-matrix(rnorm(200),ncol=20)
 loop - 20
 size - 20
 enres-matrix(nrow = size, ncol = loop)
 fit-matrix(nrow = size, ncol = loop)
 store-matrix(nrow = size, ncol = loop)
 for(j in 1: 10)
 print (paste(j,/200,sep=))
 {
 enres-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE)
 fit-predict.enet(enres, X, type=coefficients)
 store[,j]-fit$coefficients
 }
 

The problem is that only the print statement is inside the for loop. I
would suggest:

for(j in 1: 10)
{ # here the opening bracket
print (paste(j,/200,sep=))
  # and not here!
enres-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE)
fit-predict.enet(enres, X, type=coefficients)
store[,j]-fit$coefficients
}


Hope this helps,
Arien



 library(elasticnet)
 Loading required package: lars
 X-matrix(rnorm(200),ncol=20)
 Y-matrix(rnorm(200),ncol=20)

 loop - 20
 size - 20

 enres-matrix(nrow = size, ncol = loop)
 fit-matrix(nrow = size, ncol = loop)
 store-matrix(nrow = size, ncol = loop)

 for(j in 1: 10)
 + print (paste(j,/200,sep=))
 [1] 1/200
 [1] 2/200
 [1] 3/200
 [1] 4/200
 [1] 5/200
 [1] 6/200
 [1] 7/200
 [1] 8/200
 [1] 9/200
 [1] 10/200
 {
 + enres-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE)
 + fit-predict.enet(enres, X, type=coefficients)
 + store[,j]-fit$coefficients
 + }
 *Error in store[, j] - fit$coefficients :
   number of items to replace is not a multiple of replacement length
 *

   [[alternative HTML version deleted]]

 __
 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
 and provide commented, minimal, self-contained, reproducible code.



-- 
drs. H.A. (Arien) Lam (Ph.D. student)
Department of Physical Geography
Faculty of Geosciences
Utrecht University, The Netherlands

__
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
and provide commented, minimal, self-contained, reproducible code.


[R] loop problem for extract coefficients

2009-04-05 Thread Alex Roy
Dear R users,
  I have problem with extracting coefficients from a
object. Here, X (predictor)and Y (response) are two matrix , I am regressing
X ( dimensions 10 x 20) on each of columns of Y[,1] (10 x 1)  and want to
store the coefficient values. I have performed a Elastic Net regression and
I want to store the coeffcients in each iteration. I got an error message .
I do not know where is the problem Please help me.

Thanks



*Code:*

---
library(elasticnet)
X-matrix(rnorm(200),ncol=20)
Y-matrix(rnorm(200),ncol=20)
loop - 20
size - 20
enres-matrix(nrow = size, ncol = loop)
fit-matrix(nrow = size, ncol = loop)
store-matrix(nrow = size, ncol = loop)
for(j in 1: 10)
print (paste(j,/200,sep=))
{
enres-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE)
fit-predict.enet(enres, X, type=coefficients)
store[,j]-fit$coefficients
}


 library(elasticnet)
Loading required package: lars
 X-matrix(rnorm(200),ncol=20)
 Y-matrix(rnorm(200),ncol=20)

 loop - 20
 size - 20

 enres-matrix(nrow = size, ncol = loop)
 fit-matrix(nrow = size, ncol = loop)
 store-matrix(nrow = size, ncol = loop)

 for(j in 1: 10)
+ print (paste(j,/200,sep=))
[1] 1/200
[1] 2/200
[1] 3/200
[1] 4/200
[1] 5/200
[1] 6/200
[1] 7/200
[1] 8/200
[1] 9/200
[1] 10/200
 {
+ enres-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE)
+ fit-predict.enet(enres, X, type=coefficients)
+ store[,j]-fit$coefficients
+ }
*Error in store[, j] - fit$coefficients :
  number of items to replace is not a multiple of replacement length
 *

[[alternative HTML version deleted]]

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] loop problem for extract coefficients

2009-04-05 Thread Bill.Venables
Perhaps your loop should be more than just a print statement.  That works fine! 
 You need to place the print statement after the '{', not before it.

fit$coefficients is a 21 x 20 array (the rows are lablelled 0 to 20) and you 
are trying to put it in the jth *column* of a 20 x 20 matrix.  Not surprisingly 
it does not fit.

Perhaps you need to know more about just what it is this software is doing. 


Bill Venables
http://www.cmis.csiro.au/bill.venables/ 


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Alex Roy
Sent: Monday, 6 April 2009 12:49 AM
To: r-help@r-project.org
Subject: [R] loop problem for extract coefficients

Dear R users,
  I have problem with extracting coefficients from a
object. Here, X (predictor)and Y (response) are two matrix , I am regressing
X ( dimensions 10 x 20) on each of columns of Y[,1] (10 x 1)  and want to
store the coefficient values. I have performed a Elastic Net regression and
I want to store the coeffcients in each iteration. I got an error message .
I do not know where is the problem Please help me.

Thanks



*Code:*

---
library(elasticnet)
X-matrix(rnorm(200),ncol=20)
Y-matrix(rnorm(200),ncol=20)
loop - 20
size - 20
enres-matrix(nrow = size, ncol = loop)
fit-matrix(nrow = size, ncol = loop)
store-matrix(nrow = size, ncol = loop)
for(j in 1: 10)
print (paste(j,/200,sep=))
{
enres-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE)
fit-predict.enet(enres, X, type=coefficients)
store[,j]-fit$coefficients
}


 library(elasticnet)
Loading required package: lars
 X-matrix(rnorm(200),ncol=20)
 Y-matrix(rnorm(200),ncol=20)

 loop - 20
 size - 20

 enres-matrix(nrow = size, ncol = loop)
 fit-matrix(nrow = size, ncol = loop)
 store-matrix(nrow = size, ncol = loop)

 for(j in 1: 10)
+ print (paste(j,/200,sep=))
[1] 1/200
[1] 2/200
[1] 3/200
[1] 4/200
[1] 5/200
[1] 6/200
[1] 7/200
[1] 8/200
[1] 9/200
[1] 10/200
 {
+ enres-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE)
+ fit-predict.enet(enres, X, type=coefficients)
+ store[,j]-fit$coefficients
+ }
*Error in store[, j] - fit$coefficients :
  number of items to replace is not a multiple of replacement length
 *

[[alternative HTML version deleted]]

__
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
and provide commented, minimal, self-contained, reproducible code.

__
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
and provide commented, minimal, self-contained, reproducible code.


[R] loop problem

2009-03-26 Thread Muhammad Azam
Dear R members
I have a problem regarding storing the lists.
Let 
L=number of distinct values of any predictor (say L=5)
P=number of predictors (say P=20)

g1 - c()
for(i in 1:P){
if(L  1){
  for(j in 1:(L-1)){
g - 
g1[j] - g
   }
}
g2[]=sort.list(g1)
}

Now the question is: What should we use inside brackets of g2[], whether 
i or some thing else? If L is not greater than 1 then there will be a NULL 
for g2. We don't want to store it in g2, so how can we handle this problem. 
Looking forward for some help. Thanks and 

 
best regards

Muhammad Azam 



  
[[alternative HTML version deleted]]

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] loop problem

2009-03-26 Thread Jim Lemon

Muhammad Azam wrote:

Dear R members
I have a problem regarding storing the lists.
Let 
L=number of distinct values of any predictor (say L=5)

P=number of predictors (say P=20)

g1 - c()
for(i in 1:P){
if(L  1){
  for(j in 1:(L-1)){
g - 
g1[j] - g
   }
}
g2[]=sort.list(g1)
}

Now the question is: What should we use inside brackets of g2[], whether i or some thing else? If L is not greater than 1 then there will be a NULL for g2. We don't want to store it in g2, so how can we handle this problem. Looking forward for some help. Thanks and 
  

Hi Muhammad,
The first thing I would ask is Why not store NULL in g2[[i]] if that 
element is NULL?. If you do this:


for(i in 1:P) {
if(L  1) {
 g1-rep(NA,L)
 for(j in 1:(L-1)) g1[j]-...
 g2[[i]]-sort.list(g1)
}
}

you will have fewer elements in g2 than you have sets of predictors and 
will then have to match up the sets of predictors with the elements in 
g2. I think you could just test for NULL


is.null(g2[[i]])

later on if you don't want to process these in a subsequent step.

Jim

__
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
and provide commented, minimal, self-contained, reproducible code.


[R] Loop problem

2008-07-14 Thread fernanda lopez
Dear all,
 I want to
write   ck-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4))   state ment
in a loop . How can I write it ?





 pk
   [,1]  [,2]   [,3]
[1,] -1.1354816 0.9808877 -0.9446314
[2,]  0.7787378 0.4536944  0.3204882
[3,] -1.7274907 1.5112011  1.4481839
[4,]  1.0629145 0.5976109 -0.5277638



 pk-matrix(rnorm(12),nrow=4,ncol=3)
 pk
   [,1]  [,2]   [,3]
[1,] -1.1354816 0.9808877 -0.9446314
[2,]  0.7787378 0.4536944  0.3204882
[3,] -1.7274907 1.5112011  1.4481839
[4,]  1.0629145 0.5976109 -0.5277638

 ck-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4))
 ck
  [,1]  [,2]   [,3]
[1,] -1.135482 0.9808877 -0.9446314
[2,] -1.135482 0.9808877 -0.9446314
[3,] -1.135482 0.9808877 -0.9446314
[4,] -1.135482 0.9808877 -0.9446314


Thanks for your help

Fernanda Lopez
Netherlands

[[alternative HTML version deleted]]

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Loop problem

2008-07-14 Thread Dimitris Rizopoulos

try this:

pk - matrix(rnorm(12), 4, 3)
matrix(rep(pk[1, ], each = 4), 4)


I hope it helps.

Best,
Dimitris


Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm


- Original Message - 
From: fernanda lopez [EMAIL PROTECTED]

To: [EMAIL PROTECTED]
Sent: Monday, July 14, 2008 10:33 AM
Subject: [R] Loop problem



Dear all,
I want to
write   ck-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4)) 
state ment

in a loop . How can I write it ?






pk

  [,1]  [,2]   [,3]
[1,] -1.1354816 0.9808877 -0.9446314
[2,]  0.7787378 0.4536944  0.3204882
[3,] -1.7274907 1.5112011  1.4481839
[4,]  1.0629145 0.5976109 -0.5277638




pk-matrix(rnorm(12),nrow=4,ncol=3)
pk

  [,1]  [,2]   [,3]
[1,] -1.1354816 0.9808877 -0.9446314
[2,]  0.7787378 0.4536944  0.3204882
[3,] -1.7274907 1.5112011  1.4481839
[4,]  1.0629145 0.5976109 -0.5277638


ck-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4))
ck

 [,1]  [,2]   [,3]
[1,] -1.135482 0.9808877 -0.9446314
[2,] -1.135482 0.9808877 -0.9446314
[3,] -1.135482 0.9808877 -0.9446314
[4,] -1.135482 0.9808877 -0.9446314


Thanks for your help

Fernanda Lopez
Netherlands

[[alternative HTML version deleted]]

__
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

and provide commented, minimal, self-contained, reproducible code.




Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Loop problem

2008-07-14 Thread Daniel Malter

Hi, do you mean this?

pk-matrix(rnorm(12),nrow=4,ncol=3)
ck1-cbind(rep(pk[,1],4),rep(pk[,2],4),rep(pk[,3],4)) ## or
ck2-cbind(rep(pk[,1],each=4),rep(pk[,2],each=4),rep(pk[,3],each=4))

pk
ck1
ck2

best,
Daniel 




fernanda lopez wrote:
 
 Dear all,
  I want to
 write   ck-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4))   state
 ment
 in a loop . How can I write it ?
 
 
 
 
 
 pk
[,1]  [,2]   [,3]
 [1,] -1.1354816 0.9808877 -0.9446314
 [2,]  0.7787378 0.4536944  0.3204882
 [3,] -1.7274907 1.5112011  1.4481839
 [4,]  1.0629145 0.5976109 -0.5277638
 
 
 
 pk-matrix(rnorm(12),nrow=4,ncol=3)
 pk
[,1]  [,2]   [,3]
 [1,] -1.1354816 0.9808877 -0.9446314
 [2,]  0.7787378 0.4536944  0.3204882
 [3,] -1.7274907 1.5112011  1.4481839
 [4,]  1.0629145 0.5976109 -0.5277638
 
 ck-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4))
 ck
   [,1]  [,2]   [,3]
 [1,] -1.135482 0.9808877 -0.9446314
 [2,] -1.135482 0.9808877 -0.9446314
 [3,] -1.135482 0.9808877 -0.9446314
 [4,] -1.135482 0.9808877 -0.9446314
 
 
 Thanks for your help
 
 Fernanda Lopez
 Netherlands
 
   [[alternative HTML version deleted]]
 
 __
 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
 and provide commented, minimal, self-contained, reproducible code.
 
 

-- 
View this message in context: 
http://www.nabble.com/Loop-problem-tp18439649p18439918.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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Loop problem

2008-07-14 Thread Gabor Grothendieck
Try this:

pk[rep(1, 4), ]


On Mon, Jul 14, 2008 at 4:33 AM, fernanda lopez [EMAIL PROTECTED] wrote:
 Dear all,
 I want to
 write   ck-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4))   state ment
 in a loop . How can I write it ?





 pk
   [,1]  [,2]   [,3]
 [1,] -1.1354816 0.9808877 -0.9446314
 [2,]  0.7787378 0.4536944  0.3204882
 [3,] -1.7274907 1.5112011  1.4481839
 [4,]  1.0629145 0.5976109 -0.5277638



 pk-matrix(rnorm(12),nrow=4,ncol=3)
 pk
   [,1]  [,2]   [,3]
 [1,] -1.1354816 0.9808877 -0.9446314
 [2,]  0.7787378 0.4536944  0.3204882
 [3,] -1.7274907 1.5112011  1.4481839
 [4,]  1.0629145 0.5976109 -0.5277638

 ck-cbind(rep(pk[1,1],4),rep(pk[1,2],4),rep(pk[1,3],4))
 ck
  [,1]  [,2]   [,3]
 [1,] -1.135482 0.9808877 -0.9446314
 [2,] -1.135482 0.9808877 -0.9446314
 [3,] -1.135482 0.9808877 -0.9446314
 [4,] -1.135482 0.9808877 -0.9446314


 Thanks for your help

 Fernanda Lopez
 Netherlands

[[alternative HTML version deleted]]

 __
 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
 and provide commented, minimal, self-contained, reproducible code.


__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Loop problem

2008-03-27 Thread Jim Lemon
Jamie Ledingham wrote:
 Dear all, I have a problem with a loop, if anyone has any knowledge on
 these things I would appreciate some comments.  The code below is
 designed to allow me to extract the top record of the data frame, and
 them remove rows from the data frame which have an index close to the
 extracted top record.
 
 
 topstorm-subset(rankeddataset[1,]) ## Extracts the top storm
 topstormindex-rankeddataset[1,1] ## Finds the top storm start index
 startindex-topstormindex-48 ## sets the start and end indexes
 endindex-topstorminde+48
 rankeddataset-rankeddataset[-1,] ## Creates a new list with the top
 storm removed
 
 ##This section of code needs looped.  It removes storms from the list
 which are too close to the extracted storm
 
 for (i in 1:30){
 if (rankeddataset[i,1]startindex  rankeddataset[i,1]endindex)
 {rankeddataset-rankeddataset[-i,]}
 }
 
 Here is some example data:
 
82856  15 / 6 / 1966   82856:8287925.9
82857  15 / 6 / 1966   82857:8288020.5
83036  23 / 6 / 1966   83036:8305917.3
87250 15 / 12 / 1966   87250:8727315.9
 
Hi again Jamie,
I had a bit of time tonight and recognized your problem. I think the 
following function will do what you want, although it is probably not 
the most elegant solution. I would check it manually with a small data 
file and a small howmany argument to make sure that it is picking up 
the maxima you want.

find.max.rain-function(raindata,howmany) {
  # a lazy way of getting the same structure as raindata
  # it assumes that raindata has the data structure that you want
  maxrain-raindata[1:howmany,]
  for(i in 1:howmany) {
   # get the current number of rows
   nrows-dim(raindata)[1]
   # find the first maximum value
   thismax-which.max(raindata$cumrain)
   # transfer it to the return data frame
   maxrain[i,]-raindata[thismax,]
   # calculate the minimum index for the 48 hour gap
   mindex-thismax-48
   # make sure it is at least 1
   if(mindex  1) mindex - 1
   # calculate the maximum index for the gap
   maxdex-thismax+48
   # make sure it doesn't go past the end of the data frame
   if(maxdex  nrows) maxdex-nrows
   # chop out that time period
   raindata-raindata[-(mindex:maxdex),]
  }
  return(maxrain)
}

Jim

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Loop problem

2008-03-27 Thread Jim Lemon
Oops, missed the first line of the example:

storm.data-data.frame(meas.index=10001:2,cumrain=runif(1,-10,10)+10)

You can generalize the function to whatever column names you have like this:

find.max.rain-function(raindata,howmany,raincol) {
  # a lazy way of getting the same structure as raindata
  # it assumes that raindata has the data structure that you want
  maxrain-raindata[1:howmany,]
  for(i in 1:howmany) {
   raindim-dim(raindata)
   thismax-which.max(raindata[[raincol]])
   cat(thismax, )
   maxrain[i,]-raindata[thismax,]
   mindex-thismax-48
   if(mindex  1) mindex - 1
   maxdex-thismax+48
   cat(mindex,maxdex, )
   if(maxdex  raindim[1]) maxdex-raindim[1]
   raindata-raindata[-(mindex:maxdex),]
   cat(raindim[1],  )
  }
  cat(\n)
  return(maxrain)
}

find.max.rain(storm.data,50,cumrain)

Jim

__
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
and provide commented, minimal, self-contained, reproducible code.


[R] Loop problem

2008-03-26 Thread Jamie Ledingham
Dear all, I have a problem with a loop, if anyone has any knowledge on
these things I would appreciate some comments.  The code below is
designed to allow me to extract the top record of the data frame, and
them remove rows from the data frame which have an index close to the
extracted top record.


topstorm-subset(rankeddataset[1,]) ## Extracts the top storm
topstormindex-rankeddataset[1,1] ## Finds the top storm start index
startindex-topstormindex-48 ## sets the start and end indexes
endindex-topstorminde+48
rankeddataset-rankeddataset[-1,] ## Creates a new list with the top
storm removed

##This section of code needs looped.  It removes storms from the list
which are too close to the extracted storm

for (i in 1:30){
if (rankeddataset[i,1]startindex  rankeddataset[i,1]endindex)
{rankeddataset-rankeddataset[-i,]}
}

Here is some example data:

   82856  15 / 6 / 1966   82856:8287925.9
   82857  15 / 6 / 1966   82857:8288020.5
   83036  23 / 6 / 1966   83036:8305917.3
   87250 15 / 12 / 1966   87250:8727315.9

The loop does not currently work, it seems to remove every second line
or so.  Can anyone suggest why this might be, I'm not particularly
experienced in using loops so it may be a rookie mistake.  Thanks in
advance.
Jamie Ledingham

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Loop problem

2008-03-26 Thread jim holtman
Basically you are moving the data up and then incrementing to the next
row.  Here is an example; assume that you are at the 2nd entry:

1
2 == here
3
4

Now your loop index is 2 and you remove the current data ('2') and are
left with:

1
3  ==  index of 2 points here
4

Now you increment the index to 3 and you get

1
3
4 == now here

So you have skipped 3.  What you should be doing is determining all
the index values that meet your criteria and then deleting them all at
once, or start from the bottom of the dataframe and work up.

On 3/26/08, Jamie Ledingham [EMAIL PROTECTED] wrote:
 Dear all, I have a problem with a loop, if anyone has any knowledge on
 these things I would appreciate some comments.  The code below is
 designed to allow me to extract the top record of the data frame, and
 them remove rows from the data frame which have an index close to the
 extracted top record.


 topstorm-subset(rankeddataset[1,]) ## Extracts the top storm
 topstormindex-rankeddataset[1,1] ## Finds the top storm start index
 startindex-topstormindex-48 ## sets the start and end indexes
 endindex-topstorminde+48
 rankeddataset-rankeddataset[-1,] ## Creates a new list with the top
 storm removed

 ##This section of code needs looped.  It removes storms from the list
 which are too close to the extracted storm

 for (i in 1:30){
 if (rankeddataset[i,1]startindex  rankeddataset[i,1]endindex)
 {rankeddataset-rankeddataset[-i,]}
 }

 Here is some example data:

   82856  15 / 6 / 1966   82856:8287925.9
   82857  15 / 6 / 1966   82857:8288020.5
   83036  23 / 6 / 1966   83036:8305917.3
   87250 15 / 12 / 1966   87250:8727315.9

 The loop does not currently work, it seems to remove every second line
 or so.  Can anyone suggest why this might be, I'm not particularly
 experienced in using loops so it may be a rookie mistake.  Thanks in
 advance.
 Jamie Ledingham

 __
 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
 and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

__
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
and provide commented, minimal, self-contained, reproducible code.