[R] backslash c
How do I get output with \color{blue}, i.e. with only one backslash??? \color{blue} [1] color{blue} Warning messages: 1: '\c' is an unrecognized escape in a character string 2: unrecognized escape removed from \color{blue} \\color{blue} [1] \\color{blue} Any help greatly appreciated. Best regards, Mikkel sessionInfo() R version 2.5.1 (2007-06-27) i386-pc-mingw32 locale: LC_COLLATE=English_Ireland.1252;LC_CTYPE=English_Ireland.1252;LC_MONETARY=English_Ireland.1252;LC_NUMERIC=C;LC_TIME=English_Ireland.1252 attached base packages: [1] stats graphics grDevices utils datasets methods [7] base __ R-help@stat.math.ethz.ch 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] arrowhead styles
One way would be to use the grid function arrow(). Paul Murrell's document on Integrating Grid Graphics Output with Base Graphics Output has an example of integrating grid graphics arrows in base graphics. There's also an Arrows() function in the package IDPMisc. Mikkel Hi all, I've been using the arrows() function in plots a lot, but I'm not happy with the appearance of the arrow heads. I understand that arrows () doesn't offer more sophisticated arrowhead shapes like e.g. a filled triangle, possibly with choice of angle at the point. Does anyone know an easy way to achieve this? thanks Hendrik Finding fabulous fares is fun. __ R-help@stat.math.ethz.ch 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] problem with cut(as.Date(2006-08-14), week)
When I run cut.Date or cut.POSIXt with argument breaks = weeks, the function gives the first day of that week, unless the date is the first day of the week, in which case it gives an error message as in: cut(as.Date(2006-08-16), week) [1] 2006-08-14 Levels: 2006-08-14 cut(as.Date(2006-08-14), week) Error in 1:(1 + max(which(breaks maxx))) : result would be too long a vector In addition: Warning message: no non-missing arguments to max; returning -Inf sessionInfo() Version 2.3.1 (2006-06-01) i386-pc-mingw32 attached base packages: [1] methods stats graphics grDevices utils datasets [7] base Sys.getlocale() [1] 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 Bug or feature? Mikkel __ R-help@stat.math.ethz.ch 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] \r with RSQLite
What am I doing wrong, or is the \r that I'm getting in the example below a bug? a - (1:10) b - (LETTERS[1:10]) df - as.data.frame(cbind(a, b)) df a b 1 1 A 2 2 B 3 3 C 4 4 D 5 5 E 6 6 F 7 7 G 8 8 H 9 9 I 10 10 J library(RSQLite) drv - dbDriver(SQLite) con - dbConnect(drv, dbname = Test) dbWriteTable(con, DF, df, row.names = FALSE, overwrite = TRUE) [1] TRUE df2 - dbGetQuery(con, SELECT DISTINCT * FROM DF) dbDisconnect(con) [1] TRUE df2 a b 1 1 A\r 2 2 B\r 3 3 C\r 4 4 D\r 5 5 E\r 6 6 F\r 7 7 G\r 8 8 H\r 9 9 I\r 10 10 J\r sessionInfo() R version 2.2.1, 2005-12-20, i386-pc-mingw32 attached base packages: [1] methods stats graphics grDevices utils datasets [7] base other attached packages: RSQLite DBI 0.4-1 0.1-10 Mikkel Grum Genetic Diversity International Plant Genetic Resources Institute __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] \r with RSQLite
Thanks for the tip. I've attempted the alternative approach of using write.table, which allows you to see the file and import it directly. Would the carriage returns be visible with notepad or with the edit command? Because I don't see the /r in the text file, but still get it when I query the database: library(RSQLite) Loading required package: DBI a - (5:10) b - (LETTERS[5:10]) df - as.data.frame(cbind(a, b)) library(RSQLite) drv - dbDriver(SQLite) con - dbConnect(drv, dbname = Test) write.table(df, file = df.txt, quote = FALSE, row.names = FALSE) dbWriteTable(con, DF, df.txt, header = TRUE, row.names = FALSE, overwrite = TRUE) [1] TRUE df2 - dbGetQuery(con, SELECT DISTINCT * FROM DF) dbDisconnect(con) [1] TRUE df2 a_b 1 5 E\r 2 6 F\r 3 7 G\r 4 8 H\r 5 9 I\r 6 10 J\r Also, this time it doesn't recognise that I didn't want the rownames. I've attempted to go throught the source code, but can't trace the problem with the carriage return. Mikkel --- bogdan romocea [EMAIL PROTECTED] wrote: \r is a carriage return character which some editors may use as a line terminator when writing files. My guess is that RSQLite writes your data frame to a temp file using \r as a line terminator and then runs a script to have SQLite import the data (together with \r - this would be the problem), but I have no idea if that's really the case. Check the documentation or ask the maintainer. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mikkel Grum Sent: Wednesday, March 15, 2006 1:46 PM To: r-help@stat.math.ethz.ch Cc: [EMAIL PROTECTED] Subject: [R] \r with RSQLite What am I doing wrong, or is the \r that I'm getting in the example below a bug? a - (1:10) b - (LETTERS[1:10]) df - as.data.frame(cbind(a, b)) df a b 1 1 A 2 2 B 3 3 C 4 4 D 5 5 E 6 6 F 7 7 G 8 8 H 9 9 I 10 10 J library(RSQLite) drv - dbDriver(SQLite) con - dbConnect(drv, dbname = Test) dbWriteTable(con, DF, df, row.names = FALSE, overwrite = TRUE) [1] TRUE df2 - dbGetQuery(con, SELECT DISTINCT * FROM DF) dbDisconnect(con) [1] TRUE df2 a b 1 1 A\r 2 2 B\r 3 3 C\r 4 4 D\r 5 5 E\r 6 6 F\r 7 7 G\r 8 8 H\r 9 9 I\r 10 10 J\r sessionInfo() R version 2.2.1, 2005-12-20, i386-pc-mingw32 attached base packages: [1] methods stats graphics grDevices utils datasets [7] base other attached packages: RSQLite DBI 0.4-1 0.1-10 Mikkel Grum Genetic Diversity International Plant Genetic Resources Institute __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] substituting an object not found
Is there any function in R like is.not.found(x, y) meaning if you can't find object x, then use object y?? Mikkel Grum __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] 7:9, 12:14 in dataframe to c(7:9, 12:14)
I want to do something like df[df$b %in% df2[i, 2], ] where df$b is a numeric vector and df2[i, 2] is a factor with levels like 7:9, 12:14. For example: a - c(paste(A, 1:10, sep = ), paste(B, 1:10, sep = )) b - 1:20 df - as.data.frame(cbind(a, b)) df$b - as.numeric(levels(df$b))[as.integer(df$b)] f - c(X, Y, Z) g - c(1:6, 7:9, 12:14, 18) df2 - as.data.frame(cbind(f, g)) i - 2 df[df$b %in% df2[i, 2], ] # or df[df$b %in% levels(df2)[as.integer(df2[i, 2])], ] # this is the closest I've got The results I want is given by df[df$b %in% c(7:9, 12:14), ] a b 7 A7 7 8 A8 8 9 A9 9 12 B2 12 13 B3 13 14 B4 14 Can it be done? Mikkel Grum __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] readline() and Rterm in Windows
I've tried your proposal in a number of ways, and there must be something I'm not understanding. If I run your script (using source() in RGui, or ctrl-R from the R Editor, I get: conout - file('CONOUT$','w') Error in file(CONOUT$, w) : unable to open connection In addition: Warning message: cannot open file 'CONOUT$', reason 'Permission denied' so I added the path as in: conout - file('C:\\R\\R-2.2.0\\CONOUT$','w') conin - file('C:\\R\\R-2.2.0\\CONIN$', 'r') cat('Please enter an ID:', file=conout) flush(conout) id - readLines(conin, 1) print(id) Using RGui and ctrl-R from the R Editor, I get conout - file('C:\\R\\R-2.2.0\\CONOUT$','w') conin - file('C:\\R\\R-2.2.0\\CONIN$', 'r') Error in file(C:\\R\\R-2.2.0\\CONIN$, r) : unable to open connection In addition: Warning message: cannot open file 'C:\R\R-2.2.0\CONIN$', reason 'No such file or directory' cat('Please enter an ID:', file=conout) flush(conout) id - readLines(conin, 1) Error in readLines(conin, 1) : object conin not found and with source(foo.R) Error in file(C:\\R\\R-2.2.0\\CONIN$, r) : unable to open connection In addition: Warning message: cannot open file 'C:\R\R-2.2.0\CONIN$', reason 'No such file or directory' When I create a batch file with the following command : C:\R\R-2.2.0\bin\Rterm.exe --vanilla C:\R\R-2.2.0\foo.R C:\R\R-2.2.0\foo.out and double click on the batch file, the out file gives me: R : Copyright 2005, The R Foundation for Statistical Computing Version 2.2.0 (2005-10-06 r35749) ISBN 3-900051-07-0 . . . Type 'q()' to quit R. conout - file('C:\\R\\R-2.2.0\\CONOUT$','w') conin - file('C:\\R\\R-2.2.0\\CONIN$', 'r') and nothing else. In none of the situations do I get prompted for input. What am I doing hopelessly wrong? Mikkel --- Duncan Murdoch [EMAIL PROTECTED] wrote: Mikkel Grum wrote: Duncan and Prof, thanks for your comments and apologies for not being more specific. I'm not getting the same results you get from the steps you propose. If I write a script foo.R with two lines id - readline(Please enter an ID: ) id and then use source(foo.R) (either at the Rterm prompt, or in RGui) it is true that get prompted, but the second line does not visibly run, i.e. I get source(id.r) Please enter an ID: 5 and if I then type id, I get id [1] id If I cut and paste the two lines in RGui (in one go), I get id - readline(Please enter an ID: ) Please enter an ID: id What I really want is a batch file on the desktop with the following commands: c:\r\R-2.2.0\bin\Rterm.exe --no-save --no-restore script.R script.out 21 c:\texmf\miktex\bin\latex \nonstopmode\input{blue.tex} and script.R reads something like: id - readline(Please enter an ID: ) id Sweave(blue.Rnw) I said that script.R didn't run, which was an incorrect description. It runs without prompting for the ID, and gives error messages all through because blue.Rnw needs the id. This is a very simplified version of what I'm doing, but if I use only the first line of the batch file and the first two lines of the script and could get that to work, I could figure out the rest. It won't work so simply. You're redirecting stdin, so user input would be taken from there; you're redirecting stdout and stderr, so the prompt won't be visible to the user. You need to open new handles to the console. The code below will do it in Windows; the syntax to specify the console in Unix-alikes will be different (but I don't know what it is). conout - file('CONOUT$','w') conin - file('CONIN$', 'r') cat('Please enter an ID:', file=conout) flush(conout) id - readLines(conin, 1) print(id) Duncan Murdoch __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] readline() and Rterm in Windows
And that was the only combination I didn't try, duhh. As you say, it works. Excellent! TX! They'll be a number of pleased usres too. --- Duncan Murdoch [EMAIL PROTECTED] wrote: Mikkel Grum wrote: I've tried your proposal in a number of ways, and there must be something I'm not understanding. If I run your script (using source() in RGui, or ctrl-R from the R Editor, I get: It requires a command line console, i.e. it will only work in Rterm, not Rgui. I was assuming you'd run it using the style of your batch file down below, but without changing the paths. Duncan Murdoch conout - file('CONOUT$','w') Error in file(CONOUT$, w) : unable to open connection In addition: Warning message: cannot open file 'CONOUT$', reason 'Permission denied' so I added the path as in: conout - file('C:\\R\\R-2.2.0\\CONOUT$','w') conin - file('C:\\R\\R-2.2.0\\CONIN$', 'r') cat('Please enter an ID:', file=conout) flush(conout) id - readLines(conin, 1) print(id) Using RGui and ctrl-R from the R Editor, I get conout - file('C:\\R\\R-2.2.0\\CONOUT$','w') conin - file('C:\\R\\R-2.2.0\\CONIN$', 'r') Error in file(C:\\R\\R-2.2.0\\CONIN$, r) : unable to open connection In addition: Warning message: cannot open file 'C:\R\R-2.2.0\CONIN$', reason 'No such file or directory' cat('Please enter an ID:', file=conout) flush(conout) id - readLines(conin, 1) Error in readLines(conin, 1) : object conin not found and with source(foo.R) Error in file(C:\\R\\R-2.2.0\\CONIN$, r) : unable to open connection In addition: Warning message: cannot open file 'C:\R\R-2.2.0\CONIN$', reason 'No such file or directory' When I create a batch file with the following command : C:\R\R-2.2.0\bin\Rterm.exe --vanilla C:\R\R-2.2.0\foo.R C:\R\R-2.2.0\foo.out and double click on the batch file, the out file gives me: R : Copyright 2005, The R Foundation for Statistical Computing Version 2.2.0 (2005-10-06 r35749) ISBN 3-900051-07-0 . . . Type 'q()' to quit R. conout - file('C:\\R\\R-2.2.0\\CONOUT$','w') conin - file('C:\\R\\R-2.2.0\\CONIN$', 'r') and nothing else. In none of the situations do I get prompted for input. What am I doing hopelessly wrong? Mikkel --- Duncan Murdoch [EMAIL PROTECTED] wrote: Mikkel Grum wrote: Duncan and Prof, thanks for your comments and apologies for not being more specific. I'm not getting the same results you get from the steps you propose. If I write a script foo.R with two lines id - readline(Please enter an ID: ) id and then use source(foo.R) (either at the Rterm prompt, or in RGui) it is true that get prompted, but the second line does not visibly run, i.e. I get source(id.r) Please enter an ID: 5 and if I then type id, I get id [1] id If I cut and paste the two lines in RGui (in one go), I get id - readline(Please enter an ID: ) Please enter an ID: id What I really want is a batch file on the desktop with the following commands: c:\r\R-2.2.0\bin\Rterm.exe --no-save --no-restore script.R script.out 21 c:\texmf\miktex\bin\latex \nonstopmode\input{blue.tex} and script.R reads something like: id - readline(Please enter an ID: ) id Sweave(blue.Rnw) I said that script.R didn't run, which was an incorrect description. It runs without prompting for the ID, and gives error messages all through because blue.Rnw needs the id. This is a very simplified version of what I'm doing, but if I use only the first line of the batch file and the first two lines of the script and could get that to work, I could figure out the rest. It won't work so simply. You're redirecting stdin, so user input would be taken from there; you're redirecting stdout and stderr, so the prompt won't be visible to the user. === message truncated === __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] readline() and Rterm in Windows
I'm running an R script in Rterm and would like the user to be prompted for input as in: id - readline(Please enter ID: ) myfunction(id) . . . etc. This works when I run one line at a time in RGui, but not when I try to run the script in Rterm (I'm working with R 2.2.0 in Windows Server 2003). Is there any way to do this? Mikkel __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] readline() and Rterm in Windows
Duncan and Prof, thanks for your comments and apologies for not being more specific. I'm not getting the same results you get from the steps you propose. If I write a script foo.R with two lines id - readline(Please enter an ID: ) id and then use source(foo.R) (either at the Rterm prompt, or in RGui) it is true that get prompted, but the second line does not visibly run, i.e. I get source(id.r) Please enter an ID: 5 and if I then type id, I get id [1] id If I cut and paste the two lines in RGui (in one go), I get id - readline(Please enter an ID: ) Please enter an ID: id What I really want is a batch file on the desktop with the following commands: c:\r\R-2.2.0\bin\Rterm.exe --no-save --no-restore script.R script.out 21 c:\texmf\miktex\bin\latex \nonstopmode\input{blue.tex} and script.R reads something like: id - readline(Please enter an ID: ) id Sweave(blue.Rnw) I said that script.R didn't run, which was an incorrect description. It runs without prompting for the ID, and gives error messages all through because blue.Rnw needs the id. This is a very simplified version of what I'm doing, but if I use only the first line of the batch file and the first two lines of the script and could get that to work, I could figure out the rest. Mikkel --- Prof Brian Ripley [EMAIL PROTECTED] wrote: What does `not work' mean here? In particular, what does `running an R script in' mean? It works as I would expect: if foo.R contains id - readline(Please enter ID: ) then source(foo.R) Please enter ID: 5 id [1] 5 Please do read the posting guide and explain what you are doing it enough detail for people to reproduce it. On Wed, 2 Nov 2005, Mikkel Grum wrote: I'm running an R script in Rterm and would like the user to be prompted for input as in: id - readline(Please enter ID: ) myfunction(id) . . . etc. This works when I run one line at a time in RGui, but not when I try to run the script in Rterm (I'm working with R 2.2.0 in Windows Server 2003). Is there any way to do this? -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] xmat[1, 2:3] - NULL
Thanks for this! This is even simpler than using !is.null(): xmat - as.data.frame(matrix(NA, 2, 3)) try(xmat[i, 2:3] - dbGetQuery(...), silent = TRUE) Best wishes, Mikkel --- Uwe Ligges [EMAIL PROTECTED] wrote: Mikkel Grum wrote: I have a situation where I'm filling out a dataframe from a database. Sometimes the database query doesn't get anything, so I end up trying to place NULL in the dataframe like below. temp - NULL xmat - as.data.frame(matrix(NA, 2, 3)) xmat[1, 2:3] - temp Error in if (m n * p (n * p)%%m) stop(gettextf(replacement has %d items, need %d, : missing value where TRUE/FALSE needed I can't get the programme to accept that sometimes what the query looks for just doesn't exist, and I just want to move on to the next calculation leaving the dataframe with a missing value in the given cell. It's a real show stopper and I haven't found a way round it. See ?try Uwe Ligges Best wishes, Mikkel PS. I'm using dbGetQuery to query an SQLite database. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] about image() function in R and colors
Javier, on the image and colors, did you try using the breaks option? You will need 21 breaks to display 20 colors (see ?image). Janek, the option asp = 1 should generally solve your scaling problem (point 2 below). I don't remember where I got that from, but it works unless you use functions like abline which will take up the whole plot region. Then you can adjust the plot region using pin = c(diff(range(a*x)), diff(range(a*y))) see ?par Best wishes, Mikkel R] about image() function in R and colors Tuszynski, Jaroslaw W. JAROSLAW.W.TUSZYNSKI at saic.com Thu Jul 7 14:22:27 CEST 2005 * Previous message: [R] Tables: Invitation to make a collective package * Next message: [R] Kernlab: problem with small datasets * Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] I do not know the solution to your problem, but I had the similar problems with image() function and most of derived functions. It seems that 'image' function was really not meant for displaying image data, instead it was designed to display matrices in the image format. Matlab had the same problem and ended up creating 2 functions: 'image' (similar to R's 'image') and 'imshow' (designed for displaying image data). There are three major processing steps in the 'image' that are hard to control or reverse: 1) scaling of the data intensities ( problem explained by Javier). Scaling is not much of a problem if continuous palette of colors is used , or to quote ?image when 'col' is a list of colors such as that generated by 'rainbow', 'heat.colors', 'topo.colors', 'terrain.colors' or similar functions. However scaling causes problems in case of discontinuous color-maps (palettes). 2) scaling of the data dimensions so it fits in default size window instead of scaling of the window to fit the data. This results in image with non-square pixels. There might be a way to force 'image' not to scale dimensions, I just did not spend much time looking for it yet. 3) Flipping of the image. As ?image shows one needs to transpose and flip matrix horizontally or perform image(t(volcano)[ncol(volcano):1,]) for the image data to be visualized in proper orientation. All those steps make sense in case of visualizing 2D data, but they are a hindrance in case of visualizing images. Jarek \=== Jarek Tuszynski, PhD. o / \ Science Applications International Corporation \__,| (703) 676-4192 \ Jaroslaw.W.Tuszynski at saic.com `\ -Original Message- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of javier garcia Sent: Thursday, July 07, 2005 7:23 AM To: R-Help Cc: statsgrass at grass.itc.it Subject: [R] about image() function in R and colors Hi! I've got a map in R imported from a GIS (GRASS) as a vector of factors. So I've got 20 different levels in the map and I've created a vector of custom colors of exactly 20 colors in lenght. I'm trying to use image() (really plot.grassmeta() that call image()) to plot the map with those colors but it doesnt work and the colors are changed. I would like that all points belonging to level1 are color 1 , and so on... Please could you tell me if this procedure is not correct? Best regards, Javier -- A. Javier Garcia Water and Soil conservation department CEBAS-CSIC Campus Universitario Espinardo PO BOX 164 30100 Murcia (SPAIN) Phone: +34 968 39 62 57 Fax: +34 968 39 62 13 email: rn001 at cebas.csic.es __ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html * Previous message: [R] Tables: Invitation to make a collective package * Next message: [R] Kernlab: problem with small datasets * Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] __ Stay connected, organized, and protected. Take the tour: __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] removing factor level represented by less than x rows
In a number of different situations I'm trying to remove factor levels that are represented by less than a certain number of rows, e.g. if I had the dataset aa below and wanted to remove the species that are represented in less than 2 rows: data(iris) aa - iris[1:101,] In this case, since I can see that the species virginica only has one row, I can write: table(aa$Species) setosa versicolor virginica 50 50 1 aa[aa$Species != virginica, ] but: aa[aa$Species == names(table(aa$Species) 2),] does not work. This must be a fairly common task with a straight forward solution that I can't see. Any ideas? Best wishes, Mikkel __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] xmat[1, 2:3] - NULL
I have a situation where I'm filling out a dataframe from a database. Sometimes the database query doesn't get anything, so I end up trying to place NULL in the dataframe like below. temp - NULL xmat - as.data.frame(matrix(NA, 2, 3)) xmat[1, 2:3] - temp Error in if (m n * p (n * p)%%m) stop(gettextf(replacement has %d items, need %d, : missing value where TRUE/FALSE needed I can't get the programme to accept that sometimes what the query looks for just doesn't exist, and I just want to move on to the next calculation leaving the dataframe with a missing value in the given cell. It's a real show stopper and I haven't found a way round it. Best wishes, Mikkel PS. I'm using dbGetQuery to query an SQLite database. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] xmat[1, 2:3] - NULL
Thanks a lot!! This was really a big help. The following solves my problem: xmat - as.data.frame(matrix(NA, 2, 3)) temp - dbGetQuery(...) if (!is.null(temp)) {xmat[i, 2:3] - temp} I'm adding data to only some columns of a larger matrix, on a row-by-row basis. Best wishes, Mikkel --- Marc Schwartz (via MN) [EMAIL PROTECTED] wrote: On Thu, 2005-07-07 at 10:20 -0700, Mikkel Grum wrote: I have a situation where I'm filling out a dataframe from a database. Sometimes the database query doesn't get anything, so I end up trying to place NULL in the dataframe like below. temp - NULL xmat - as.data.frame(matrix(NA, 2, 3)) xmat[1, 2:3] - temp Error in if (m n * p (n * p)%%m) stop(gettextf(replacement has %d items, need %d, : missing value where TRUE/FALSE needed I can't get the programme to accept that sometimes what the query looks for just doesn't exist, and I just want to move on to the next calculation leaving the dataframe with a missing value in the given cell. It's a real show stopper and I haven't found a way round it. Best wishes, Mikkel PS. I'm using dbGetQuery to query an SQLite database. NULL represents a zero length object in R. Thus, trying to set only the first row in a data frame to NULL makes no sense, since you cannot have a 0 length object that also has a single row (as you seem to be trying to do above). Since a data frame is a series of lists, you could do the following: temp - NULL xmat - as.data.frame(matrix(NA, 2, 3)) xmat V1 V2 V3 1 NA NA NA 2 NA NA NA xmat[, 1] - temp xmat V2 V3 1 NA NA 2 NA NA which removes the first column in the data frame. This is the same as: xmat[, -1] V2 V3 1 NA NA 2 NA NA You could also set the entire xmat to NULL as follows: xmat V1 V2 V3 1 NA NA NA 2 NA NA NA xmat - NULL xmat NULL You can then test to see if 'xmat' is a NULL: is.null(xmat) [1] TRUE and base a boolean expression and resultant action on that result: if (!is.null(xmat)) { do_calculations... } If your calculations are on a row by row basis, where NA's represent missing data, you can also use one of several functions to eliminate those rows. See ?na.action, ?na.omit and ?complete.cases for more information and examples. HTH, Marc Schwartz __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Sweave with layout() and loop
When I try the following code with the Windows graphics window, a new window is opened for each multiple of four images I produce. par(layout(matrix(c(1,2,3,4), 2, 2, byrow = TRUE)), mar = c(2, 3, 2, 3)) for (i in 1:n) { image(... ) } When I try to do the same with Sweave to produce a pdf document, I only get one graphic with the first four graphs. How do I get the rest when n is greater than four? Plots, fig=TRUE, eps=FALSE, echo=FALSE, results=hide, width=6.8, height=9.8= par(layout(matrix(c(1,2,3,4), 2, 2, byrow = TRUE)), mar = c(2, 3, 2, 3)) for (i in 1:n) { image(... ) } Any ideas? cheers, Mikkel __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] plot/lm/abline
Hi Mathias, Try abline(lm(z2 ~ z1)) Mikkel .. Hi, when I run plot.default(z1, z2, xlab = x, ylab = y, main = , pch = +) abline(lm(z1 ~ z2)) then the plot is plotted perfectly (scatterplot), however, the lm() function doesnt appear on the plot. What could be wrong? (Yesterday it worked perfectly, with the lm() line.) Running R 2 on OS X. Mathias Hunskår Furevik Norway __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] x[x$a==q,,drop=TRUE]
I'm trying to select a subset of a dataframe while dropping some factors. While the dataset gets smaller all Factor levels remain and I need to get rid of them. Strangely enough, I am almost certain that the same code on the same data worked OK earlier today - and it is not the first time that I'm not able to replicate earlier results with this command (I know, I might just be going crazy). What am I doing wrong? I'm working on Windows Server 2003, R 2.1.0 (2005-04-18). str(spray) `data.frame': 370 obs. of 7 variables: $ PD: Factor w/ 8 levels Botrytis,Downy,..: 2 2 2 2 4 2 2 5 5 5 ... $ postSpmtsQ: num 1309 1309 384 384 1044 ... $ ante62Q : num 284 284 218 218 366 ... $ ante08Q : num 331 331 228 228 492 ... $ ante29Q : num 297 297 1067 1067 1034 ... $ ante16Q : num 0 0 0.2 0.2 0 0 0 6.7 0 31.5 ... $ Trt : Factor w/ 41 levels Acrobat MZ WP,..: 27 5 27 5 36 27 5 24 24 24 ... sprayS - spray[spray$PD == Spidermites, , drop = TRUE] str(sprayS) `data.frame': 111 obs. of 7 variables: $ PD: Factor w/ 8 levels Botrytis,Downy,..: 5 5 5 5 5 5 5 5 5 5 ... $ postSpmtsQ: num 13395 31588 84254 136 619 ... $ ante62Q : num 1357 21187 21819 218 237 ... $ ante08Q : num973 21740 25112 228 134 ... $ ante29Q : num2103 106970 66676 1067119 ... $ ante16Q : num 6.7 0 31.5 0.2 0 0 0 0 14.3 0 ... $ Trt : Factor w/ 41 levels Acrobat MZ WP,..: 24 24 24 24 24 24 24 24 24 24 ... table(sprayS$Trt) Acrobat MZ WP Agrifos Apollo 50 SC CALMAG 0 013 0 DM-31Dynamec 1.8 EC Equation Pro DF Evisect S 013 0 0 Flint Floramite Impulse Karate 015 0 0 Karate zeonMelodyMeltatox 40 EC MKP 0 0 0 0 Molasses Nembicidine Nimrod 250 EC Nissorun 10 EC 0 0 0 12 Oberon Orthene 75 WP Oscar 20 SC Pegasus 15 0 9 26 Polar 50 WSGPotfos Proplant Pyrus 0 0 0 0 Ridomil MZ 63 5WP Rovral aqua flo Score 250 EC Secure 36 SC 0 0 0 8 Sequestrone Shavit f Sporekill Stroby 50 WG 0 0 0 0 SwitchTracer Trafos K Vandozeb 0 0 0 0 Vitomex 0 cheers, Mikkel __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Zelig: starting values for negbin
I'm running a negative binomial model in zelig and it's asking me to supply starting values for the coefficients. How do I supply these? Could I use the coefficients from the poisson model (which runs smoothly)? z.out - zelig(postSpmtsQ ~ (ante62Q + ante08Q + ante29Q + ante16Q) * Trt, model = negbin, data = sprayS) Error: no valid set of coefficients has been found: please supply starting values I don't find anything on this in the documentation and would appreciate any hints. Mikkel __ Find restaurants, movies, travel and more fun for the weekend. Check it out! __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] image() z-values beyond zlim
Dear useRs, When plotting with image(), I would like the z-values that extend beyond the upper zlim to be indicated with one colour, or preferably with som sort of hatching, as I'm printing in black and white. By default these values just show up as blank areas in the image. I've tried all sorts of things, but nothing seems to work for me. Any solutions would be greatly appreciated. cheers, Mikkel __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Supressing empty sections with Sweave
Frank Harrel proposed the following solution: \ifnum\Sexpr{x}=1 \section{} . . . \fi and it works. Amazingly simple! cheers, Mikkel --- Frank E Harrell Jr [EMAIL PROTECTED] wrote: Mikkel Grum wrote: Dear useRs, I'm writing regular survey reports using Sweave. Each report has several sections along the lines of: \section*{Disease X} MapX,fig=TRUE,echo=FALSE= image(vectorx,vectory,matrixz) @ Notes with or without Sexpr{a}. \vfill \pagebreak \section*{Disease Y} MapY,fig=TRUE,echo=FALSE= ...etc. Often one or more of the diseases is not observed (all values in matrixz are 0), in which case I would prefer not to display the section at all. Does any one no whether it is possible automate this with Sweave? Mikkel __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Supressing empty sections with Sweave
Dear useRs, I'm writing regular survey reports using Sweave. Each report has several sections along the lines of: \section*{Disease X} MapX,fig=TRUE,echo=FALSE= image(vectorx,vectory,matrixz) @ Notes with or without Sexpr{a}. \vfill \pagebreak \section*{Disease Y} MapY,fig=TRUE,echo=FALSE= ...etc. Often one or more of the diseases is not observed (all values in matrixz are 0), in which case I would prefer not to display the section at all. Does any one no whether it is possible automate this with Sweave? Mikkel __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] scheduling R tasks under windows
I'm trying to schedule R tasks in Windows Server 2003. I can run the following from the DOS prompt without any difficulty: c:\Reportsc:\r\rw2001\bin\rterm.exe --no-restore --no-save test.R test.out where test.r has two lines: library(tools); Sweave(rlr.Rnw). When I try to run the same from the task scheduler, I fill in the dialogue box as follows: Run:c:\r\rw2001\bin\rterm.exe --no-restore --no-save test.R test.out Start in: c:\Reports Which opens Rterm, but is preceded by ARGUMENT 'test.R' __ignored__ and ARGUMENT 'test.out' __ignored__ Anyone know what I'm doing wrong? Mikkel __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] RSQLite query error
Dear R-helpers, I ran the following little test on RSQLite and got the data below from the query. Unless I've made some mistake, the results of both the where and order by statements have problems: library(RSQLite) con-dbConnect(dbDriver(SQLite),dbname=test) data(USArrests) dbWriteTable(con,arrests,USArrests,overwrite=TRUE) dbListTables(con) dbReadTable(con,arrests) dbGetQuery(con,paste(SELECT row_names,Murder,Rape FROM arrests, WHERE Rape30 ORDER BY Murder)) row_names Murder Rape 1 Alaska 10.0 44.5 2 New Mexico 11.4 32.1 3 Michigan 12.1 35.1 4 Nevada 12.2 46.0 5Florida 15.4 31.9 6 North Dakota0.8 7.3 7 New Hampshire2.1 9.5 8 Maine2.1 7.8 9 Rhode Island3.4 8.3 10 West Virginia5.7 9.3 11 Colorado7.9 38.7 12 Arizona8.1 31.0 13California9.0 40.6 I'm running R 2.0.0 on Windows XP. Should I make a bug report or can someone point to an error that I've made? cheers Mikkel __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] RODBC query on one line
Dear R-helpers, When I use sqlQuery in the package RODBC, I cannot break the line, but have to write the entire SQL Query on the same line. Is this expected behaviour? It is definitely workable, but makes the queries a slightly difficult to read and edit. I'm using R 1.9.1 and RODBC 1.0-4 on Windows Server 2003 and querying a Sybase database. Best wishes, Mikkel __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] RODBC query on one line
Thanks Brian and Sean, Works well and solves another problem I had: changing the same condition in a series of related but different queries, by making only one change in a variable that is then used in all the queries. Mikkel --- Sean Davis [EMAIL PROTECTED] wrote: I often use paste to build up SQL queries into line-sized chunks, but this is only a convenience and not required. It does improve readability and maintainability, in my opinion. Sean On Sep 1, 2004, at 5:09 AM, Mikkel Grum wrote: Dear R-helpers, When I use sqlQuery in the package RODBC, I cannot break the line, but have to write the entire SQL Query on the same line. Is this expected behaviour? It is definitely workable, but makes the queries a slightly difficult to read and edit. I'm using R 1.9.1 and RODBC 1.0-4 on Windows Server 2003 and querying a Sybase database. Best wishes, Mikkel __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] data files for packages
Dear R-helpers, This must be really simple and clearly expained somewhere, but I can't find it. I'm writing an R-package and want to create data sets for the examples. How do I save them in the format needed for a package? cheers, Mikkel __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] latitude longitude data
Dear R-helpers, I get GPS readings with bug counts (bugs meaning insects in this case) made along rows in crop fields and use these to make maps of bug distribution. The GPS readings are not quite accurate enough for my purpose, so since I know what row each reading is made in, I adjust the latitudinal coordinate using: grd-lm(lat~lon+Row,data) data$lat-predict(grd[,c(lon,Row)]) which adjusts the latitude pretty well when the rows run East-West, but not at all when the rows run North-South, and it doesn't adjust the longitude at all. Is there a better approach I could use to adjust both longitude and latitude onto the nearest point in the row, whatever the direction of the rows? In other words, move the point onto the row in a direction that is perpendicular to the row? cheers, Mikkel __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] vectorized lines
Hi, I thought that the following code would give me a set of parallel lines on a plot as in the second example. q-c(-2253,-2119,-1985,-1850) p-c(1,2,3,4) a-rep(min(p),4) b-rep(max(p),4) plot(p,q) # example 1 lines(c(a,b),c(q,q)) Now this gives me the lines I really want: # example 2 lines(c(a[1],b[1]),c(q[1],q[1])) lines(c(a[2],b[2]),c(q[2],q[2])) lines(c(a[3],b[3]),c(q[3],q[3])) lines(c(a[4],b[4]),c(q[4],q[4])) I assumed that example 1 was a shorter (vectorized) way of writing example 2?? What have I got wrong? cheers, Mikkel __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] factor - numeric
Hi, I'm extracting data from a database with values for different observation types in the same variable (another variable deifnes the observation type). Some of these observation types are factors, so R naturally classifies the entire variable as a factor. I want to select a subset and convert the values to numeric values, but it isn't working; as shown below: rsm2-subset(rsm,PlantStrataId==2,select=c(Value)) rsm2$Value [1] 70 30 15 50 [26]2 [51] 6 [76] 3010 [101]17 15 [126] 24 Levels: 0-25 1 10 12 13 15 17 2 20 23 25 25-50 3 30 4 40 5 50 50-75 ... Present rsm2$Value-as.numeric(rsm2$Value) Warning message: NAs introduced by coercion rsm2$Value [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [26] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [51] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [76] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [101] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [126] NA is there any way out? Help much appreciated. cheers, Mikkel __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] converting text coordinates to decimal degrees
I receive GPS readings in a text string such as 0121.6723S 03643.6893E and need the coordinates as decimal degrees in two separate variables: -1.361205 and 36.728155. How do I do this in R? mikkel __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html