[R] Sorting a data frame with values of different lengths
Sarah Goslee
sarah.goslee at gmail.com
Tue Jun 7 16:22:44 CEST 2011
Hi,
On Tue, Jun 7, 2011 at 10:01 AM, William Armstrong
<William.Armstrong at noaa.gov> wrote:
> Hi Sarah,
>
> str(W_table) gives me:
>
>> str(W_table)
> 'data.frame': 11 obs. of 3 variables:
> $ pds_gagehandles.i.: Factor w/ 1 level "mibe": 1 1 1 1 1 1 1 1 1 1 ...
> $ p : chr "1" "2" "3" "4" ...
> $ W : chr "746" "870.5" "767" "1066" ...
See? W is character, so that's how it's being used to sort.
Somewhere in your code it's being converted to character. Since I
can't run your code myself, I can't easily see where that's happening.
Nothing jumps out as an obvious error.
Sarah
> here is the script I am using, with the lines that create W and the W_table
> bolded:
>
> for(i in 1:length(pds_gagehandles)){
> POTWY<-get(paste(pds_gagehandles[i],"_POTWY",sep=""))
> num_wyrs<-length(POTWY)
> pre70wyrs_ind<-1:(length(POTWY)-39)
> post70wyrs_ind<-(length(POTWY)-38):length(POTWY)
>
> for(p in 1:11){
> if(p==1){
> pre70_POTWY<-POTWY[pre70wyrs_ind]
> post70_POTWY<-POTWY[post70wyrs_ind]
> original_stats<-wilcox.test(pre70_POTWY,post70_POTWY)
> W<-original_stats$statistic
> W_table<-data.frame(pds_gagehandles[i],p,W)
> W_table_name<-paste(pds_gagehandles[i],"_W_table",sep="")
> }else{
> sample_POTWY<-sample(POTWY)
> sample_pre70_POTWY<-sample_POTWY[pre70wyrs_ind]
> sample_post70_POTWY<-sample_POTWY[post70wyrs_ind]
> sample_stats<-wilcox.test(sample_pre70_POTWY,sample_post70_POTWY)
> sample_W<-sample_stats$statistic
> sample_info<-c(pds_gagehandles[i],p,sample_W)
> W_table<-rbind(W_table,sample_info)
> if(p==1001){
> assign(W_table_name,W_table)
>
> write.table(W_table,col.names=c('gage_handle','iteration','W'),file=paste(W_table_name,".txt",sep=""),sep="\t",row.names=FALSE)
> }
> }
> }
> }
>
> Thank you very much for your help.
>
> Billy
>
> --
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-help
mailing list