SEVERAL Log Lines for Classes 3 and 4, STAT 705 Fall 2017 ========================================================== > A = (1:20) %o% rep(1,20) > B = outer(1:20,1:20, function(x,y) x <= y) > C = c(A)[c(B)] ###-------------------------------------------- ## We considered the following code for an R function to ## examine the columns of an input data-frame infram , ## decide which ones are numeric, and to calculate and output ## the numbers of missing elements in those columns. > framfix = function(infram) { numcols = which( sapply(infram, function(col) is.numeric(col)) ) newfram = sapply( infram[,numcols], function(col) sum(is.na(col)) ) list(numcols=numcols, newfram=newfram) } ## Here is a little test of the function code just given: > toyfram = cbind.data.frame(letcol = letters[1:23], numcol1 = c(1:10,rep(NA,3),11:20), numcol2 = c(1,replace(1:22,(0:21) %% 2 ==1, NA)), bool1 = c(as.logical(rbinom(20,1,0.4)),NA,NA,NA) ) > toyfram letcol numcol1 numcol2 bool1 1 a 1 1 TRUE 2 b 2 1 TRUE 3 c 3 NA FALSE 4 d 4 3 FALSE 5 e 5 NA FALSE 6 f 6 5 TRUE 7 g 7 NA FALSE 8 h 8 7 FALSE 9 i 9 NA TRUE 10 j 10 9 FALSE 11 k NA NA TRUE 12 l NA 11 FALSE 13 m NA NA FALSE 14 n 11 13 FALSE 15 o 12 NA FALSE 16 p 13 15 FALSE 17 q 14 NA FALSE 18 r 15 17 FALSE 19 s 16 NA TRUE 20 t 17 19 TRUE 21 u 18 NA NA 22 v 19 21 NA 23 w 20 NA NA > framfix(toyfram) $numcols numcol1 numcol2 2 3 $newfram numcol1 numcol2 3 11 ## -----------------------Done as part of solution to HW2: > summary(diff(nepali$id)) Min. 1st Qu. Median Mean 3rd Qu. Max. 0.0 0.0 0.0 400.5 0.0 239319.0 #### shows that the id's are in sorted increasing numerical order > tmp = c(0,as.numeric(substr(nepali$id,1,5))) newind = which(tmp[-1] > tmp[-1001]) ### 113 indices at which new households begin ### ------------------------------- alt solution to HW2 (e) > aux = rle(as.numeric(substr(nepali$id,1,5))) > length(aux) [1] 2 > names(aux) [1] "lengths" "values" > sapply(aux,length) lengths values 113 113 > aux$lengths [1] 10 15 5 15 10 10 10 5 10 5 15 10 10 10 10 10 5 10 10 5 5 10 10 5 10 [26] 10 10 10 20 10 10 10 5 20 15 15 5 5 10 10 5 15 5 10 5 10 15 10 10 5 [51] 10 20 5 10 5 10 5 10 5 15 5 10 5 5 15 10 10 15 5 10 10 10 5 10 5 [76] 5 5 5 10 5 5 10 5 5 20 5 10 5 5 5 10 10 5 5 15 10 5 5 5 10 [101] 5 5 10 10 10 10 5 10 5 15 10 5 5 > aux$values [1] 12001 12002 12003 12005 12006 12007 12008 12009 12011 12012 12013 12014 [13] 12015 12016 12019 12021 12023 12024 12025 12026 12027 12029 12032 12033 [25] 12034 12035 12036 12037 12038 12039 12040 12041 12043 12044 12045 12047 [37] 12054 12056 12057 12058 12059 12060 12061 12063 12065 12067 12068 12069 [49] 36001 36002 36005 36011 36012 36013 36014 36016 36018 36019 36021 36022 [61] 36025 36030 36033 36034 36035 36036 36037 36039 36041 36043 36047 36048 [73] 36049 36050 36051 36053 36054 36055 36056 36057 36058 36059 36061 36062 [85] 36067 36069 36070 36071 36072 36074 36075 36078 36079 36080 36081 36084 [97] 36087 36088 36090 36091 36092 36094 36095 36099 36100 52002 52003 52004 [109] 52005 52006 52007 52008 52009 ## still another way: > HHdat2 = aggregate.data.frame(nepali[,c("mage","lit","died","alive")], by=list(substr(nepali$id,1,5)), function(col) var(col) ) ### ----------------------------------------- ## calling function within standard function "integrate" > integrate(function(x) sin(x)^2, 0,6) 3.134143 with absolute error < 5.6e-10 > tmp = integrate(function(x) sin(x)^2, 0,6) > unlist(tmp) $value [1] 3.134143 $abs.error [1] 5.598301e-10 $subdivisions [1] 1 $message [1] "OK" $call integrate(f = function(x) sin(x)^2, lower = 0, upper = 6) > unlist(tmp[1:3]) value abs.error subdivisions 3.134143e+00 5.598301e-10 1.000000e+00