[R] loading and manipulating 10 data frames-simplified

jim holtman jholtman at gmail.com
Fri Mar 27 13:20:28 CET 2009


Look at using a 'list' as obtained from 'lapply'

fileNames <- 'your files to be read'
Bin_main <- lapply(fileNames, function(.file){
    input <- read.csv(fileNames, ....)
    # all your calculations; e.g.,
    acre <- ...
    ....
    cbind(acres, parprob, ...)
})

Now look at the structure ('str(Bin_main)') and it should have 10 (or
how ever many files you have) elements with the data you want.

On Thu, Mar 26, 2009 at 5:25 PM, PDXRugger <J_R_36 at hotmail.com> wrote:
>
> I have to load 10 different data frames and then manipulate those 10 data
> frames but would like to do this in a more simplified code than what i am
> doing.  I have tried a couple of approaches but cannot get it to work
> correctly.
>
> So the initial (bulky) code is:
>
> #Bin 1
> #-------
>
> #Loads bin data frame from csv files with acres and TAZ data
> Bin1_main <-
> read.csv(file="I:/Research/Samba/urb_transport_modeling/LUSDR/Workspace/BizLandPrice/data/Bin_lookup_values/Bin1_lookup.csv",head=FALSE);
>
> #Separates Acres data from main data and converts acres to square feet
> Bin1_Acres=Bin1_main[[1]]*43560
>
> #Separates TAZ data from main data
> Bin1_TAZ=Bin1_main[[2]]
>
> #Separates TAZ data from main data and converts acres to square feet
> Bin1_TAZvacant=Bin1_main[[3]]*43560
>
> #Sums each parcel acreage data of the bin
> Bin1Acres_sum=sum(Bin1_Acres)
>
> #Creates data frame of cumlative percentages of each parcel of bin
> Bin1_cumper=cumsum(Bin1_Acres/Bin1Acres_sum)
>
> #Calculates the probability of choosing particular parcel from bin
> Bin1_parprob=abs(1-Bin1_cumper)
>
> #Combines parcel acreage data and cumlative percentage data
> Bin1Main.data = cbind(Bin1_Acres,Bin1_parprob,Bin1_TAZ,Bin1_TAZvacant)
>
>
> #Bin 2
> #-------
>
> #Loads bin data frame from csv files with acres and TAZ data
> Bin2_main <-
> read.csv(file="I:/Research/Samba/urb_transport_modeling/LUSDR/Workspace/BizLandPrice/data/Bin_lookup_values/Bin2_lookup.csv",header=FALSE);
>
> #Separates Acres data from main data and converts acres to square feet
> Bin2_Acres=Bin2_main[[1]]*43560
>
> #Separates TAZ data from main data
> Bin2_TAZ=Bin2_main[[2]]
>
> #Separates TAZ data from main data and converts acres to square feet
> Bin2_TAZvacant=Bin2_main[[3]]*43560
>
> #Sums each parcel acreage data of the bin
> Bin2Acres_sum=sum(Bin2_Acres)
>
> #Creates data frame of cumlative percentages of each parcel of bin
> Bin2_cumper=cumsum(Bin2_Acres/Bin2Acres_sum)
>
> #Calculates the probability of choosing particular parcel from bin
> Bin2_parprob=abs(1-Bin2_cumper)
>
> #Combines parcel acreage data and cumlative percentage data
> Bin2Main.data = cbind(Bin2_Acres,Bin2_parprob,Bin2_TAZ,Bin2_TAZvacant)
>
> #Bin 3
> #-------
>
> #Loads bin data frame from csv files with acres and TAZ data
> Bin3_main <-
> read.csv(file="I:/Research/Samba/urb_transport_modeling/LUSDR/Workspace/BizLandPrice/data/Bin_lookup_values/Bin3_lookup.csv",header=FALSE);
>
> #Separates Acres data from main data and converts acres to square feet
> Bin3_Acres=Bin3_main[[1]]*43560
>
> #Separates TAZ data from main data
> Bin3_TAZ=Bin3_main[[2]]
>
> #Separates TAZ data from main data and converts acres to square feet
> Bin3_TAZvacant=Bin3_main[[3]]*43560
>
> #Sums each parcel acreage data of the bin
> Bin3Acres_sum=sum(Bin3_Acres)
>
> #Creates data frame of cumlative percentages of each parcel of bin
> Bin3_cumper=cumsum(Bin3_Acres/Bin3Acres_sum)
>
> #Calculates the probability of choosing particular parcel from bin
> Bin3_parprob=abs(1-Bin3_cumper)
>
> #Combines parcel acreage data and cumlative percentage data
> Bin3Main.data = cbind(Bin3_Acres,Bin3_parprob,Bin3_TAZ,Bin3_TAZvacant)
>
> #Bin 4
> #-------
>
> #Loads bin data frame from csv files with acres and TAZ data
> Bin4_main <-
> read.csv(file="I:/Research/Samba/urb_transport_modeling/LUSDR/Workspace/BizLandPrice/data/Bin_lookup_values/Bin4_lookup.csv",header=FALSE);
>
> #Separates Acres data from main data and converts acres to square feet
> Bin4_Acres=Bin4_main[[1]]*43560
>
> #Separates TAZ data from main data
> Bin4_TAZ=Bin4_main[[2]]
>
> #Separates TAZ data from main data and converts acres to square feet
> Bin4_TAZvacant=Bin4_main[[3]]*43560
>
> #Sums each parcel acreage data of the bin
> Bin4Acres_sum=sum(Bin4_Acres)
>
> #Creates data frame of cumlative percentages of each parcel of bin
> Bin4_cumper=cumsum(Bin4_Acres/Bin4Acres_sum)
>
> #Calculates the probability of choosing particular parcel from bin
> Bin4_parprob=abs(1-Bin4_cumper)
>
> #Combines parcel acreage data and cumlative percentage data
> Bin4Main.data = cbind(Bin4_Acres,Bin4_parprob,Bin4_TAZ,Bin4_TAZvacant)
>
> #Bin 5
> #-------
>
> #Loads bin data frame from csv files with acres and TAZ data
> Bin5_main <-
> read.csv(file="I:/Research/Samba/urb_transport_modeling/LUSDR/Workspace/BizLandPrice/data/Bin_lookup_values/Bin5_lookup.csv",header=FALSE);
>
> #Separates Acres data from main data and converts acres to square feet
> Bin5_Acres=Bin5_main[[1]]*43560
>
> #Separates TAZ data from main data
> Bin5_TAZ=Bin5_main[[2]]
>
> #Separates TAZ data from main data and converts acres to square feet
> Bin5_TAZvacant=Bin5_main[[3]]*43560
>
> #Sums each parcel acreage data of the bin
> Bin5Acres_sum=sum(Bin5_Acres)
>
> #Creates data frame of cumlative percentages of each parcel of bin
> Bin5_cumper=cumsum(Bin5_Acres/Bin5Acres_sum)
>
> #Calculates the probability of choosing particular parcel from bin
> Bin5_parprob=abs(1-Bin5_cumper)
>
> #Combines parcel acreage data and cumlative percentage data
> Bin5Main.data = cbind(Bin5_Acres,Bin5_parprob,Bin5_TAZ,Bin5_TAZvacant)
>
> #Bin 6
> #-------
>
> #Loads bin data frame from csv files with acres and TAZ data
> Bin6_main <-
> read.csv(file="I:/Research/Samba/urb_transport_modeling/LUSDR/Workspace/BizLandPrice/data/Bin_lookup_values/Bin6_lookup.csv",header=FALSE);
>
> #Separates Acres data from main data and converts acres to square feet
> Bin6_Acres=Bin6_main[[1]]*43560
>
> #Separates TAZ data from main data
> Bin6_TAZ=Bin6_main[[2]]
>
> #Separates TAZ data from main data and converts acres to square feet
> Bin6_TAZvacant=Bin6_main[[3]]*43560
>
> #Sums each parcel acreage data of the bin
> Bin6Acres_sum=sum(Bin6_Acres)
>
> #Creates data frame of cumlative percentages of each parcel of bin
> Bin6_cumper=cumsum(Bin6_Acres/Bin6Acres_sum)
>
> #Calculates the probability of choosing particular parcel from bin
> Bin6_parprob=abs(1-Bin6_cumper)
>
> #Combines parcel acreage data and cumlative percentage data
> Bin6Main.data = cbind(Bin6_Acres,Bin6_parprob,Bin6_TAZ,Bin6_TAZvacant)
>
>
> #Bin7
> #-------
>
> #Loads bin data frame from csv files with acres and TAZ data
> Bin7_main <-
> read.csv(file="I:/Research/Samba/urb_transport_modeling/LUSDR/Workspace/BizLandPrice/data/Bin_lookup_values/Bin7_lookup.csv",header=FALSE);
>
> #Separates Acres data from main data and converts acres to square feet
> Bin7_Acres=Bin7_main[[1]]*43560
>
> #Separates TAZ data from main data
> Bin7_TAZ=Bin7_main[[2]]
>
> #Separates TAZ data from main data and converts acres to square feet
> Bin7_TAZvacant=Bin7_main[[3]]*43560
>
> #Sums each parcel acreage data of the bin
> Bin7Acres_sum=sum(Bin7_Acres)
>
> #Creates data frame of cumlative percentages of each parcel of bin
> Bin7_cumper=cumsum(Bin7_Acres/Bin7Acres_sum)
>
> #Calculates the probability of choosing particular parcel from bin
> Bin7_parprob=abs(1-Bin7_cumper)
>
> #Combines parcel acreage data and cumlative percentage data
> Bin7Main.data = cbind(Bin7_Acres,Bin7_parprob,Bin7_TAZ,Bin7_TAZvacant)
>
> #Bin 8
> #-------
>
> #Loads bin data frame from csv files with acres and TAZ data
> Bin8_main <-
> read.csv(file="I:/Research/Samba/urb_transport_modeling/LUSDR/Workspace/BizLandPrice/data/Bin_lookup_values/Bin8_lookup.csv",header=FALSE);
>
> #Separates Acres data from main data and converts acres to square feet
> Bin8_Acres=Bin8_main[[1]]*43560
>
> #Separates TAZ data from main data
> Bin8_TAZ=Bin8_main[[2]]
>
> #Separates TAZ data from main data and converts acres to square feet
> Bin8_TAZvacant=Bin8_main[[3]]*43560
>
> #Sums each parcel acreage data of the bin
> Bin8Acres_sum=sum(Bin8_Acres)
>
> #Creates data frame of cumlative percentages of each parcel of bin
> Bin8_cumper=cumsum(Bin8_Acres/Bin8Acres_sum)
>
> #Calculates the probability of choosing particular parcel from bin
> Bin8_parprob=abs(1-Bin8_cumper)
>
> #Combines parcel acreage data and cumlative percentage data
> Bin8Main.data = cbind(Bin8_Acres,Bin8_parprob,Bin8_TAZ,Bin8_TAZvacant)
>
> #Bin 9
> #-------
>
> #Loads bin data frame from csv files with acres and TAZ data
> Bin9_main <-
> read.csv(file="I:/Research/Samba/urb_transport_modeling/LUSDR/Workspace/BizLandPrice/data/Bin_lookup_values/Bin9_lookup.csv",header=FALSE);
>
> #Separates Acres data from main data and converts acres to square feet
> Bin9_Acres=Bin9_main[[1]]*43560
>
> #Separates TAZ data from main data
> Bin9_TAZ=Bin9_main[[2]]
>
> #Separates TAZ data from main data and converts acres to square feet
> Bin9_TAZvacant=Bin9_main[[3]]*43560
>
> #Sums each parcel acreage data of the bin
> Bin9Acres_sum=sum(Bin9_Acres)
>
> #Creates data frame of cumlative percentages of each parcel of bin
> Bin9_cumper=cumsum(Bin9_Acres/Bin9Acres_sum)
>
> #Calculates the probability of choosing particular parcel from bin
> Bin9_parprob=abs(1-Bin9_cumper)
>
> #Combines parcel acreage data and cumlative percentage data
> Bin9Main.data = cbind(Bin9_Acres,Bin9_parprob,Bin9_TAZ,Bin9_TAZvacant)
>
> #Bin 10
> #-------
>
> #Loads bin data frame from csv files with acres and TAZ data
> Bin10_main <-
> read.csv(file="I:/Research/Samba/urb_transport_modeling/LUSDR/Workspace/BizLandPrice/data/Bin_lookup_values/Bin10_lookup.csv",header=FALSE);
>
> #Separates Acres data from main data and converts acres to square feet
> Bin10_Acres=Bin10_main[[1]]*43560
>
> #Separates TAZ data from main data
> Bin10_TAZ=Bin10_main[[2]]
>
> #Separates TAZ data from main data and converts acres to square feet
> Bin10_TAZvacant=Bin10_main[[3]]*43560
>
> #Sums each parcel acreage data of the bin
> Bin10Acres_sum=sum(Bin10_Acres)
>
> #Creates data frame of cumlative percentages of each parcel of bin
> Bin10_cumper=cumsum(Bin10_Acres/Bin10Acres_sum)
>
> #Calculates the probability of choosing particular parcel from bin
> Bin10_parprob=abs(1-Bin10_cumper)
>
> #Combines parcel acreage data and cumlative percentage data
> Bin10Main.data = cbind(Bin10_Acres,Bin10_parprob,Bin10_TAZ,Bin10_TAZvacant)
> #--------------------------------------------------------------------------
>
>
> I have tried :
>
> 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
> BinMain.data = cbind(Bin_Acres,Bin_parprob,Bin_TAZ,Bin_TAZvacant)
> })
>
> #-----------------------------------------------------------------
>
> I am thinking that a simple for loop would work with a paste command but
> that appoach has yet to work either.  Thanks
>
> Cheers,
> JR
> --
> View this message in context: http://www.nabble.com/loading-and-manipulating-10-data-frames-simplified-tp22731441p22731441.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