[R] create new matrix from user-defined function

arun smartpink111 at yahoo.com
Wed Jul 10 23:09:32 CEST 2013


Hi,
You could try:
 mat1<-matrix(dat3[rowSums(dat3[,2:3])!=dat3[,4],1],ncol=1,dimnames=list(NULL,"MW_EEsDue_ERRORS"))
 mat1
#     MW_EEsDue_ERRORS
#[1,]             1882
#[2,]             1884
#[3,]             1885
A.K.


#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



More information about the R-help mailing list