[R] YALAQ - Yet Another LApply Question

Thompson, David (MNR) David.John.Thompson at ontario.ca
Wed Oct 15 18:19:05 CEST 2008


 Please forgive this repost, it's been a week without a squeak. No
comments?

Original post:
https://stat.ethz.ch/pipermail/r-help/2008-October/176340.html
Hello,

Two lapply questions (system info and sample data below):

1) Why does the first form of command1 add the name of y _after_ the
str() output rather than before as does the second (preferred) form?
   # command1 version1
   invisible(lapply(ls(pattern='bn'), function(y) cat(y, "\n",
str(get(y)), "\n") ))

   # command1 version2 (preferred output)
   invisible(lapply(ls(pattern='bn'), function(y) { cat(y, "\n") ;
str(get(y)) ; cat("\n") } ))

2) Why does the same method as command1 version2 above not work with the
summary() command as does the second (preferred) set of commands?
   # command2 version1
   lapply(ls(pattern='bn'), function(y) { cat(y, "\n") ; summary(get(y))
; cat("\n") } )

   # command2 version2 (preferred output)
   smry.list <- lapply(ls(pattern='bn'), function(y) summary(get(y)))
   names(smry.list) <- ls(pattern='bn')
   smry.list

Thanx, DaveT.

###
# system info:
> sessionInfo() ; cat("\n") ; Sys.info()[c(1:3,5)]
R version 2.7.2 (2008-08-25) 
i386-pc-mingw32 

locale:
LC_COLLATE=English_Canada.1252;LC_CTYPE=English_Canada.1252;LC_MONETARY=
English_Canada.1252;LC_NUMERIC=C;LC_TIME=English_Canada.1252

attached base packages:
[1] stats     graphics  grDevices datasets  tcltk     utils     methods
base     

other attached packages:
[1] debug_1.1.0     mvbutils_1.1.1  lattice_0.17-14 plotrix_2.4-7
svSocket_0.9-5  TinnR_1.0.2     R2HTML_1.59     Hmisc_3.4-3    

loaded via a namespace (and not attached):
[1] cluster_1.11.11 grid_2.7.2      svMisc_0.9-5    tools_2.7.2    

                     sysname                      release
version                      machine 
                   "Windows"                         "XP" "build 2600,
Service Pack 2"                        "x86" 
# system info:
###

###
# sample data
# dput(ban.nat.1994[sample(row.names(ban.nat.1994), 20),])
bn94 <- structure(list(oplt = c(18L, 50L, 11L, 16L, 54L, 35L, 45L, 40L, 
15L, 50L, 38L, 45L, 53L, 15L, 1L, 54L, 33L, 13L, 30L, 21L), tree =
c(144L, 
824L, 47L, 525L, 291L, 702L, 717L, 615L, 821L, 551L, 750L, 639L, 
664L, 813L, 31L, 346L, 689L, 59L, 200L, 658L), bd1 = c(NA, NA, 
3.6, 3.1, 4.72, 2.03, 2.88, 1.65, 5.39, 3.04, 2.75, 3.06, 2.81, 
2.78, NA, 6.5, 4.62, 4.76, NA, 2.69), bd2 = c(NA, NA, 3.41, 3.06, 
4.86, 2.09, 2.78, 1.8, 5.08, 3.26, 2.71, 3.1, 2.87, 2.73, NA, 
6.6, 4.53, 4.97, NA, 2.81), bd = c(NA, 4.25, 3.51, 3.08, 4.79, 
2.06, 2.83, 1.72, 5.23, 3.15, 2.73, 3.08, 2.84, 2.76, NA, 6.55, 
4.58, 4.87, NA, 2.75), ht = c(NA, 20.4, 18.1, 18, 25.8, 13.1, 
15.7, 4, 16, 14, 12.7, 8.6, 8.1, 16.2, NA, 52.7, 31.7, 23.7, 
NA, 17.6), spr = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_), stat = c(1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L), dam = c(NA, 2L, NA, 
NA, NA, NA, NA, 2L, NA, NA, 2L, NA, 1L, NA, NA, NA, 1L, NA, NA, 
NA), com = c("", "from partial data set, tend = 1", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "")), .Names =
c("oplt", 
"tree", "bd1", "bd2", "bd", "ht", "spr", "stat", "dam", "com"
), row.names = c(1049L, 2845L, 549L, 884L, 3128L, 1887L, 2432L, 
2087L, 879L, 2729L, 1990L, 2421L, 3089L, 871L, 3L, 3158L, 1801L, 
704L, 1653L, 1215L), class = "data.frame")

