[R] create new matrix from user-defined function

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Wed Jul 10 23:42:05 CEST 2013


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of bcrombie
> Sent: Wednesday, July 10, 2013 12:19 PM
> To: r-help at r-project.org
> Subject: [R] create new matrix from user-defined function
> 
> #Let's say I have the following data set:
> 
> dat3 = data.frame(A_CaseID = c(1881, 1882, 1883, 1884, 1885),
>                   B_MW_EEsDue1 = c(2, 2, 1, 4, 6),
>                   C_MW_EEsDue2 = c(5, 5, 4, 1, 6),
>                   D_MW_EEsDueTotal = c(7, 9, 5, 6, 112))
> dat3
> # A_CaseID B_MW_EEsDue1 C_MW_EEsDue2 D_MW_EEsDueTotal
> # 1     1881            2            5                7
> # 2     1882            2            5                9
> # 3     1883            1            4                5
> # 4     1884            4            1                6
> # 5     1885            6            6              112
> 
> # I want to:
> #CREATE A NEW 1-COLUMN MATRIX (of unknown #rows) LISTING ONLY "A"'s
> WHERE "D
> != B + C"
> #THIS COLUMN CAN BE LABELED "MW_EEsDue_ERRORS", and output for this
> example
> should be:
> 
> # MW_EEsDue_ERRORS
> # 1 1882
> # 2 1884
> # 3 1885
> 
> #What is the best way to do this?  Thanks for your time.  BNC
> 
> 

Here is one option, there are many others.  Only you can decide what is "best".

data.frame(MW_EEsDue_ERRORS=dat3[dat3[[4]] != dat3[[2]]+dat3[[3]],][[1]])


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204



More information about the R-help mailing list