[R] for loop using index values

Frede Aakmann Tøgersen frtog at vestas.com
Thu May 15 07:27:30 CEST 2014


As David says it will be good to see how the structure of the list subset is. Now I'm going to guess that the i'th element of subset is a dataframe since you are using nrow(subset[[i]]).

Now you're doing 

select<-which(nrow(subset[[i]])>=100) ## added a missing closing )

so now select holds the number of rows of subset[[i]] only.

Perhaps it should be something like

select <- which(lapply(subset, nrow) >= 100).

Using some dummy data:

> tmp <- list(matrix(0,2,3),matrix(0,9,3),matrix(0,8,3),matrix(0,3,3),matrix(0,6,3))
> which((lapply(tmp, nrow) > 6))
[1] 2 3

 
By the way there is a function called subset so you could run into trouble later on when calling your list for subset.

Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of David Winsemius
> Sent: 15. maj 2014 06:49
> To: David Gwenzi
> Cc: r-help at r-project.org
> Subject: Re: [R] for loop using index values
> 
> 
> On May 14, 2014, at 9:23 PM, David Gwenzi wrote:
> 
> > Hi all
> >
> > I have point data along a transect and I want to divide the transect
> > into
> > small blocks of 10m length each. I have named the blocks as a list i.e
> > subset[[i]]. Now the issue is I want to process only those blocks
> > that have
> > at least 100 data points and keep the original index values of those
> > subsets. How do I set the for loop. I have tried the following but
> > it is
> > still processing everything
> >
> > select<-which(nrow(subset[[i]])>=100
> > for (i in c(select)){
> > ..............
> > }
> > Thank you in advance,
> 
> So can you provide str( subset[[1]] )?
> 
> 
> >
> > David Gwenzi
> > Graduate Degree Program in Ecology
> > Natural Resources Ecology Lab
> > Colorado State University
> >
> > 	[[alternative HTML version deleted]]
> 
> And this is a plain text mailing list as described in the Posting Guide.
> 
> --
> 
> David Winsemius, MD
> Alameda, CA, USA
> 
> ______________________________________________
> 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.



More information about the R-help mailing list