On 22.07.2011 22:42, Sean Bignami wrote:
Hello again R-folks,
I'm trying to apply a loop to multiple data frames and then organize the data in a manageable form. I
have the code for my loop figured out (thanks to help from this list)...it runs up to 2000 iterations
of a "while" loop until it finds a 40-row "d2p" column sum>5
n<-nrow(dsub.f1)
test<-0
for(k in 1:2001){
if(k>2000){
break} #stops after 2000 iterations
else{
while(test<5){ #continues unti sum>5, or 2000 iter.
i<-sample(1:n-40,1)
x<-dsub.f1[seq(from=i, to=i+40),]
test<-sum(x[,"d2p"],na.rm=TRUE)}
}
}
I don't see much sense in the code above. Which of the objects are you
interested in?
My data frames are all named dsub.f1 through dsub.f360 (and are subsets of a laster data
frame "d") which I created using this code:
for(i in 1:360){
assign(paste("dsub.f",d[d$fish==i,1],sep=""),subset(d[d$fish==i,]))}
I find it easier to put such things in a list as in:
dsub.f <- split(d, d$fish)
I created a list of all my data frames using:
df.filter<-function(x) inherits(get(x),'data.frame')
#pulls all dataframes
dfs<-Filter(df.filter,ls());str(dfs)
dsubs<-dfs[3:360];head(dsubs)
#removes "d" and "data.csv" dataframes that I don't want
This is not required since those data.frames are collected in one list
(see above).
But I can't figure out how to write a "for" loop that will apply the (top) code
above to each data frame.
Just write the code in a function athat assumes a data.frame as an
argument and then sapply() it on your list of data.frames.
Best wishes,
Uwe Ligges
Also, once I'm able to apply the loop to each data frame, I would like to
organize the data in a manageable form so I can reference it in other analyses.
I hope I explained what I'm trying to do well enough. Thanks!!!
sean
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.
______________________________________________
[email protected] 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.