[R] summing "transfers"
Farley, Robert
FarleyR at metro.net
Fri Mar 16 18:57:11 CET 2012
I just want the weighted sum of all the cases where either route is found in VEHx and the other is found in the previous or subsequent variable. The only examples I can think of producing are the following:
#
> OBDataSumm <- read.spss("P:/Data/OBSurveys/OBSurvey-2010-2011/Final Delivery, Metro On-Board O-D Survey/LAMTA_OD_WEIGHTED_DATA_SETS_012512/LAMTA_OD_SUMMARY_WEIGHTED_012512.SAV", use.value.labels=TRUE, trim_values = TRUE, trim.factor.names = TRUE, max.value.labels=Inf, to.data.frame=TRUE)
Warning message:
In read.spss("P:/Data/OBSurveys/OBSurvey-2010-2011/Final Delivery, Metro On-Board O-D Survey/LAMTA_OD_WEIGHTED_DATA_SETS_012512/LAMTA_OD_SUMMARY_WEIGHTED_012512.SAV", :
P:/Data/OBSurveys/OBSurvey-2010-2011/Final Delivery, Metro On-Board O-D Survey/LAMTA_OD_WEIGHTED_DATA_SETS_012512/LAMTA_OD_SUMMARY_WEIGHTED_012512.SAV: Unrecognized record type 7, subtype 18 encountered in system file
>
> describe(OBDataSumm,num.desc=c("mean","median","var","sd","valid.n"),xname=NA,maxfac=10,show.pc=TRUE)
Description of OBDataSumm
Numeric
mean median var sd valid.n
SAMPN 2.155e+05 1.733e+05 1.151e+10 1.073e+05 3.378e+04
~snip~
expfactor 1.168 1.089 0.1975 0.4444 3.378e+04
expwgt 41.93 22.22 7720 87.87 3.378e+04
ltfactor 0.6339 0.5 0.08128 0.2851 3.378e+04
ltweight 26.7 12.42 4473 66.88 3.378e+04
Factor
~snip~
VEH1
Value Count Percent
MT-802 1645 4.87
MT-801 1211 3.58
MT-804 975 2.89
MT-803 568 1.68
MT-.51 510 1.51
MT-720 460 1.36
MT-.60 433 1.28
MT-.18 409 1.21
MT-.81 396 1.17
MT-805 396 1.17
mode = MT-802 Valid n = 33782 467 categories - only first 10 shown
VEH2
Value Count Percent
12142 35.94
MT-802 2304 6.82
MT-801 1375 4.07
MT-803 874 2.59
MT-804 598 1.77
MT-901 527 1.56
MT-805 461 1.36
MT-720 323 0.96
MT-910 268 0.79
MT-207 260 0.77
mode = Valid n = 33782 379 categories - only first 10 shown
VEH3
Value Count Percent
25795 76.36
MT-802 883 2.61
MT-801 600 1.78
MT-901 256 0.76
MT-803 251 0.74
MT-804 227 0.67
MT-805 126 0.37
MT-224 114 0.34
MT-720 113 0.33
MT-204 105 0.31
mode = Valid n = 33782 378 categories - only first 10 shown
VEH4
Value Count Percent
31532 93.34
MT-801 126 0.37
MT-802 111 0.33
MT-901 89 0.26
MT-804 74 0.22
MT-803 71 0.21
MT-224 38 0.11
MT-741 36 0.11
MT-.60 34 0.1
MT-207 33 0.1
mode = Valid n = 33782 321 categories - only first 10 shown
VEH5
Value Count Percent
... 33270 98.48
MT-803 ... 18 0.05
MT-802 ... 13 0.04
MT-233 ... 11 0.03
MT-741 ... 11 0.03
MT-804 ... 11 0.03
MT-234 ... 10 0.03
MT-801 ... 9 0.03
MT-901 ... 9 0.03
MT-.60 ... 8 0.02
mode = Valid n = 33782 187 categories - only first 10 shown
VEH6
Value Count Percent
... 33700 99.76
MT-232 ... 3 0.01
MT-741 ... 3 0.01
MT-802 ... 3 0.01
MT-.40 ... 2 0.01
MT-.45 ... 2 0.01
MT-.68 ... 2 0.01
MT-128 ... 2 0.01
MT-207 ... 2 0.01
MT-210 ... 2 0.01
mode = Valid n = 33782 66 categories - only first 10 shown
VEH7
Value Count Percent
... 33766 99.95
MT-.62 ... 1 0
MT-.78 ... 1 0
MT-.84 ... 1 0
MT-108 ... 1 0
MT-150 ... 1 0
MT-152 ... 1 0
MT-158 ... 1 0
MT-166 ... 1 0
MT-246 ... 1 0
mode = Valid n = 33782 17 categories - only first 10 shown
VEH8
Value Count Percent
... 33779 99.99
MT-.45 ... 1 0
MT-901 ... 1 0
OC-043 ... 1 0
mode = Valid n = 33782
~snip~
> sink("C:/Users/farleyr/Desktop/BikeModel/test.txt")
>
> colvf <- 81 # column of first VEH in indat
> colvl <- 88 # column of last VEH in indat
> tapply( rep( OBDataSumm$expwgt, colvl-colvf ),
+ list( as.matrix(OBDataSumm[ ,colvf:(colvl-1)]),
+ as.matrix(OBDataSumm[ ,(colvf+1):colvl]) ), sum)
> sink()
>
Produces a file that begins with
AM-PAC AM-PAC
1.605193e+06 1.328313e+06 NA NA
NA 4.226964e+06 NA NA
AM-PAC 2.741611e+02 NA NA NA
AM-PAC NA 1.676836e+01 NA NA
~and ends with ~
FZ-274 NA NA NA NA NA
FZ-280 NA NA NA NA NA
[ reached getOption("max.print") -- omitted 595 rows ]]
But I have no clue if this even has the interchanges I need.
Robert Farley
LACMTA
-----Original Message-----
From: Jeff Newmiller [mailto:jdnewmil at dcn.davis.ca.us]
Sent: Thursday, 15 March, 2012 19:34
To: Farley, Robert; R-help at r-project.org
Subject: Re: [R] summing "transfers"
Then save to a variable, and maybe then to a file?
Are you wanting it in long form? You can use reshape if so.
Note that a reproducible example and sample result always helps prompt more specific answers.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
"Farley, Robert" <FarleyR at metro.net> wrote:
>I have a dataframe from an On-Board Survey with weights ("expwgt") and
>variables for up to 8 used lines: VEH1 through VEH8. The lines are
>labeled "MT-..1" through "MT-902". I want to know how many transfers
>there are between MT-802 and MT-901. That is, when one of them is VEHx
>and the other is VEHx+1 or VEHx-1
>
>E.g. {VEH1 = MT-802 AND VEH2 = MT-901 } plus {VEH2 = MT-901 AND VEH3 =
>MT-802} plus all the other combinations....
>
>
>Someone suggested:
>
>colvf <- 81 # column of first VEH in indat colvl <- 88 # column of last
>VEH in indat tapply( rep( SubOre$expwgt, colvl-colvf ),
> list( as.matrix(SubOre[ ,colvf:(colvl-1)]),
> as.matrix(SubOre[ ,(colvf+1):colvl]) ), sum)
>
>but there are too many rows and columns to print.
>
>
>
>
>Robert Farley
>LACMTA
>1 Gateway Plaza
>Los Angeles, CA 90012-2952
>(213)922-2532
>FarleyR at Metro.net
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>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