[R] For Loop - loading 10 sets of data and calculating

jim holtman jholtman at gmail.com
Fri Nov 7 21:21:08 CET 2008


# I would put this in a list in the following manner

Bin <- lapply(1:10, function(.file){

    #-------

    #Loads bin data frame from csv files with acres and TAZ data
    fileName <-
paste("I:/Research/Samba/urb_transport_modeling/LUSDR/Workspace/BizLandPrice/data/Bin_lookup_values/Bin",
.file, "_lookup.csv", sep="")
    Bin_main <- read.csv(file=fileName,head=FALSE);

    #Separates Acres data from main data and converts acres to square feet
    Bin_Acres=Bin_main[[1]]*43560

    #Separates TAZ data from main data
    Bin_TAZ=Bin_main[[2]]

    #Separates TAZ data from main data and converts acres to square feet
    Bin_TAZvacant=Bin_main[[3]]*43560

    #Sums each parcel acreage data of the bin
    BinAcres_sum=sum(Bin_Acres)

    #Creates data frame of cumlative percentages of each parcel of bin
    Bin_cumper=cumsum(Bin_Acres/BinAcres_sum)

    #Calculates the probability of choosing particular parcel from bin
    Bin_parprob=abs(1-Bin_cumper)

    #Combines parcel acreage data and cumlative percentage data
    cbind(Bin_Acres,Bin_parprob,Bin_TAZ,Bin_TAZvacant)
})

On Fri, Nov 7, 2008 at 1:48 PM, PDXRugger <J_R_36 at hotmail.com> wrote:
>
> I am trying to simplify my code by adding a for loop that will load and
> compute a sequence of code 10 time.  They way i run it now is that the same
> 8 lines of code are basically reproduced 10 times.  I would like to replace
> the numeric value in the code (e.g. Bin1, Bin2....Bin10) each time the loop
> goes around.  Below i tried doing this with a simple for loop and adding the
> string character before each numeric value.  I need to first load the data
> then calculate, im sure this is possible as i have seen it done but cant
> seem to reproduce it in my own code.  Hope my question is clear and i hope
> someone can offer some guidance.
>
> Cheers,
> JR
>
> for (i in 1:10) {
>
> #-------
>
> #Loads bin data frame from csv files with acres and TAZ data
> Bin$i_main <-
> read.csv(file="I:/Research/Samba/urb_transport_modeling/LUSDR/Workspace/BizLandPrice/data/Bin_lookup_values/Bin$i_lookup.csv",head=FALSE);
>
> #Separates Acres data from main data and converts acres to square feet
> Bin$i_Acres=Bin$i_main[[1]]*43560
>
> #Separates TAZ data from main data
> Bin$i_TAZ=Bin$i_main[[2]]
>
> #Separates TAZ data from main data and converts acres to square feet
> Bin$i_TAZvacant=Bin$i_main[[3]]*43560
>
> #Sums each parcel acreage data of the bin
> Bin$iAcres_sum=sum(Bin$i_Acres)
>
> #Creates data frame of cumlative percentages of each parcel of bin
> Bin$i_cumper=cumsum(Bin$i_Acres/Bin$iAcres_sum)
>
> #Calculates the probability of choosing particular parcel from bin
> Bin$i_parprob=abs(1-Bin$i_cumper)
>
> #Combines parcel acreage data and cumlative percentage data
> Bin$iMain.data = cbind(Bin%i_Acres,Bin$i_parprob,Bin$i_TAZ,Bin$i_TAZvacant)
> }
>
> --
> View this message in context: http://www.nabble.com/For-Loop---loading-10-sets-of-data-and-calculating-tp20386673p20386673.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



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

What is the problem that you are trying to solve?



More information about the R-help mailing list