Re: [R] assign()ing within apply
Any tips? DaveT. -Original Message- From: Thompson, David (MNR) Sent: December 9, 2008 04:03 PM To: 'r-help@r-project.org' Subject: assign()ing within apply Hello, I'm trying to convert a character column in several dataframes to lower case. ### # # Sample data and 'spp' column summaries: # dput(ban.ovs.1993[sample(row.names(ban.ovs.1993), 20), 1:4]) ban.ovs.93 - structure(list(oplt = c(43L, 43L, 38L, 26L, 35L, 8L, 39L, 1L, 34L, 50L, 10L, 29L, 31L, 24L, 18L, 12L, 27L, 49L, 28L, 51L), rplt = 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_), tree = c(427L, 410L, 639L, 494L, 649L, 166L, 735L, 163L, 120L, 755L, 612L, 174L, 129L, 331L, 269L, 152L, 552L, 227L, 243L, 96L), spp = c(MH, MST, MH, HE, BE, MH, MH, MH, MH, Or, IW, Or, MH, MH, BY, MH, MH, BE, MH, MR)), .Names = c(oplt, rplt, tree, spp), row.names = c(4587L, 4570L, 3947L, 2761L, 3653L, 652L, 4136L, 64L, 3567L, 5318L, 838L, 3091L, 3366L, 2423L, 1775L, 1061L, 2893L, 5161L, 2967L, 5395L), class = data.frame) # dput(pem.ovs.1994[sample(row.names(pem.ovs.1994), 20), 1:4]) pem.ovs.94 - structure(list(oplt = c(8L, 17L, 36L, 9L, 31L, 11L, 35L, 51L, 51L, 49L, 40L, 1L, 9L, 17L, 4L, 42L, 6L, 3L, 39L, 25L), tree = c(531L, 557L, 546L, 261L, 592L, 134L, 695L, 933L, 945L, 114L, 34L, 54L, 549L, 574L, 193L, 96L, 70L, 4L, 546L, 789L), spp = c(MH, MH, MH, BF, BF, MH, IW, OR, OR, BF, MH, IW, OR, MH, SM, BE, BE, BE, OR, OR), oaz = c(38L, 205L, 140L, 277L, 329L, 209L, 222L, 24L, 67L, 187L, 156L, 181L, 174L, 248L, 42L, 279L, 273L, 357L, 160L, 183L)), .Names = c(oplt, tree, spp, oaz), row.names = c(1204L, 2943L, 5790L, 1616L, 5063L, 2013L, 5691L, 8188L, 8200L, 7822L, 6302L, 54L, 1698L, 2960L, 421L, 6690L, 775L, 245L, 6205L, 4121L), class = data.frame) # count per spp invisible(lapply(ls(pat='ovs'), function(y) { cat(y, \n) ; print(summary.factor(get(y)[,'spp'])) ; cat(\n) } ) ) ban.ovs.93 BE BY HE IW MH MR MST Or 2 1 1 1 11 1 1 2 pem.ovs.94 BE BF IW MH OR SM 3 3 2 6 5 1 # ### I have tried variants on the following and cannot remember how to have the result assign()ed back to the original dataframes. lapply(ls(pat='ovs'), function(y) { assign(paste(y, [,'spp'], sep=), tolower(get(y)[,'spp'])) } ) I *do* get the expected results: [[1]] [1] mh mst mh he be mh mh mh mh or iw or mh mh by mh mh be mh mr [[2]] [1] mh mh mh bf bf mh iw or or bf mh iw or mh sm be be be or or , I just can't remember how to get them back into the original dataframe objects. Suggestions? Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources david.john.thomp...@ontario.ca http://ontario.ca/ofri * __ R-help@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.
Re: [R] assign()ing within apply
Thanks Jorge, I am aware of the method you suggest, however, it's not quite what I was asking for. I was attempting to do this change on 56 dataframes, so I attempted to do so with a list of all rather than each individually. Thanks, DaveT. From: Jorge Ivan Velez [mailto:jorgeivanve...@gmail.com] Sent: December 12, 2008 11:11 AM To: Thompson, David (MNR) Subject: Re: [R] assign()ing within apply Dear David, Try this: ban.ovs.93[,'spp']-tolower(ban.ovs.93[,'spp']) ban.ovs.93 pem.ovs.94[,'spp']-tolower(pem.ovs.94[,'spp']) pem.ovs.94 HTH, Jorge On Fri, Dec 12, 2008 at 9:51 AM, Thompson, David (MNR) david.john.thomp...@ontario.ca wrote: Any tips? DaveT. -Original Message- From: Thompson, David (MNR) Sent: December 9, 2008 04:03 PM To: 'r-help@r-project.org' Subject: assign()ing within apply Hello, I'm trying to convert a character column in several dataframes to lower case. ### # # Sample data and 'spp' column summaries: # dput(ban.ovs.1993[sample(row.names(ban.ovs.1993), 20), 1:4]) ban.ovs.93 - structure(list(oplt = c(43L, 43L, 38L, 26L, 35L, 8L, 39L, 1L, 34L, 50L, 10L, 29L, 31L, 24L, 18L, 12L, 27L, 49L, 28L, 51L), rplt = 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_), tree = c(427L, 410L, 639L, 494L, 649L, 166L, 735L, 163L, 120L, 755L, 612L, 174L, 129L, 331L, 269L, 152L, 552L, 227L, 243L, 96L), spp = c(MH, MST, MH, HE, BE, MH, MH, MH, MH, Or, IW, Or, MH, MH, BY, MH, MH, BE, MH, MR)), .Names = c(oplt, rplt, tree, spp), row.names = c(4587L, 4570L, 3947L, 2761L, 3653L, 652L, 4136L, 64L, 3567L, 5318L, 838L, 3091L, 3366L, 2423L, 1775L, 1061L, 2893L, 5161L, 2967L, 5395L), class = data.frame) # dput(pem.ovs.1994[sample(row.names(pem.ovs.1994), 20), 1:4]) pem.ovs.94 - structure(list(oplt = c(8L, 17L, 36L, 9L, 31L, 11L, 35L, 51L, 51L, 49L, 40L, 1L, 9L, 17L, 4L, 42L, 6L, 3L, 39L, 25L), tree = c(531L, 557L, 546L, 261L, 592L, 134L, 695L, 933L, 945L, 114L, 34L, 54L, 549L, 574L, 193L, 96L, 70L, 4L, 546L, 789L), spp = c(MH, MH, MH, BF, BF, MH, IW, OR, OR, BF, MH, IW, OR, MH, SM, BE, BE, BE, OR, OR), oaz = c(38L, 205L, 140L, 277L, 329L, 209L, 222L, 24L, 67L, 187L, 156L, 181L, 174L, 248L, 42L, 279L, 273L, 357L, 160L, 183L)), .Names = c(oplt, tree, spp, oaz), row.names = c(1204L, 2943L, 5790L, 1616L, 5063L, 2013L, 5691L, 8188L, 8200L, 7822L, 6302L, 54L, 1698L, 2960L, 421L, 6690L, 775L, 245L, 6205L, 4121L), class = data.frame) # count per spp invisible(lapply(ls(pat='ovs'), function(y) { cat(y, \n) ; print(summary.factor(get(y)[,'spp'])) ; cat(\n) } ) ) ban.ovs.93 BE BY HE IW MH MR MST Or 2 1 1 1 11 1 1 2 pem.ovs.94 BE BF IW MH OR SM 3 3 2 6 5 1 # ### I have tried variants on the following and cannot remember how to have the result assign()ed back to the original dataframes. lapply(ls(pat='ovs'), function(y) { assign(paste(y, [,'spp'], sep=), tolower(get(y)[,'spp'])) } ) I *do* get the expected results: [[1]] [1] mh mst mh he be mh mh mh mh or iw or mh mh by mh mh be mh mr [[2]] [1] mh mh mh bf bf mh iw or or bf mh iw or mh sm be be be or or , I just can't remember how to get them back into the original dataframe objects. Suggestions? Thanx, DaveT. * Silviculture Data Analyst
[R] assign()ing within apply
Hello, I'm trying to convert a character column in several dataframes to lower case. ### # # Sample data and 'spp' column summaries: # dput(ban.ovs.1993[sample(row.names(ban.ovs.1993), 20), 1:4]) ban.ovs.93 - structure(list(oplt = c(43L, 43L, 38L, 26L, 35L, 8L, 39L, 1L, 34L, 50L, 10L, 29L, 31L, 24L, 18L, 12L, 27L, 49L, 28L, 51L), rplt = 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_), tree = c(427L, 410L, 639L, 494L, 649L, 166L, 735L, 163L, 120L, 755L, 612L, 174L, 129L, 331L, 269L, 152L, 552L, 227L, 243L, 96L), spp = c(MH, MST, MH, HE, BE, MH, MH, MH, MH, Or, IW, Or, MH, MH, BY, MH, MH, BE, MH, MR)), .Names = c(oplt, rplt, tree, spp), row.names = c(4587L, 4570L, 3947L, 2761L, 3653L, 652L, 4136L, 64L, 3567L, 5318L, 838L, 3091L, 3366L, 2423L, 1775L, 1061L, 2893L, 5161L, 2967L, 5395L), class = data.frame) # dput(pem.ovs.1994[sample(row.names(pem.ovs.1994), 20), 1:4]) pem.ovs.94 - structure(list(oplt = c(8L, 17L, 36L, 9L, 31L, 11L, 35L, 51L, 51L, 49L, 40L, 1L, 9L, 17L, 4L, 42L, 6L, 3L, 39L, 25L), tree = c(531L, 557L, 546L, 261L, 592L, 134L, 695L, 933L, 945L, 114L, 34L, 54L, 549L, 574L, 193L, 96L, 70L, 4L, 546L, 789L), spp = c(MH, MH, MH, BF, BF, MH, IW, OR, OR, BF, MH, IW, OR, MH, SM, BE, BE, BE, OR, OR), oaz = c(38L, 205L, 140L, 277L, 329L, 209L, 222L, 24L, 67L, 187L, 156L, 181L, 174L, 248L, 42L, 279L, 273L, 357L, 160L, 183L)), .Names = c(oplt, tree, spp, oaz), row.names = c(1204L, 2943L, 5790L, 1616L, 5063L, 2013L, 5691L, 8188L, 8200L, 7822L, 6302L, 54L, 1698L, 2960L, 421L, 6690L, 775L, 245L, 6205L, 4121L), class = data.frame) # count per spp invisible(lapply(ls(pat='ovs'), function(y) { cat(y, \n) ; print(summary.factor(get(y)[,'spp'])) ; cat(\n) } ) ) ban.ovs.93 BE BY HE IW MH MR MST Or 2 1 1 1 11 1 1 2 pem.ovs.94 BE BF IW MH OR SM 3 3 2 6 5 1 # ### I have tried variants on the following and cannot remember how to have the result assign()ed back to the original dataframes. lapply(ls(pat='ovs'), function(y) { assign(paste(y, [,'spp'], sep=), tolower(get(y)[,'spp'])) } ) I *do* get the expected results: [[1]] [1] mh mst mh he be mh mh mh mh or iw or mh mh by mh mh be mh mr [[2]] [1] mh mh mh bf bf mh iw or or bf mh iw or mh sm be be be or or , I just can't remember how to get them back into the original dataframe objects. Suggestions? Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ontario.ca/ofri __ R-help@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.
Re: [R] a bit off topic: data for dragsters?
Roland, I don't know of the specific data you are asking for but the official website of the 'National Hot Rod Assoc' (?) is www.nhra.com. DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ontario.ca/ofri * Dear all, does anyone of you know where to get some simple data from dragster racing (the race where you go for 1/4 mile straight)? Best would be some data where you have two columns with Col 1 Time since start of race in milliseconds and Col 2 Distance traveled. Thank you very much, Roland __ R-help@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.
Re: [R] Date classes in ggplot2
Is not melt part of reshape package, i.e., require(reshape)? DaveT. -Original Message- From: stephen sefick [mailto:[EMAIL PROTECTED] Sent: October 19, 2008 12:12 PM To: hadley wickham Cc: R Help Subject: Re: [R] Date classes in ggplot2 well I must have either resolve it without knowing it, ot something funny is going on... sorry I didn't run it in a clean R session. Should have. works fine thanks Stephen Sefick On Sun, Oct 19, 2008 at 12:01 PM, hadley wickham [EMAIL PROTECTED] wrote: On Sun, Oct 19, 2008 at 10:49 AM, stephen sefick [EMAIL PROTECTED] wrote: updn.gg - (structure(list(date = structure(c(11808, 11869, 11961, 11992, 12084, 12173, 12265, 12418, 12600, 12631, 12753, 12996, 13057, 13149), class = Date), unrestored = c(1.13789418691602, 0.704948049842955, 0.276777348238899, 0.417586861554189, 0.504870337754768, 0.673201771716216, 0.560704221510771, 0.835737007551542, 1.10773858390693, 0.197070828834836, 0.942350681588179, 0.950447141061461, 0.246637790002705, 0.324035567509960 ), restored = c(1.39981554315924, 0.89196314359498, 0.407816250252697, 0.823496839063978, 1.14429021220358, 1.23971035967413, 0.960868900583432, 0.927685306209829, 1.22072345292821, 0.249842897450642, 1.00879641624694, 0.925372139878243, 0.317259909172362, 0.382677149697482)), .Names = c(date, unrestored, restored), row.names = c(NA, -14L), class = data.frame)) #I would like to do this in ggplot xyplot(unrestored+restored~date, data=updn.gg, type=c(l), auto.key=TRUE) #this is what I have tried and get an error message melt.updn - melt(updn.gg, id.var=date) What error message? It works for me. Hadley -- http://had.co.nz/ -- Stephen Sefick Research Scientist Southeastern Natural Sciences Academy Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis __ R-help@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.
Re: [R] package Utils Sweave Example Error
(Question/suggestion inline below) -Original Message- From: Duncan Murdoch [mailto:[EMAIL PROTECTED] Sent: October 16, 2008 02:17 PM To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Subject: Re: [R] package Utils Sweave Example Error On 10/16/2008 12:19 PM, Felipe Carrillo wrote: This is my sessionInfo() and Sys.getenv(PATH) library(tools) testfile - system.file(Sweave, Sweave-test-1.Rnw, package = utils) ## enforce par(ask=FALSE) options(device.ask.default=FALSE) ## create a LaTeX file Sweave(testfile) Writing to file Sweave-test-1.tex Processing code chunks ... 1 : print term verbatim 2 : term hide 3 : echo print term verbatim 4 : term verbatim 5 : echo term verbatim 6 : echo term verbatim eps pdf 7 : echo term verbatim eps pdf You can now run LaTeX on 'Sweave-test-1.tex' ## This can be compiled to PDF by texi2dvi(Sweave-test-1.tex, pdf=TRUE,quiet=FALSE) This is pdfTeX, Version 3.1415926-1.40.9 (MiKTeX 2.7) entering extended mode (C:/Program Files/R/R-2.7.2/bin/Sweave-test-1.tex LaTeX2e 2005/12/01 Babel v3.8l and hyphenation patterns for english, dumylang, nohyphenation, ge rman, ngerman, german-x-2008-06-18, ngerman-x-2008-06-18, french, loaded. (C:\Program Files\MiKTeX 2.7\tex\latex\base\article.cls Document Class: article 2005/09/16 v1.4f Standard LaTeX document class (C:\Program Files\MiKTeX 2.7\tex\latex\base\size10.clo)) (C:\Program Files\MiKTeX 2.7\tex\latex\ltxmisc\a4wide.sty (C:\Program Files\MiKTeX 2.7\tex\latex\ntgclass\a4.sty)) ! Missing \endcsname inserted. to be read again \protect l.11 \begin {document} ? pdflatex.EXE: Bad file descriptor This appears to be the problem. I'm not certain, but I would guess if you look in the C:/Program Files/R/R-2.7.2/bin/Sweave-test-1.tex file you'll see a line something like this: \usepackage{C:/Program Files/R/R-2.7.2/share/texmf/Sweave} That line (which was added by Sweave) will not work, because LaTeX does not understand blanks in file paths. There are a couple of workarounds: Does the Windoze convention of using C:/Progra~1/ to represent C:/Program Files/ not work in these situations? DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ontario.ca/ofri * 1. Install R in a directory that doesn't contain blanks. 2. Include \usepackage{Sweave} in the Rnw file you're processing; this will stop Sweave from adding anything. I believe we set things up so that MikTeX will find it in the right place (though I don't normally use texi2dvi, so I'm not sure). To test this, you can just go and edit the Sweave-test-1.tex file now, replacing the long line with the short version. Duncan Murdoch texify: pdflatex failed for some reason (see log file). Error in texi2dvi(Sweave-test-1.tex, pdf = TRUE, quiet = FALSE) : running 'texi2dvi' on 'Sweave-test-1.tex' failed sessionInfo() R version 2.7.2 (2008-08-25) i386-pc-mingw32 locale: LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252 attached base packages: [1] tools stats graphics grDevices utils datasets methods base Sys.getenv(PATH) PATH C:\\Program Files\\MiKTeX 2.7\\miktex\\bin;C:\\GTK\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS ;C:\\WINDOWS\\System32\\Wbem;C:\\Program Files\\SYSTAT 10\\;C:\\Program Files\\SYSTAT 10\\XGRAPH\\;C:\\Program Files\\Wave Systems Corp\\Dell Preboot Manager\\Access Client\\v5\\;C:\\Program Files\\ATI Technologies\\ATI.ACE\\Core-Static;c:\\Program Files\\Microsoft SQL Server\\90\\Tools\\binn\\ Thank you Felipe D. Carrillo Supervisory Fishery Biologist Department of the Interior US Fish Wildlife Service California, USA --- On Thu, 10/16/08, Duncan Murdoch [EMAIL PROTECTED] wrote: Try running it as texi2dvi(Sweave-test-1.tex, pdf=TRUE, quiet=FALSE), and post the list of error messages. You should also show us the results of sessionInfo() and Sys.getenv(PATH), and tell us which version of LaTeX you have installed. Duncan Murdoch __ R-help@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. __ R-help@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.
Re: [R] YALAQ - Yet Another LApply Question
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-5tools_2.7.2 sysname release version machine Windows XP build 2600, Service Pack 2x86 # 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 =
Re: [R] YALAQ - Yet Another LApply Question
Thank you very much Greg. Apparently, my learning curve is still well below it's horizontal asymptote. And no wonder in the vast universe of the R-project, eh? Thanx, DaveT. -Original Message- From: Greg Snow [mailto:[EMAIL PROTECTED] Sent: October 15, 2008 01:52 PM To: Thompson, David (MNR); r-help@r-project.org Subject: RE: YALAQ - Yet Another LApply Question For the first question, ?str tells us that the str function does not return anything, but has a side effect of printing information to the console. So in version 1 when you call the cat function it evaluates its arguments and as it evaluates str(...) the information is printed, then nothing is passed into cat which then prints the name of the object (after str has done its printing). In the second version cat finishes printing the name before str is called so that it is in the correct order. You might prefer to use lapply/sapply to create a list with all the pieces of interest, then just do str on the resulting list, or look at the TkListView function in the TeachingDemos package for another way to look at the structure of a list. For example: TkListView( sapply( ls(pattern='bn'), function(y) get(y), simplify=FALSE, USE.NAMES=TRUE ) ) For the 2nd question, the difference is that the summary function does not print anything, just returns the summary information. When you type summary at the command line the parser automatically calls the print method for the summary object returned, but within functions/loops/apply the summary object is returned (included in the output of lapply) but not printed. You need to explicitly print the results of summary. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] 801.408.8111 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] project.org] On Behalf Of Thompson, David (MNR) Sent: Wednesday, October 15, 2008 10:19 AM To: r-help@r-project.org Subject: Re: [R] YALAQ - Yet Another LApply Question 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-5tools_2.7.2 sysname release version machine Windows XP build 2600, Service Pack 2x86 # 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_
[R] Fatal error: unable to restore saved data in .RData
Hello, I am unable to start an R session by double-clicking on an existing .RData file from the Windoze file explorer. A dialogue appears with the message Fatal error: unable to restore saved data in .RData, and I notice in the R Console the last few lines are: Loading required package: R2HTML NULL error in function () : unused argument(s) (1:43) I tried starting a fresh R session, changing the working directory, saving a new empty workspace, closing R, and restarting with the new (empty) .RData file to the same effect. I tried with the --vanilla option, changing the working directory and loading the existing workspace, and get the error message: Error in function () : unused argument(s) (1:43) And again with the --no-restore-data option, same effect. Suggestions? 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-5tools_2.7.2 sysname release version machine Windows XP build 2600, Service Pack 2 x86 # system info: ### * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ontario.ca/ofri __ R-help@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.
Re: [R] Fatal error: unable to restore saved data in .RData
Hello, I may have found the problem, although I don't understand it. When I commented out the following line in my Rprofile.site file the problem _seemed_ to disappear. # ans as shorthand for .Last.value # makeActiveBinding(ans, function() .Last.value, .GlobalEnv) I decided to do this after reading the help for bindenv {base} and the phrase . . . experimental interface . . . JUMPED right out at me. Again, I don't know if this was _actually_ the cause of my problem but, I am underway again. Thanx, DaveT. -Original Message- From: Thompson, David (MNR) Sent: October 10, 2008 10:26 AM To: 'r-help@r-project.org' Subject: Fatal error: unable to restore saved data in .RData Hello, I am unable to start an R session by double-clicking on an existing .RData file from the Windoze file explorer. A dialogue appears with the message Fatal error: unable to restore saved data in .RData, and I notice in the R Console the last few lines are: Loading required package: R2HTML NULL error in function () : unused argument(s) (1:43) I tried starting a fresh R session, changing the working directory, saving a new empty workspace, closing R, and restarting with the new (empty) .RData file to the same effect. I tried with the --vanilla option, changing the working directory and loading the existing workspace, and get the error message: Error in function () : unused argument(s) (1:43) And again with the --no-restore-data option, same effect. Suggestions? 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-5tools_2.7.2 sysname release version machine Windows XP build 2600, Service Pack 2x86 # system info: ### * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ontario.ca/ofri * __ R-help@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.
[R] YALAQ - Yet Another LApply Question
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-5tools_2.7.2 sysname release version machine Windows XP build 2600, Service Pack 2x86 # 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,
[R] YAPMQ - Yet Another PlotMath Question
Hello, I'm having trouble finding (remembering) how to pass values into text functions in plots, as demonstrated by: slope - 45 ; plot(1:10) ; text(2, 7, labels = expression(45~degree)) ; text(2, 5, labels = paste(bquote(.(slope)), expression(degree))) Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ontario.ca/ofri __ R-help@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.
Re: [R] YAPMQ - Yet Another PlotMath Question
-Original Message- From: Peter Dalgaard [mailto:[EMAIL PROTECTED] Sent: June 19, 2008 04:23 PM To: Thompson, David (MNR) Cc: r-help@r-project.org Subject: Re: [R] YAPMQ - Yet Another PlotMath Question Thompson, David (MNR) wrote: Hello, I'm having trouble finding (remembering) how to pass values into text functions in plots, as demonstrated by: slope - 45 ; plot(1:10) ; text(2, 7, labels = expression(45~degree)) ; text(2, 5, labels = paste(bquote(.(slope)), expression(degree))) text(2, 5, labels = bquote(paste(.(slope), degree))) Notice that paste()ing two expressions does not yield an expression result. Instead, it deparses both arguments and gives a character string. Thank you Peter. Are all the syntactic elements noted in plotmath{grDevices} available to bquote()? * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ontario.ca/ofri __ R-help@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.
Re: [R] YAPMQ - Yet Another PlotMath Question
-Original Message- From: Katharine Mullen [mailto:[EMAIL PROTECTED] Sent: June 19, 2008 04:26 PM To: Thompson, David (MNR) Cc: r-help@r-project.org Subject: Re: [R] YAPMQ - Yet Another PlotMath Question by example: slope - 45 plot(1:10) text(2, 7, labels = expression(45~degree)) ; text(2, 5, labels = substitute(paste(slope * degree), list(slope=45))) text(2, 3, labels = substitute(paste(lambda * = * mt * (nm)), list(mt = 33))) On Thu, 19 Jun 2008, Thompson, David (MNR) wrote: Hello, I'm having trouble finding (remembering) how to pass values into text functions in plots, as demonstrated by: slope - 45 ; plot(1:10) ; text(2, 7, labels = expression(45~degree)) ; text(2, 5, labels = paste(bquote(.(slope)), expression(degree))) Thank you Katharine. I suspected this would be one of those cats that you could skin nine-ways-from-Sunday. Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ontario.ca/ofri __ R-help@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.
[R] echo Rprofile.site on startup
Hello, I have not been able to find how to have R echo the Rprofile.site contents to the console on start-up. Changing the windoze shortcut to include the '--verbose' option does not show what I am looking for. I would like to see the options and commands listed, in addition to the library() loadings, as they are processed. Examples of the type of options I like and shortcut-commands (aliases) that I collect to shrink my keystroke count are: options(digits=2) options(graphics.record=TRUE) # show head and tail together so - function(x, n=20) { print(head(x, n)) ; print(tail(x, n)) } # shorthand for part of Sys.info() and sessionInfo() si - function() { Sys.info()[c(1,2,3,5)] ; sessionInfo() } I would like to view these as processed when I am changing options and adding aliases so that I may know which parts are successfully/properly defined. Does the same process work for all of Rprofile.site, .Rprofile, .First, .Last (, etc.?) profile files? Is there a 'best practice' on which of the profile files one should use? I use only the Rprofile.site since I am the only user on my PC. Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ontario.ca/ofri * Sys.info() sysname release version machine Windows XP build 2600, Service Pack 2x86 sessionInfo() R version 2.7.0 (2008-04-22) 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] datasets grDevices splines grid graphics stats tcltk utils methods base other attached packages: [1] ggplot2_0.6colorspace_0.95RColorBrewer_1.0-2 MASS_7.2-42proto_0.3-8reshape_0.8.0 car_1.2-8 svSocket_0.9-5 [9] svIO_0.9-5 R2HTML_1.59svMisc_0.9-5 svIDE_0.9-5 loaded via a namespace (and not attached): [1] tools_2.7.0 __ R-help@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.
Re: [R] ggplot questions
Thanx Thierry, Suggestion #1 had no effect. I have been playing with variants on #2 along the way. DaveT. -Original Message- From: ONKELINX, Thierry [mailto:[EMAIL PROTECTED] Sent: June 6, 2008 04:02 AM To: Thompson, David (MNR); hadley wickham Cc: r-help@r-project.org Subject: RE: [R] ggplot questions David, 1. Try scale_x_continuous(lim = c(0, 360)) + scale_y_continuous(lim = c(0, 16)) 2. You could set the colour of the gridlines equal to the backgroup colour with ggopt HTH, Thierry __ R-help@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.
Re: [R] ggplot questions
OK, The original ggplot() construct (below) on the following two dataframes (test1, test2) generate different outputs, which I have attached. The output that I expect is that shown in test2.png. My expectations are that I have set the plotting limits with 'scale_x_continuous(lim = c(0, 360)) + scale_y_continuous(lim = c(0, 16))' so, both data sets should produce the same output except for the 'o' at plot center and the 'N' at the top. The only difference in the two dataframes are inclusion of first two rows in test2 with rplt column changed to character: test2[1:2,] oplt rplt az dist 10o 00 20N 360 16 Ahhh, wait a second! In composing this message I may have found the problem. It appears that including the 'scale_x_continuous()' component twice in my original version was causing (?) the erratic behaviour. And I have confirmed that the ordering of the layer, scale* and coord* components does not affect the output. However, I'm still getting more x-breaks than requested with radial lines corresponding to 45, 135, 225, 315 degrees (NE, SE, SW, NW). Still open to suggestions on that. # new version working with both dataframes ggplot() + coord_polar() + layer( data = test1, mapping = aes(x = az, y = dist, label = rplt), geom = text) + scale_x_continuous(lim = c(0, 360), breaks=c(90, 180, 270, 360), labels=c('E', 'S', 'W', 'N')) + scale_y_continuous(lim = c(0, 16), breaks=c(0, 4, 8, 12, 16), labels=c('centre', '4m', '8m', '12m', '16m')) ## ## ## # original version NOT WORKING with test1 ggplot() + coord_polar() + scale_x_continuous(lim = c(0, 360)) + scale_y_continuous(lim = c(0, 16)) + layer( data = test, mapping = aes(x = az, y = dist, label = rplt), geom = text) + scale_x_continuous(breaks=c(90, 180, 270, 360), labels=c('90', '180', '270', '360')) # data generating test1.png test1 -structure(list(oplt = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L ), rplt = 1:10, az = c(57L, 94L, 96L, 152L, 182L, 185L, 227L, 264L, 332L, 354L), dist = c(4.09, 2.8, 7.08, 7.09, 3.28, 7.85, 6.12, 1.97, 7.68, 7.9)), .Names = c(oplt, rplt, az, dist ), row.names = c(NA, 10L), class = data.frame) # data generating test2.png test2 - structure(list(oplt = c(0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), rplt = c(o, N, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), az = c(0, 360, 57, 94, 96, 152, 182, 185, 227, 264, 332, 354), dist = c(0, 16, 4.09, 2.8, 7.08, 7.09, 3.28, 7.85, 6.12, 1.97, 7.68, 7.9)), .Names = c(oplt, rplt, az, dist), row.names = c(NA, 12L), class = data.frame) Many, many thanks for your patience and perseverance on this one Hadley, DaveT. -Original Message- From: hadley wickham [mailto:[EMAIL PROTECTED] Sent: June 6, 2008 02:06 PM To: Thompson, David (MNR) Cc: r-help@r-project.org Subject: Re: [R] ggplot questions Does the difference have something to do with ggplot() using ranges derived from the data? When I modify my original 'test' dataframe with two extra rows as defined below, I get expected results in both versions. Order shouldn't matter - and if it's making a difference, that's a bug. But I'm still not completely sure what you're expecting. This highlights my next question (warned you ;-) ), I have been unsuccessful in trying to define fixed plotting ranges to generate a 'template' graphic that I may reuse with successive 'overstory plot' data sets. I have used '+ xlim(0, 360) + ylim(0, 16)' but, this seems to not have any effect on the final plot layout. Could you please produce a small reproducible example that demonstrates this? It may well be a bug. Hadley -- http://had.co.nz/ attachment: test1.pngattachment: test2.png__ R-help@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.
Re: [R] ggplot questions
Thanx Hadley, More questions inline. -Original Message- From: hadley wickham [mailto:[EMAIL PROTECTED] Sent: June 5, 2008 01:09 PM To: Thompson, David (MNR) Cc: r-help@r-project.org Subject: Re: [R] ggplot questions On Wed, Jun 4, 2008 at 2:03 PM, Thompson, David (MNR) [EMAIL PROTECTED] wrote: Hello, A few questions about the following examples: 1. Why do the two plotting versions not produce the same result? Because version one has two layers, and version two has one? Does the difference have something to do with ggplot() using ranges derived from the data? When I modify my original 'test' dataframe with two extra rows as defined below, I get expected results in both versions. This highlights my next question (warned you ;-) ), I have been unsuccessful in trying to define fixed plotting ranges to generate a 'template' graphic that I may reuse with successive 'overstory plot' data sets. I have used '+ xlim(0, 360) + ylim(0, 16)' but, this seems to not have any effect on the final plot layout. 2. Is the 'scale_x_continuous' (or *_y_* or *_*_discrete) geom the best way to setup grids (as in visual guide-lines) in polar (or for that matter, any) coordinate system? It's one way. Another way is to use geom_hline and geom_vline. (Also note that scale_x_continuous is a _scale_ not a geom) OK. So, how do I suppress automatic generation of plot grids so that I could define my own with geom_hline and geom_vline? 3. Why do these commands appear to generate 3 plot pages each? Could you provide a bit more detail here please? Sorry Hadley, I could not reproduce the same behaviour. I had restarted my computer since then so, maybe, I was just suffering from the usual leaky-resources-M$-syndrome. Thanks, Hadley -- http://had.co.nz/ # for ease of reference: # the two original dataframes test - structure(list(oplt = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L ), rplt = 1:10, az = c(57L, 94L, 96L, 152L, 182L, 185L, 227L, 264L, 332L, 354L), dist = c(4.09, 2.8, 7.08, 7.09, 3.28, 7.85, 6.12, 1.97, 7.68, 7.9)), .Names = c(oplt, rplt, az, dist ), row.names = c(NA, 10L), class = data.frame) labs - structure(list(oplt = c(0, 0, 0, 0, 0), rplt = structure(c(3L, 1L, 4L, 5L, 2L), .Label = c(E, N, o, S, W), class = factor), az = c(0, 90, 180, 270, 360), dist = c(0, 16, 16, 16, 16)), .Names = c(oplt, rplt, az, dist), row.names = c(NA, -5L), class = data.frame) # the new dataframe test2 - structure(list(oplt = c(0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), rplt = c(o, N, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), az = c(0, 360, 57, 94, 96, 152, 182, 185, 227, 264, 332, 354), dist = c(0, 16, 4.09, 2.8, 7.08, 7.09, 3.28, 7.85, 6.12, 1.97, 7.68, 7.9)), .Names = c(oplt, rplt, az, dist), row.names = c(NA, 12L), class = data.frame) ### plot version 1, setup plot structure first, add 'data' points later ggplot() + coord_polar() + layer( data = labs, mapping = aes(x = az, y = dist, label = rplt), geom = text) + scale_x_continuous(breaks=c(90, 180, 270, 360)) + layer( data = test, mapping = aes(x = az, y = dist, label = rplt), geom = text) ### plot version 2, try the same all in one step ggplot() + coord_polar() + layer( data = test, mapping = aes(x = az, y = dist, label = rplt), geom = text) + scale_x_continuous(breaks=c(90, 180, 270, 360), labels=c('90', '180', '270', '360')) sessionInfo() R version 2.7.0 (2008-04-22) 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] datasets grDevices splines grid graphics stats tcltk utils methods base other attached packages: [1] ggplot2_0.6colorspace_0.95RColorBrewer_1.0-2 MASS_7.2-42proto_0.3-8reshape_0.8.0 car_1.2-8 [8] svSocket_0.9-5 svIO_0.9-5 R2HTML_1.59 svMisc_0.9-5 svIDE_0.9-5 Thanx again, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ontario.ca/ofri __ R-help@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.
[R] ggplot questions
Hello, A few questions about the following examples: 1. Why do the two plotting versions not produce the same result? 2. Is the 'scale_x_continuous' (or *_y_* or *_*_discrete) geom the best way to setup grids (as in visual guide-lines) in polar (or for that matter, any) coordinate system? 3. Why do these commands appear to generate 3 plot pages each? 4. Perhaps more questions to follow ;-) ### the data test - structure(list(oplt = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L ), rplt = 1:10, az = c(57L, 94L, 96L, 152L, 182L, 185L, 227L, 264L, 332L, 354L), dist = c(4.09, 2.8, 7.08, 7.09, 3.28, 7.85, 6.12, 1.97, 7.68, 7.9)), .Names = c(oplt, rplt, az, dist ), row.names = c(NA, 10L), class = data.frame) labs - structure(list(oplt = c(0, 0, 0, 0, 0), rplt = structure(c(3L, 1L, 4L, 5L, 2L), .Label = c(E, N, o, S, W), class = factor), az = c(0, 90, 180, 270, 360), dist = c(0, 16, 16, 16, 16)), .Names = c(oplt, rplt, az, dist), row.names = c(NA, -5L), class = data.frame) ### plot version 1, setup plot structure first, add 'data' points later ggplot() + coord_polar() + layer( data = labs, mapping = aes(x = az, y = dist, label = rplt), geom = text) + scale_x_continuous(breaks=c(90, 180, 270, 360)) + layer( data = test, mapping = aes(x = az, y = dist, label = rplt), geom = text) ### plot version 2, try the same all in one step ggplot() + coord_polar() + layer( data = test, mapping = aes(x = az, y = dist, label = rplt), geom = text) + scale_x_continuous(breaks=c(90, 180, 270, 360), labels=c('90', '180', '270', '360')) ### the scenario I am generating graphics to show the physical layout of a forestry research experiment. There are 54 'cut blocks' (64x64m, 0.4ha) each containing 1 circular 'overstory plot' (16m radius, 0.08ha). Each 'overstory plot' (oplt) contains 10 circular 'regeneration plots' (rplt) (0.56m radius, 1m^2 or 1.13m radius, 4m^2), these are the label data points plotted below, later to have the 1m^2 or 4m^2 rplt outline added. Later, each individual tree will be plotted across each oplt in the views below and each rplt in expanded views. All locations (rplt centers, tree positions) are recorded as azimuth and distance from their respective plot centers, hence my inclination to use polar coordinates. Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ontario.ca/ofri __ R-help@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.
Re: [R] Thank you
Yes! Again, thank you ALL very, very much. Even simply lurking on the list generates many gems worth collecting. DaveT. From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] I totally agree both of you. This is a super place to mature the R. I learn a lot from this R heaven! Chunhao Quoting Esmail Bonakdarian [EMAIL PROTECTED]: Hear hear! .. I've benefited greatly from reading the postings here and some of the members have been very generous with their knowledge too! Esmail Tubin wrote: In the past few weeks I have had to give myself a crash course in R, in order to accomplish some necessary tasks for my job. During that time, I've found this forum to be helpful time and time again - usually I find the answer to my problem by searching the archives; once or twice I've posted questions and been given near-immediate solutions. I just wanted to thank the forum participants for all of their contributions over the years! __ R-help@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.
Re: [R] list manipulation
Beautiful! Thanks Jim. DaveT. -Original Message- From: jim holtman [mailto:[EMAIL PROTECTED] Sent: May 6, 2008 07:33 PM To: Thompson, David (MNR) Subject: Re: [R] list manipulation The reason for the NULLs is that is the output of the lapply you are executing. If you don't want to see the value, then use 'invisible' or assign to an object since you are executing this at the command level, the default is to print the value. invisible(lapply(...)) On Tue, May 6, 2008 at 2:35 PM, Thompson, David (MNR) [EMAIL PROTECTED] wrote: Hello, I have a set of one-liners (many thanks to previous responses from this list) that I use to look at newly imported data sets with functions like dim(), names(), str(), etc. within lapply(). Generally, these commands work for me but, I am apparently still missing some aspect of list manipulation. I don't understand why I get a set of NULL list elements at the end of each output as demonstrated below. How can I generate this (and similar) result(s) without all the trailing NULLs? lapply(ls(pattern='bn'), function(x) cat(x, dim(get(x)), \t, names(get(x)), \n)) bn1993 2885 11 oplt rplt rsiz tree bd ht oaz odst raz rdst spr bn1994 3158 7oplt tree bd ht spr stat dam bn1995 734 7 oplt tree bd ht spr stat dam bn1996 293 7 oplt tree bd ht spr stat dam bn1997 264 7 oplt tree bd ht spr stat dam bn1998 768 7 oplt tree bd ht spr stat dam bn1999 654 7 oplt tree bd ht dbh stat dam bn2003 1407 9oplt tree bd94 ht94 ht99 ht02 ht03 stat dam [[1]] NULL [[2]] NULL [[3]] NULL [[4]] NULL [[5]] NULL [[6]] NULL [[7]] NULL [[8]] NULL Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@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.
[R] list manipulation
Hello, I have a set of one-liners (many thanks to previous responses from this list) that I use to look at newly imported data sets with functions like dim(), names(), str(), etc. within lapply(). Generally, these commands work for me but, I am apparently still missing some aspect of list manipulation. I don't understand why I get a set of NULL list elements at the end of each output as demonstrated below. How can I generate this (and similar) result(s) without all the trailing NULLs? lapply(ls(pattern='bn'), function(x) cat(x, dim(get(x)), \t, names(get(x)), \n)) bn1993 2885 11 oplt rplt rsiz tree bd ht oaz odst raz rdst spr bn1994 3158 7oplt tree bd ht spr stat dam bn1995 734 7 oplt tree bd ht spr stat dam bn1996 293 7 oplt tree bd ht spr stat dam bn1997 264 7 oplt tree bd ht spr stat dam bn1998 768 7 oplt tree bd ht spr stat dam bn1999 654 7 oplt tree bd ht dbh stat dam bn2003 1407 9oplt tree bd94 ht94 ht99 ht02 ht03 stat dam [[1]] NULL [[2]] NULL [[3]] NULL [[4]] NULL [[5]] NULL [[6]] NULL [[7]] NULL [[8]] NULL Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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.
Re: [R] list manipulation
Thanks Mark, Your suggestion led me to this: !is.null(lapply(ls(pattern='bn'), function(y) cat(y, dim(get(y)), \t, names(get(y)), \n))) bn1993 2885 11 oplt rplt rsiz tree bd ht oaz odst raz rdst spr bn1994 3158 7oplt tree bd ht spr stat dam bn1995 734 7 oplt tree bd ht spr stat dam bn1996 293 7 oplt tree bd ht spr stat dam bn1997 264 7 oplt tree bd ht spr stat dam bn1998 768 7 oplt tree bd ht spr stat dam bn1999 654 7 oplt tree bd ht dbh stat dam bn2003 1407 9oplt tree bd94 ht94 ht99 ht02 ht03 stat dam [1] TRUE Which is far cleaner and more compact (which was probably what I was _actually_ looking for) than what I had started with. Thanx, DaveT. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: May 6, 2008 02:47 PM To: Thompson, David (MNR) Subject: RE: [R] list manipulation Hi: i would guess that you are sending objects into the lapply that dim and names don't work on so you get NULL in those cases. you can probably figure out what's happening by doing ls(pattern='bn') and seeing what gets returned ? There could be some other reason but that's my best guess. if you don't care why and you just want to get rid of them you can do initialoutput-lapply( that stuff) finaloutput-initialoutput[!sapply(initialoutput,is.null] print(finaloutput) i'm no R expert by any stretch but, if you want to get better at R , the best way is to live on this list if possible and watch the questions and solutions. for me, it beats any book out there. good luck and let me know if anything above is unclear or doesn't work. On Tue, May 6, 2008 at 2:35 PM, Thompson, David (MNR) wrote: Hello, I have a set of one-liners (many thanks to previous responses from this list) that I use to look at newly imported data sets with functions like dim(), names(), str(), etc. within lapply(). Generally, these commands work for me but, I am apparently still missing some aspect of list manipulation. I don't understand why I get a set of NULL list elements at the end of each output as demonstrated below. How can I generate this (and similar) result(s) without all the trailing NULLs? lapply(ls(pattern='bn'), function(x) cat(x, dim(get(x)), \t, names(get(x)), \n)) bn1993 2885 11 oplt rplt rsiz tree bd ht oaz odst raz rdst spr bn1994 3158 7oplt tree bd ht spr stat dam bn1995 734 7 oplt tree bd ht spr stat dam bn1996 293 7 oplt tree bd ht spr stat dam bn1997 264 7 oplt tree bd ht spr stat dam bn1998 768 7 oplt tree bd ht spr stat dam bn1999 654 7 oplt tree bd ht dbh stat dam bn2003 1407 9oplt tree bd94 ht94 ht99 ht02 ht03 stat dam [[1]] NULL [[2]] NULL [[3]] NULL [[4]] NULL [[5]] NULL [[6]] NULL [[7]] NULL [[8]] NULL Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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. __ R-help@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.
[R] Recovering SPlus GraphSheets
Hello, Is it possible with R to recover the contents of SPlus GraphSheets (*.sgr) without access to SPlus? Also, some of the .sgr files may have multiple pages. Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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.
[R] Changing default plot behaviour
Hello, How would I make the default behaviour of my plots produce output such as the following (i.e. tick marks inside on all axes, labels only on two (arbitrary?) sides) without needing the five additional commands each time? plot(1:10, axes=FALSE) axis(1, tcl=0.5) axis(2, tcl=0.5) axis(3, tcl=0.5, labels=FALSE) axis(4, tcl=0.5, labels=FALSE) box() Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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.
Re: [R] Changing default plot behaviour
Thank you all for your help. I successfully used Richard's suggestion (much like John's) and continued with my work, neglecting to respond to the list. Sorry about that. Thanx again, DaveT. -Original Message- From: John Kane [mailto:[EMAIL PROTECTED] Sent: April 9, 2008 03:16 PM To: Thompson, David (MNR); r-help@r-project.org Subject: Re: [R] Changing default plot behaviour This may be a bit simple minded but why not change those commmands into a single function something like this and run it rather than the actual plot command? myfunction - function(a) { plot(a, axes=FALSE) axis(1, tcl=0.5) axis(2, tcl=0.5) axis(3, tcl=0.5, labels=FALSE) axis(4, tcl=0.5, labels=FALSE) box() } --- Thompson, David (MNR) [EMAIL PROTECTED] wrote: Hello, How would I make the default behaviour of my plots produce output such as the following (i.e. tick marks inside on all axes, labels only on two (arbitrary?) sides) without needing the five additional commands each time? plot(1:10, axes=FALSE) axis(1, tcl=0.5) axis(2, tcl=0.5) axis(3, tcl=0.5, labels=FALSE) axis(4, tcl=0.5, labels=FALSE) box() Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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. __ Looking for the perfect gift? Give the gift of Flickr! http://www.flickr.com/gift/ __ R-help@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.
Re: [R] Finding Interaction and main effects contrasts for two-way ANOVA
Dale, Other than the first SAS contrast, does the following demonstrate what your asking for? summary(twoway) material temp voltage 1:12 50:12 Min. : 20 2:12 65:12 1st Qu.: 70 3:12 80:12 Median :108 Mean :106 3rd Qu.:142 Max. :188 contrasts(twoway$material) 2 3 1 0 0 2 1 0 3 0 1 contrasts(twoway$temp) 65 80 50 0 0 65 1 0 80 0 1 fit - aov(voltage ~ material*temp, data=twoway) summary.aov(fit) Df Sum Sq Mean Sq F value Pr(F) material 2 1068453427.91 0.0020 ** temp 2 39119 19559 28.97 1.9e-07 *** material:temp 4 961424033.56 0.0186 * Residuals 27 18231 675 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 # setting (partial) contrasts contrasts(twoway$material) - c(1,-1,0) # ignoring the second available df contrasts(twoway$temp) - c(0,1,-1) # ignoring the second available df contrasts(twoway$material) [,1] [,2] 11 -0.41 2 -1 -0.41 30 0.82 contrasts(twoway$temp) [,1] [,2] 500 -0.82 651 0.41 80 -1 0.41 summary.aov(fit, split=list(material=list('m1-m2'=1), temp=list('t50 - t80'=1))) Df Sum Sq Mean Sq F value Pr(F) material 2 1068453427.91 0.00198 ** material: m1-m2 1 380038005.63 0.02506 * temp 2 39119 19559 28.97 1.9e-07 *** temp: t50 - t80 1 11310 11310 16.75 0.00035 *** material:temp 4 961424033.56 0.01861 * material:temp: m1-m2.t50 - t80 1 497049707.36 0.01146 * Residuals27 18231 675 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 # other examples of setting contrasts # compare m1 vs m2 and m2 vs m3 contrasts(twoway$material) - matrix(c(1,-1,0,1,1,-2), nrow=3) contrasts(twoway$material) [,1] [,2] 110 2 -11 30 -1 # compare m1 vs m2 and m1+m2 vs m3 contrasts(twoway$material) - matrix(c(1,-1,0,1,1,-2), nrow=3) contrasts(twoway$material) [,1] [,2] 111 2 -11 30 -2 I'm not sure if 'summary.aov' is the only lm-family summary method with the split argument. DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca * -Original Message- From: Steele [mailto:[EMAIL PROTECTED] Sent: March 6, 2008 09:08 PM To: [EMAIL PROTECTED] Subject: [R] Finding Interaction and main effects contrasts for two-way ANOVA I've tried without success to calculate interaction and main effects contrasts using R. I've found the functions C(), contrasts(), se.contrasts() and fit.contrasts() in package gmodels. Given the url for a small dataset and the two-way anova model below, I'd like to reproduce the results from appended SAS code. Thanks. --Dale. ## the dataset (from Montgomery) twoway - read.table(http://dsteele.veryspeedy.net/sta501/twoway.txt;, col.names=c('material', 'temp','voltage'),colClasses=c('factor', 'factor', 'numeric')) ## the model fit - aov(voltage ~ material*temp, data=twoway) /* SAS code */ proc glm data=twoway; class material temp; model voltage = material temp material*temp; contrast '21-22-31+32' material*temp 0 0 0 1 -1 0 -1 1 0; estimate '21-22-31+32' material*temp 0 0 0 1 -1 0 -1 1 0; contrast 'material1-material2' material 1 -1 0; estimate 'material1-material2' material 1 -1 0; contrast 'temp50 - temp80' temp 1 0 -1; estimate 'temp50 - temp80' temp 1 0 -1; run; __ R-help@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.
[R] Summary: Reorder data frame columns by negating list of names
Thank you all, All responses worked and focused on the use of combining the desired column names (or numbers in Barry's solution) with variations of 'match(x, table)' and 'x %in% table' to generate the remaining column names (numbers). xxx - data.frame(matrix(1:40, ncol=8)) names(xxx) - letters[1:8] # Common to all ind - c('b', 'd', 'h') xxx[, c(ind, yyy)] # where c(ind, yyy) was: # Jim Holtman c(ind, setdiff(names(xxx), ind)) [1] b d h a c e f g # Dimitris Rizopoulos c(ind, names(xxx)[!names(xxx) %in% ind]) [1] b d h a c e f g # Barry Rowlingson c(match(ind, names(xxx)), (1:dim(xxx)[2])[-match(ind, names(xxx))]) [1] 2 4 8 1 3 5 6 7 Thanks again, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca * - Original Message - From: Thompson, David (MNR) [EMAIL PROTECTED] To: r-help@r-project.org Sent: Tuesday, February 12, 2008 6:19 PM Subject: [R] Reorder data frame columns by negating list of names Hello, I would like to reorder columns in a data frame by their names as demonstrated below: Take this data frame: xxx - data.frame(matrix(1:40, ncol=8)) names(xxx) - letters[1:8] xxx a b c d e f g h 1 1 6 11 16 21 26 31 36 2 2 7 12 17 22 27 32 37 3 3 8 13 18 23 28 33 38 4 4 9 14 19 24 29 34 39 5 5 10 15 20 25 30 35 40 and reorder the columns like this: xxx[,c( c('b', 'd', 'h'), c('a', 'c', 'e', 'f', 'g') )] b d h a c e f g 1 6 16 36 1 11 21 26 31 2 7 17 37 2 12 22 27 32 3 8 18 38 3 13 23 28 33 4 9 19 39 4 14 24 29 34 5 10 20 40 5 15 25 30 35 where I only have to name the columns that I'm interested in moving to the first few positions, something like: xxx[,c( c('b', 'd', 'h'), -c('b', 'd', 'h') )] Error in -c(b, d, h) : invalid argument to unary operator Suggestions? and Thank you, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ R-help@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.
[R] Reorder data frame columns by negating list of names
Hello, I would like to reorder columns in a data frame by their names as demonstrated below: Take this data frame: xxx - data.frame(matrix(1:40, ncol=8)) names(xxx) - letters[1:8] xxx a b c d e f g h 1 1 6 11 16 21 26 31 36 2 2 7 12 17 22 27 32 37 3 3 8 13 18 23 28 33 38 4 4 9 14 19 24 29 34 39 5 5 10 15 20 25 30 35 40 and reorder the columns like this: xxx[,c( c('b', 'd', 'h'), c('a', 'c', 'e', 'f', 'g') )] b d h a c e f g 1 6 16 36 1 11 21 26 31 2 7 17 37 2 12 22 27 32 3 8 18 38 3 13 23 28 33 4 9 19 39 4 14 24 29 34 5 10 20 40 5 15 25 30 35 where I only have to name the columns that I'm interested in moving to the first few positions, something like: xxx[,c( c('b', 'd', 'h'), -c('b', 'd', 'h') )] Error in -c(b, d, h) : invalid argument to unary operator Suggestions? and Thank you, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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.
[R] Can I index a dataframe with a reference from/to a second dataframe?
Hello, I am unable to figure out how to code a new column in a data frame based on an existing column that matches a column in a reference data frame, in a relational-db fashion. I would like this to maintain a minimum set of reference tables that may be reused over several similar datasets. Specifically, I have two data frames as listed below, 'Bos' and 'tree.' For each case in 'Bos' I want to look up the matching 'spp' code in 'tree' and insert the associated 'type' code into a new 'type' column in 'Bos' as in: # add type and keep factors from reference list Bos$type - tree[as.character(tree$spp)==as.character(Bos$spp), 'type'] Bos$keep - tree[tree$spp==Bos$spp, 'keep'] And I know I have seen this before but, can't remember where. I have filtered through many of the threads referencing 'as.numeric(levels(Bos$spp))[as.integer(Bos$spp)]' handling of factors, any kind of sql reference I could think of, anything 'ODBC'-ish but, I think this may be an indexing issue. I am trying to compare elements of two different sized (list) objects (different type objects even?) and not cycling through Bos$spp to find matches in tree$spp as expected, . . . , has this an apply solution? My data frames: dput(head(Bos, 30)) structure(list(oplt = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), rplt = c(3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), tree = c(32, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129), spp = structure(c(10L, 10L, 12L, 14L, 10L, 10L, 14L, 10L, 15L, 10L, 9L, 3L, 10L, 10L, 12L, 10L, 13L, 12L, 12L, 10L, 12L, 10L, 10L, 8L, 5L, 2L, 10L, 2L, 12L, 10L), .Label = c(AW, BD, BE, BF, BW, BY, CB, HE, IW, MH, MR, OR, PO, SW, SA), class = factor), dbh = c(12.1, 10.1, 63.3, 9, 7.1, 12.1, 13.9, 6.3, 6.1, 7.9, 5.1, 9.8, 7.1, 18.7, 44.2, 28.7, 19.8, 28, 46.6, 9, 61.6, 3.3, 9.1, 8.7, 5.8, 3.1, 11.1, 12.3, 28, 8.6), cc = structure(c(2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L), .Label = c(dom, sup), class = factor), ba = c(114.990145103020, 80.1184666481737, 3147.0040469356, 63.6172512351933, 39.5919214168654, 114.990145103020, 151.746779150021, 31.1724531052447, 29.2246656600190, 49.0166993776348, 20.4282062299676, 75.429639612691, 39.5919214168654, 274.645883758454, 1534.38526793979, 646.924613208844, 307.907495978336, 615.7521601036, 1705.53923570736, 63.6172512351933, 2980.24045490142, 8.55298599939821, 65.0388219109427, 59.4467869875528, 26.4207942166902, 7.54767635024948, 96.7689077121996, 118.8228881404, 615.7521601036, 58.0880481648753 )), .Names = c(oplt, rplt, tree, spp, dbh, cc, ba), row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 ), class = data.frame) dput(tree) structure(list(spp = structure(1:33, .Label = c(AB, AS, AW, BD, BE, BF, BW, BY, CA, CB, CC, CE, DL, DP, EA, HE, IW, LC, MH, MM, MR, OR, PO, PR, PV, PW, RS, SA, SB, SM, SW, VC, VL), class = factor), spp.orig = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 12L, 10L, 13L, 11L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 31L, 29L, 30L, 32L, 33L, 34L ), .Label = c(AB, AMEHUM, AMESPP, AW, BD, BE, BF, BW, BY, CB, CE, CORALT, CORCOR, DIELON, DIRPAL, EA, HE, IW, LONCAN, MH, MM, MR, OR, PO, PR, PRUVIR, PW, RIBSPP, SB, SM, SORAME, SW, VIBACE, VIBALN), class = factor), OPL = structure(c(15L, 7L, 14L, 29L, 13L, 2L, 9L, 8L, 10L, 23L, 1L, 28L, 11L, 12L, 31L, 30L, 17L, 16L, 5L, 6L, 4L, 25L, 22L, 20L, 24L, 21L, 26L, 27L, 19L, 3L, 18L, 32L, 33L), .Label = c(HCORCAN, WABIBAL, WACEPEN, WACERUB, WACESAS, WACESPI, WAMESPP, WBETALL, WBETPAP, WCORALT, WDIELON, WDIRPAL, WFAGGRA, WFRAAME, WFRANIG, WLONCAN, WOSTVIR, WPICGLA, WPICMAR, WPINRES, WPINSTR, WPOPTRE, WPRUSER, WPRUVIV, WQUERUB, WRIBAME, WSORAME, WTHUOCC, WTILAME, WTSUCAN, WULMAME, WVIBACE, WVIBLAO), class = factor), form = c(1.1, 1.2, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.2, 1.1, 2, 1.1, 1.2, 1.2, 1.1, 1.1, 1.1, 1.2, 1.1, 1.2, 1.1, 1.1, 1.1, 1.1, 1.2, 1.1, 1.2, 1.1, 1.1, 1.2, 1.1, 1.2, 1.2), Type = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c(H, W), class = factor), keep = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L), .Label = c(no, yes), class = factor), Sname = structure(c(15L, 6L, 14L, 29L, 13L, 1L, 8L, 7L, 9L, 23L, 10L, 28L, 11L, 12L, 31L, 30L, 17L, 16L, 4L, 5L, 3L, 25L, 22L, 20L, 24L, 21L, 26L, 27L, 19L, 2L,
Re: [R] Converting plots to ggplot2
Hello Hadley, Thank you (and Domenico) for your help. I'm almost there. For convenience, I'll restate the question. With the following data: a - seq(0, 360, 5)*pi/180 ; a ac - sin(a + (45*pi/180)) + 1 ; ac s - seq(5, 45, 10)*pi/180 ; s asc - lapply(s, function(x) x*cos(ac) + x*sin(ac)) ; asc I can generate my desired plot by traditional methods with: plot(a, ac, type='l', xaxt = n, xlab='azimuth', ylab='index') axis(1, at=seq(0,6,1), labels=round(seq(0,6,1)*180/pi),0) abline(v=c(45*pi/180, 225*pi/180)) ; par(new=TRUE) lapply(asc, function(x) {plot(a, x, type='l', xaxt = n, yaxt = n, xlab='', ylab='', ylim=c(0, 2)) ; par(new=TRUE)}) With ggplot2 and your suggestions I can generate the same with: p - qplot(a, ac, geom='line') + geom_line(aes(x=a, y=asc[[1]])) + geom_line(aes(x=a, y=asc[[2]])) + geom_line(aes(x=a, y=asc[[3]])) + geom_line(aes(x=a, y=asc[[4]])) + geom_line(aes(x=a, y=asc[[5]])) + scale_x_continuous(name=azimuth) + scale_y_continuous(name=index)) But these attempts to recreate the x-axis labels both generate the following error: p + scale_x_continuous(labels=round(seq(0,6,1)*180/pi,0)) p + scale_x_continuous(breaks=seq(0,6,1), labels=round(seq(0,6,1)*180/pi,0)) Error in get(new, env = ScaleContinuous, inherits = TRUE)(ScaleContinuous, : unused argument(s) (labels = c(0, 57, 115, 172, 229, 286, 344)) And these attempts to draw reference lines both generate the following error: p + geom_vline(45*pi/180) + geom_vline(225*pi/180) p + geom_vline(0.79) + geom_vline(3.9) Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 73, 1 What am I missing? Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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.
[R] Converting plots to ggplot2
Hello Hadley, I am trying to reproduce the following with ggplot: a - seq(0, 360, 5)*pi/180 ; a ac - sin(a + (45*pi/180)) + 1 ; ac plot(a, ac, type='b', xaxt = n) axis(1, at=seq(0,6,1), labels=round(seq(0,6,1)*180/pi),1) abline(v=c(45*pi/180, 225*pi/180)) I can get the basic plot: p - qplot(a, ac, geom=c('point', 'line')) ; p but cannot seem to add the vertical reference lines: # representing NE and SW compass points p + geom_vline(intercept=45*pi/180) p + geom_vline(intercept=225*pi/180) nor find a reference to manipulating the axes labels (still searching the news archives though). Also, I would like to add additional curves to the same plot with the sequence 'asc' generated by: s - seq(5, 45, 10)*pi/180 ; s asc - lapply(s, function(x) x*cos(ac) + x*sin(ac)) ; asc Suggestions? Thanx, DaveT. sessionInfo() R version 2.6.1 (2007-11-26) 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] datasets tcltk utils stats graphics grDevices splines grid [9] methods base other attached packages: [1] svGUI_0.9-5svViews_0.9-5 svIO_0.9-5 svMisc_0.9-5 [5] R2HTML_1.58ggplot2_0.5.2 RColorBrewer_0.2-3 MASS_7.2-34 [9] proto_0.3-7reshape_0.7.4 loaded via a namespace (and not attached): [1] lattice_0.14-17 Sys.info()[c(1:3,5)] sysname release Windows NT 5.1 version machine (build 2600) Service Pack 2 x86 * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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.
[R] Solution: Available environment variables
Thank you Duncan (additional comments below), -Original Message- From: Duncan Murdoch [mailto:[EMAIL PROTECTED] Sent: December 20, 2007 02:36 PM To: Thompson, David (MNR) Cc: r-help@r-project.org Subject: Re: [R] Available environment variables On 20/12/2007 2:13 PM, Thompson, David (MNR) wrote: Hello, I am trying to set my environment to streamline the downloading / updating of packages. I have been through R-intro.html (10.8 Customizing-the-environment), R-FAQ (5.2 How can add-on packages be installed?), rw-FAQ, and help pages for Sys.setenv {base}, download.packages {utils}, etc.,. I am looking for something similar to the download.packages destdir argument on a global environment level, e.g., destdir - 'C:/Program Files/_dave/stats/R/library/_download' in Rprofile.site. Any suggestions? You could create your own function: download.packages - function(pkgs, destdir='C:/Program Files/_dave/stats/R/library/_download', ...) utils::download.packages(pkgs, destdir, ...) Also, in a more general context, is there a way of listing all available (or common, or default, or standard, . . .) environment variables? Sys.getenv() will return a list of the ones that are currently defined. I would like to know if there is an existing convention for any arbitrary process that I may try to do without reinventing the wheel. This same question could apply to options() other than those included with a standard installation. I don't know what you mean about an arbitrary process. For options(), you could save the names that you consider standard (e.g. by starting an empty session and entering standardoptions - names(options()), then at a later time do newoptions - names(options()) newoptions[ !(newoptions %in% standardoptions) ] There's no easy way to know the options that would mean something in the current context but are not currently defined. Duncan Murdoch Creating a local copy of the function with alternate argument defaults works perfectly. I have done that in the past, don't know why I didn't think of it this time. Thank you. Saving initial- and post-session options (or their names) also nicely addresses part of my concern. Thank you. I am familiar with Sys.getenv() and was asking about potential environment variables that are NOT currently defined. However, while trying to rephrase my question, having reread the Startup {base} help and related documentation, could not clearly refine the issue in my own mind so, I am now content with the situation. I think that I may be confusing the intended use of environment files and profile files as noted in the details section of the Startup {base} documentation. Part of my concern is to find a way of safeguarding against my own errors and typos that may 'break the system' or having something installed later (the arbitrary process part) breaking my current setup. Also, I may be thinking of the startup process as something 'different' from the interactive R session processing, which I am beginning to see it is not. Thanks again, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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.
Re: [R] str() options
Beautiful! Exactly what I was looking for. Thank you, Gabor -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: November 27, 2007 05:02 PM To: Thompson, David (MNR) Subject: Re: [R] str() options On Nov 27, 2007 1:55 PM, Thompson, David (MNR) [EMAIL PROTECTED] wrote: Hello, Is there a way to have str() and ls.str() return all factor levels? str is an S3 generic so you can define your own str.factor. Here we show the usual str and then show it again after we have added our own str.factor: str(iris) 'data.frame': 150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... $ Species : Factor w/ 3 levels setosa,versicolor,..: 1 1 1 1 1 1 1 1 1 1 ... str.factor - function(object, ...) { + cat(factor w/, nlevels(object), levels, levels(object), \n) + } str(iris) 'data.frame': 150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... $ Species :factor w/ 3 levels setosa versicolor virginica * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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.
[R] Summary: Process multiple columns of data.frame
Thank you Jim Holtman and Mark Leeds for your help. Original question: How do I do the following more concisely? Bout[is.na(Bout$bd.n), 'bd.n'] - 0 Bout[is.na(Bout$ht.n), 'ht.n'] - 0 Bout[is.na(Bout$dbh.n), 'dbh.n'] - 0 . . . Solution: for (i in c('bd.n', 'ht.n', 'dbh.n')) Bout[is.na(Bout[[i]]), i] - 0 Toy example: df.0 - as.data.frame( cbind( + c1=c(NA, NA, 10, NA, 15, 11, 12, 14, 14, 11), + c2=c(13, NA, 16, 16, NA, 12, 14, 19, 18, NA), + c3=c(NA, NA, 11, 19, 17, NA, 11, 16, 20, 13), + c4=c(20, NA, 15, 11, NA, 15, NA, 13, 14, 15), + c5=c(14, NA, 13, 16, 17, 17, 16, NA, 15, NA), + c6=c(NA, NA, 13, 11, NA, 16, 15, 12, NA, 20)) ) df.1 - df.0 for (i in c('c2', 'c3', 'c4')) df.1[is.na(df.1[[i]]), i] - 0 df.0 c1 c2 c3 c4 c5 c6 1 NA 13 NA 20 14 NA 2 NA NA NA NA NA NA 3 10 16 11 15 13 13 4 NA 16 19 11 16 11 5 15 NA 17 NA 17 NA 6 11 12 NA 15 17 16 7 12 14 11 NA 16 15 8 14 19 16 13 NA 12 9 14 18 20 14 15 NA 10 11 NA 13 15 NA 20 df.1 c1 c2 c3 c4 c5 c6 1 NA 13 0 20 14 NA 2 NA 0 0 0 NA NA 3 10 16 11 15 13 13 4 NA 16 19 11 16 11 5 15 0 17 0 17 NA 6 11 12 0 15 17 16 7 12 14 11 0 16 15 8 14 19 16 13 NA 12 9 14 18 20 14 15 NA 10 11 0 13 15 NA 20 Thank you, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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.
Re: [R] Process multiple columns of data.frame
I did specify only the three columns of interest c('bd.n','ht.n','dbh.n') as the second (column) index, and I did superficially check the results which turned out as I had hoped. Only the three columns listed were changed and existing values _in_ those columns were retained. So, I appear to be on my way again. Thanx again, DaveT. * -Original Message- From: jim holtman [mailto:[EMAIL PROTECTED] Sent: November 20, 2007 04:21 PM To: Thompson, David (MNR) Cc: r-help@r-project.org Subject: Re: [R] Process multiple columns of data.frame The problem with that solution is that if any of the columns in a row has an NA in it, then all the columns of that row will be set to zero. Is the what you want, or do you only want the specific entries set to 0? On Nov 20, 2007 4:07 PM, Thompson, David (MNR) [EMAIL PROTECTED] wrote: Jim, What I'm getting at is my lack of understanding when manipulating data frame indices. I have tried something like your matrix example with variants of: Bout[ is.na(Bout[,c('bd.n','ht.n','dbh.n')]), ] - 0 only to receive the following error message: Error in `[-.data.frame`(`*tmp*`, is.na(Bout[, c(bd.n, ht.n, dbh.n)]), : non-existent rows not allowed My questions were not focussed on the assignment value but, on the selection of multiple columns (of a single type) to process. Which is what I meant by concisely. And, lo and behold, in my attempts to reframe my questions I (finally) came upon a solution: Bout[is.na(Bout$bd.n) | is.na(Bout$ht.n) | is.na(Bout$dbh.n), c('bd.n','ht.n','dbh.n')] - 0 Thank you for your time, DaveT. * -Original Message- From: jim holtman [mailto:[EMAIL PROTECTED] Sent: November 20, 2007 02:25 PM To: Thompson, David (MNR) Cc: r-help@r-project.org Subject: Re: [R] Process multiple columns of data.frame It really depends on what you want to set the values that contain NAs to for the various type of the columns. Do you always want numerics =0, characters =, and factors =whatever? Do you want to do this for all the columns in a dataframe? If you want to it for all the columns in a matrix, it is easy, since all are the same type, and you are setting them to zero (e.g., yourMatrix[is.na(yourMatrix)] - 0). You can always write a function that can take in the names of the columns and then depending on their types set the corresponding values. So it depends on what you mean by concisely and how often you want to do it. On Nov 20, 2007 12:27 PM, Thompson, David (MNR) [EMAIL PROTECTED] wrote: Hello, How do I do the following more concisely? Bout[is.na(Bout$bd.n), 'bd.n'] - 0 Bout[is.na(Bout$ht.n), 'ht.n'] - 0 Bout[is.na(Bout$dbh.n), 'dbh.n'] - 0 Would the form of such a command be different between numeric, character and factor columns? . . . between data.frames and matrices? Thanx, DaveT. * Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources [EMAIL PROTECTED] http://ofri.mnr.gov.on.ca __ R-help@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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@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.