# dput(ban.nat.1995[sample(row.names(ban.nat.1995), 20),])
bn95 <- structure(list(oplt = c(2L, 27L, 54L, 8L, 8L, 51L, 3L, 4L, 20L, 
35L, 15L, 22L, 31L, 7L, 4L, 31L, 34L, 6L, 20L, 51L), tree = c(773L, 
308L, 331L, 234L, 235L, 170L, 211L, 701L, 745L, 709L, 798L, 350L, 
207L, 736L, 718L, 240L, 193L, 266L, 735L, 244L), bd1 = c(8.41, 
NA, 6.93, 2.74, 6.06, 2.36, 5.87, 3.53, 2.48, NA, 9, 4.18, 1.74, 
3.42, 5.54, 3.74, 3.26, 2.38, 4.42, 3.65), bd2 = c(8.76, NA, 
7.17, 2.82, 6.16, 2.33, 6.05, 2.58, 2.5, NA, 9.04, 4.22, 1.68, 
3.39, 5.52, 3.68, 3.18, 2.38, 4.47, 3.74), bda = c(8.59, NA, 
7.05, 2.78, 6.11, 2.34, 5.96, 3.05, 2.49, NA, 9.02, 4.2, 1.71, 
3.41, 5.53, 3.71, 3.22, 2.38, 4.44, 3.7), ht = c(69.2, NA, 55.2, 
25.7, 47.8, 17.1, 35.6, 11, 12, NA, 52.5, 33.2, 10.4, 16.2, 32.7, 
22.1, 15, 13.6, 24.5, 22.4), spr = c(NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_), stat = c(0L, 1L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), dam = c(NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA), com = c("", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "")), .Names =
c("oplt", 
"tree", "bd1", "bd2", "bda", "ht", "spr", "stat", "dam", "com"
), row.names = c(34L, 462L, 724L, 227L, 228L, 673L, 76L, 123L, 
368L, 632L, 281L, 391L, 495L, 199L, 140L, 528L, 604L, 191L, 358L, 
689L), class = "data.frame")

# dput(ban.nat.1996[sample(row.names(ban.nat.1996), 20),])
bn96 <- structure(list(oplt = c(23L, 33L, 22L, 19L, 27L, 3L, 27L, 19L, 
48L, 31L, 54L, 37L, 42L, 45L, 51L, 51L, 27L, 44L, 22L, 33L), 
    tree = c(616L, 709L, 426L, 233L, 441L, 213L, 411L, 231L, 
    714L, 206L, 355L, 638L, 266L, 201L, 345L, 298L, 151L, 374L, 
    571L, 708L), bd1 = c(4.68, 1.92, 4.89, 4.88, 2.02, 4.2, 2, 
    2.85, 3.38, 2.48, 3.42, NA, 2.1, 2.05, 1.98, 2.26, NA, 5.16, 
    2.9, 2.32), bd2 = c(4.7, 1.98, 4.94, 5.14, NA, 3.96, NA, 
    2.9, 3.2, 2.42, 3.36, NA, 2, 2.02, NA, NA, NA, 5.2, 2.8, 
    2.24), bd = c(4.69, 1.95, 4.92, 5.01, NA, 4.08, NA, 2.88, 
    3.29, 2.45, 3.39, NA, 2.05, 2.03, NA, NA, NA, 5.18, 2.85, 
    2.28), ht = c(31.9, 14.7, 30.7, 24.2, 9.5, 24.3, 8.2, 28.1, 
    22.5, 10.6, 35.5, NA, 13.3, 9.3, 20.2, 10, NA, 24.7, 18.5, 
    15.7), spr = c(NA, NA, NA, 2L, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA), stat = c(0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 
    0L, 0L), dam = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, 2L, NA, NA), yrc = c(95L, 95L, 
    95L, 95L, 93L, 95L, 93L, 95L, 94L, 95L, 95L, 95L, 94L, 96L, 
    93L, 93L, 95L, NA, 95L, 95L), az = c(NA, 172L, NA, NA, 276L, 
    NA, 243L, NA, 221L, 2L, 80L, 270L, 342L, NA, 240L, 170L, 
    6L, NA, NA, 155L), dist = c(NA, 11.21, NA, NA, 8.1, NA, 15.94, 
    NA, 9.11, 9.23, 6.39, 6.45, 10.88, NA, 12.42, 15.19, 15.16, 
    NA, NA, 13.52), com = c("", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "new", "", "")), .Names = c("oplt", 
"tree", "bd1", "bd2", "bd", "ht", "spr", "stat", "dam", "yrc", 
"az", "dist", "com"), row.names = c(366L, 758L, 335L, 285L, 600L, 
61L, 583L, 283L, 1184L, 671L, 1422L, 818L, 930L, 1006L, 1331L, 
1299L, 447L, 996L, 343L, 757L), class = "data.frame")
# sample data
###

*************************************
Silviculture Data Analyst
Ontario Forest Research Institute
Ontario Ministry of Natural Resources
david.john.thompson at ontario.ca
http://ontario.ca/ofri



More information about the R-help mailing list