Re: [R] Problem with Plotting in R

2018-12-19 Thread MacQueen, Don via R-help
You haven't described what you are trying to get with the command that doesn't work. My guess is that this might be what you want: plot( x, MyData$NWorth, type="l" ) lines( x, MyData$NWorthSm) However, you might also have to calculate and supply a for the ylim argument to

Re: [R] Different performance with different R versions

2018-12-12 Thread MacQueen, Don via R-help
Probably more appropriate for R-SIG-Mac -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 12/11/18, 12:22 AM, "R-help on behalf of cowan robin" wrote: I am running a small simulation, and getting

Re: [R] How to create gridded data

2018-11-14 Thread MacQueen, Don via R-help
Sarah's answer is probably the best approach, but to do it using very basic R methods that predate the very good spatial support that R now has, I would likely do this: ## Thanks, Jim Lemon, for this step: df1 <- read.table(text= "latitude longitude Precip 45.5 110.5 3.2

Re: [R] missRanger package

2018-11-12 Thread MacQueen, Don via R-help
I could not find the word "censor" in the documentation for the missRanger package, so I think additional explanation is needed. Also, I would expect information about censoring to be included in data provided to a function in a package -- inserting censoring into a package doesn't make sense.

Re: [R] Remove specific rows from nested list of matrices

2018-11-02 Thread MacQueen, Don via R-help
It appears that at the bottom of the nesting, so to speak, you have a character matrix. That is, the contents of the [[1]][[1]][[1]] element is a character matrix that, according to the row and column labels, has 4 rows and 5 columns. However, the matrix itself, as printed, has, apparently, 4

Re: [R] Speeding up R code - Apply a function to each row of a matrix using the dplyr package

2018-11-01 Thread MacQueen, Don via R-help
Without more study, I can only give some general pointers. The as.vector() in X1 <- as.vector(coord[1]) is almost certainly not needed. It will add a little bit to your execution time. Converting the output of func() to a one row matrix is almost certainly not needed. Just return c(res1, res2).

Re: [R] Plot a path

2018-10-31 Thread MacQueen, Don via R-help
Probably sort the data frame by date Then plot( mydf$geogr.longitude, mydf$geogr.latitude, type='l') Search the web for some tutorials See the help pages for plot plot.default -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550

Re: [R] Gamma Simulation from Exponential Distribution: find sample size given power and sig.Level

2018-10-29 Thread MacQueen, Don via R-help
If this is homework, then r-help has a no homework policy. I'm assuming that if it is homework then the focus is on statistical concepts, not on R programming. It looks like your gen_p_vals function should be defined as gen_p_vals <- function(reps = n) instead of n = reps. Why not just

Re: [R] "logical indexing, " [was] match() question or needle haystack problem for a data.frame

2018-10-29 Thread MacQueen, Don via R-help
Wrong comparisons, I think. The opposite of A & B is !(A & B) There is no single operator that can replace the "&" in A & B that gives the "opposite" -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509

Re: [R] date and time data on x axis

2018-10-29 Thread MacQueen, Don via R-help
Here's an example of 24 hours of data at one second intervals. npts <- 24*60*60 df <- data.frame( tm = seq( Sys.time(), by='1 sec', length=npts), yd = round(runif(npts),2) ) head(df) with(df, plot(tm,yd)) The x axis appears to me to be

Re: [R] Genuine relative paths with R

2018-10-08 Thread MacQueen, Don via R-help
A few facts (and some opinions): First fact: R understands relative paths (as do the other languages you mentioned) (you have misunderstood R if you think it doesn't) Second fact: R interprets relative paths as being relative to its current working directory Third fact: To find out

Re: [R] How can I create a loop for this? Please help me

2018-10-05 Thread MacQueen, Don via R-help
Bert (in his separate email) is right about learning the basics, and using R-sig-geo. You're kind of jumping right into the deep end! None the less, here's an example that should help. For one thing, you're being more complicated than necessary. SpatialPointsDataFrame objects can, much of the

Re: [R] Sp-package overlay

2018-10-03 Thread MacQueen, Don via R-help
In addition, the function you want is sp::over or its equivalent in sf -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 10/2/18, 1:41 PM, "R-help on behalf of Ben Tupper" wrote: Hi, I'm

Re: [R] From for loop to lappy?

2018-10-01 Thread MacQueen, Don via R-help
Try A <- lapply(file.names, function(fn) extract_tables(fn) -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 10/1/18, 3:32 PM, "R-help on behalf of Ek Esawi" wrote: Hi All— I am using

Re: [R] Printing standard notation and scientific notation in the same column of a dataframe

2018-09-27 Thread MacQueen, Don via R-help
First compare > format(c(0.52, 0.17, 0.03, 1e-20)) [1] "5.2e-01" "1.7e-01" "3.0e-02" "1.0e-20" > prettyNum(c(0.52, 0.17, 0.03, 1e-20)) [1] "0.52" "0.17" "0.03" "1e-20" > If you want to print one column at a time, that will do what you ask. If you want to print the entire data frame, with

Re: [R] Access function as text from package by name

2018-09-27 Thread MacQueen, Don via R-help
Or sink('stuff.txt') ; graphics::box ; sink() to have it in a text file. -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 9/27/18, 4:55 AM, "R-help on behalf of Rui Barradas" wrote: Hello,

Re: [R] Summarizing R script

2018-09-26 Thread MacQueen, Don via R-help
I wonder if the lintr package might be helpful. -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 9/26/18, 7:00 AM, "R-help on behalf of Spencer Brackett" wrote: R users, Is anyone aware

Re: [R] For Loop

2018-09-24 Thread MacQueen, Don via R-help
In my opinion this is a pretty reasonable question for someone new to R. Yes, it can be written without a for loop, and it would be better. Rich Heiberger gave a good solution early on, but I'd like to add an outline of the reasoning that leads to the solution. You are taking the log of a

Re: [R] How to vectorize this function

2018-09-20 Thread MacQueen, Don via R-help
ve., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 9/20/18, 12:36 PM, "Jeff Newmiller" wrote: re: your last comment... why do you prefer to multiply by the reciprocal? On September 20, 2018 10:56:22 AM PDT, "MacQueen, Don via R-help" wrote

Re: [R] How to vectorize this function

2018-09-20 Thread MacQueen, Don via R-help
In addition to what the other said, if callM is a vector then an expression of the form if (callM <= call0) is inappropriate. Objects inside the parentheses of if() should have length one. For example, > if (1:5 < 3) 'a' else 'b' [1] "a" Warning message: In if (1:5 < 3) "a" else "b" :

Re: [R] as.Date and ylim in empty plot

2018-09-19 Thread MacQueen, Don via R-help
I'm a little surprised at some of what happens, but you can get date labels on the x axis like this: drng <- as.Date( c('2005-1-1' , '2005-12-31') ) plot(1, type="n", xlab="", ylab="", xaxt='n', xlim=drng, ylim=c(-.5, -10)) axis(1, at= pretty(drng), lab=format(pretty(drng))) and if you prefer

Re: [R] Applying by() when groups have different lengths

2018-09-17 Thread MacQueen, Don via R-help
I'm also going to guess that maybe your object rainfall_by_site has already been split into separate data frames (because of its name). But by() does the splitting internally, so you should be passing it the original unsplit data frame. You could supply example data by providing the first

Re: [R] Applying by() when groups have different lengths

2018-09-17 Thread MacQueen, Don via R-help
Try changing it to by(rainfall_by_site, rainfall_by_site[, 'name'], function(x) {mean.rain <- mean(x[, 'prcp']) }) Inside the function, so to speak, the function sees an object named "x", because that's how the function is defined: function(x). So you have to operate on x inside

Re: [R] New to R

2018-09-14 Thread MacQueen, Don via R-help
If l.out is not a data frame, what is it? A list? A matrix? Some other structure? Try str(l.out) class(l.out) and see what you get. Can't help you convert it to a data frame without knowing what it is. After you have a data frame, then write.table(), write.csv(), or write.csv2() will

Re: [R] sink() output to another directory

2018-09-13 Thread MacQueen, Don via R-help
In my experience, any path that can be used at the shell prompt in a unix-alike can be used anywhere that R wants a file name. [that is, when running R on a unix-alike system, and when pwd at the shell prompt returns the same value as getwd() in R] Hopefully, that helps... -Don -- Don

Re: [R] histogram in GNU R....

2018-09-07 Thread MacQueen, Don via R-help
In addition to the other suggestions, try typing x11() before using hist(). That *should* start a graphics window. If it does not, then type capabilities() and see if "X11" is TRUE. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550

Re: [R] Display time of PDF plots

2018-09-05 Thread MacQueen, Don via R-help
(this is somewhat a change of subject from the original question) Rich, there functions such as aggregate() in base R. There are also many options in CRAN packages. But I tend to have difficulty getting them to do exactly what I want, and usually end up rolling my own. The idea is to split

Re: [R] r-data partitioning considering two variables (character and numeric)

2018-08-27 Thread MacQueen, Don via R-help
Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 8/27/18, 4:10 PM, "R-help on behalf of MacQueen, Don via R-help" wrote: You could start with split() grp <- rep('', nrow(mydata) ) grp[mydata$stand_ID

Re: [R] r-data partitioning considering two variables (character and numeric)

2018-08-27 Thread MacQueen, Don via R-help
You could start with split() grp <- rep('', nrow(mydata) ) grp[mydata$stand_ID %in% c(7,9,67)] <- 'A-training' grp[mydata$stand_ID %in% c(3,18,20,21,32)] <- 'B-testing' split(mydata, grp) or perhaps grp <- ifelse( mydata$stand_ID %in% c(7,9,67) , 'A-training', 'B-testing' ) split(mydata, grp)

Re: [R] Multiple counters in a single for loop

2018-08-24 Thread MacQueen, Don via R-help
I don't know of any such option, but it's easy enough to achieve something more or less equivalent. x <- runif(5) for (ir in seq(nrow(myi <- cbind(x, 1:length(x) { i <- myi[ir,1] j <- myi[ir,2] cat(i,j,'\n') } I consider that for() statement to be ugly and unreadable. Normally I

Re: [R] plotmath and logical operators?

2018-08-20 Thread MacQueen, Don via R-help
Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Aug 20, 2018 at 2:00 PM MacQueen, Don via R-help mailto:r-help@r-project.

[R] plotmath and logical operators?

2018-08-20 Thread MacQueen, Don via R-help
I would like to use plotmath to annotate a plot with an expression that includes a logical operator. ## works well tmp <- expression(x >= 3) plot(1) mtext(tmp) ## not so well tmp <- expression(x >= 3 & y <= 3) plot(1) mtext(tmp) Although the text that's displayed makes sense, it won't be

Re: [R] Understanding read.csv error message

2018-08-17 Thread MacQueen, Don via R-help
small typo in previous: should be quote="" (I left behind a single quote by mistake) -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 8/17/18, 5:03 PM, "R-help on behalf of MacQueen,

Re: [R] Understanding read.csv error message

2018-08-17 Thread MacQueen, Don via R-help
Hi Rich, It's not obvious what would be causing that error from read.csv. But here's what I would probably try: Add quote='"" to your arguments. The default is to use surround text strings with double quotes, but your file doesn't. Copy the first few rows into another file and try it. If it

[R] Using rmarkdown with many plots created in a loop

2018-08-16 Thread MacQueen, Don via R-help
I would appreciate some suggestions of a good way to prepare a report using rmarkdown, in which I loop through subsets of a data set, creating a plot of each subset, and interspersing among the figures some text relevant to each figure. One way is to have an R script write the rmd file, then

Re: [R] searching for a specific row name in R

2018-08-15 Thread MacQueen, Don via R-help
It depends on what you want to check. There are a lot of things you can check without looping. For example, if you want to check whether all of the first letters of the identifier names are "A", you could do table( substr(idnames,1,1)) to show you all of the first letters, and how many there

Re: [R] searching for a specific row name in R

2018-08-13 Thread MacQueen, Don via R-help
Or to return a logical value, i.e., TRUE if the column contains the value, FALSE if it does not: any( x[,2] == 'A501' ) -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 8/13/18, 12:09 AM, "R-help

Re: [R] Resolving installed package updates warning

2018-08-10 Thread MacQueen, Don via R-help
Rich, C++11 is a programming language. Interestingly, someone else asked for help yesterday with exactly the same error message for rgdal. The subject line was "Help reinstalling rgdal (Ubuntu 16.04)" The suggestions were to visit the mailing lists R-sig-geo and/or R-sig-debian. It's

Re: [R] Resolving installed package updates warning

2018-08-10 Thread MacQueen, Don via R-help
I would start by trying to install rgdal by itself, rather than as part of a "batch" update. As in Install.packages('rgdal') My expectation is that you will see a more complete error message specific to rgdal, which presumably will provide a clue or pointer. -Don -- Don MacQueen Lawrence

Re: [R] Help reinstalling rgdal (Ubuntu 16.04)

2018-08-09 Thread MacQueen, Don via R-help
There are quite a few messages on R-sig-geo about installing rgdal on Ubuntu. Maybe one of them contains your solution? (I use Mac, so can't help directly) -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509

Re: [R] error with the expand.grid command

2018-08-09 Thread MacQueen, Don via R-help
In my experience, error messages that reference a closure usually mean that you have supplied a function where you aren't supposed to. In this case, I'd look and see if lon, lat, or time is a function (by accident, of course). More specifically, right now in one of my R sessions, I get: >

Re: [R] Plot function: hourly data and x-axis

2018-08-09 Thread MacQueen, Don via R-help
Normally, one turns off the x-axis tick marks and labels by supplying xaxt='n' in the plot() call, and then adds a customized x-axis using the axis() function. But without more information, little help can be provided (a vague question receives a vague answer). I'd suggest reviewing the

Re: [R] Combinations of true/false values where one pair is mutually exclusive

2018-08-02 Thread MacQueen, Don via R-help
From what I can tell, the simplest way is to First generate all the combinations Then exclude those you don't want. Here's an example, with only three variables (D, E, and F), that excludes those where E and F both fail > tmp <- c('p','f') > X <- expand.grid(D=tmp, E=tmp, F=tmp) > X <-

Re: [R] read txt file - date - no space

2018-07-30 Thread MacQueen, Don via R-help
Or, without removing the first line dadf <- read.table("xxx.txt", stringsAsFactors=FALSE, skip=1) Another alternative, dadf$datetime <- as.POSIXct(paste(dadf$V1,dadf$V2)) since the dates appear to be in the default format. (I generally prefer to work with datetimes in POSIXct class rather

Re: [R] Formatting summary() output to a file

2018-07-27 Thread MacQueen, Don via R-help
Hmmm. I do get output in the file with the first example, and the second example is too complicated for me. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 7/27/18, 9:56 AM, "R-help on behalf of

Re: [R] Formatting summary() output to a file

2018-07-27 Thread MacQueen, Don via R-help
Given your description, I would start with sink('wysumallyrs.txt') print( summary(wyallyrs) ) sink() and see if that doesn't meet your needs. Some of the basic principles: (1) Whenever you type the name of an R object at the R prompt, it is as if R wraps whatever you typed inside print().

Re: [R] SQL Database

2018-07-26 Thread MacQueen, Don via R-help
Harold, I don't have much experience with ODBC/RODBC, but given that it's working on Win, a driver problem seems plausible. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 7/26/18, 9:37 AM,

Re: [R] SQL Database

2018-07-26 Thread MacQueen, Don via R-help
From my point of view, the logic is this: If the external database is Oracle, use ROracle If the external database is MySQL, use RMySQL and similarly for other databases If there is no R package specific to the database, then you drop back to RODBC or RJDBC. Hopefully you can get the

Re: [R] A couple of batch mode questions

2018-07-25 Thread MacQueen, Don via R-help
From my perspective, which is a unix-alike perspective, Rscript makes R useable in exactly the same way as other unix style scripting languages such as perl, tcsh, bash, etc. This is useful, and a good thing. If I remember (and understood) correctly, it is why Rscript was introduced, later in

Re: [R] optim function

2018-07-13 Thread MacQueen, Don via R-help
There's a CRAN Task View on optimization. There might be something useful there. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 7/13/18, 11:43 AM, "R-help on behalf of Federico Becerra" wrote:

Re: [R] (no subject)

2018-07-11 Thread MacQueen, Don via R-help
Maybe I missed it, but I didn't see anyone suggest a visit to the CRAN Spatial task view. This would be a good place to start learning how to work with spatial data in R. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell

Re: [R] R couldnt recognize US Pasific timezome

2018-07-09 Thread MacQueen, Don via R-help
Or (perhaps preferably) "US/Pacific" for daylight savings time support. -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 7/9/18, 1:46 AM, "R-help on behalf of Jeff Newmiller" wrote: Several of the

Re: [R] inconsistency in display of character vector....

2018-07-08 Thread MacQueen, Don via R-help
In addition to what Ben and Jeff have said, I think you can simplify your function considerably. See these examples: > substr( c('abc', 'abcd','abcde') , c(2,1,3), c(2,2,4)) [1] "b" "ab" "cd" > foo <- c('abc', 'abcd','abcde') > substr( foo , 1, nchar(foo)-2) [1] "a" "ab" "abc" > foo <-

Re: [R] trouble with exiting loop if condition is met

2018-06-28 Thread MacQueen, Don via R-help
Does this example help? > for (ii in 1:10) { cat( ii,'\n') ; if (ii >3) break } 1 2 3 4 The loop won't stop unless you tell it to stop, and I don't see any place where you told it to stop. -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550

Re: [R] as.Date and Legend in a plot

2018-06-22 Thread MacQueen, Don via R-help
What David Winsemius said, plus: Does legend('topleft', c("Climax", "Thule", "Sopo"), lty = 1, col = ("black","red","blue")) not work? It should, which will illustrate that you CAN add a legend to the plot. The x,y that you supply to legend must be values that are within the x,y range of

Re: [R] numeric comparison error

2018-06-18 Thread MacQueen, Don via R-help
What Jeff, said, plus to see it explicitly: > print(cpgbins[5:7], digits=18) [1] 0.200011 0.25 0.300044 > print(c(0.2, 0.25, 0.3), digits=18) [1] 0.200011 0.25 0.299989 -Don -- Don MacQueen Lawrence Livermore

Re: [R] subsetting lists....

2018-06-18 Thread MacQueen, Don via R-help
The unlist solution is quite clever. But I will note that none of the solutions offered so far succeed if the input is, for example, YH <- list(1:5, letters[1:3], 1:7) iuhV <- c(2,2,4) and the desire is to return a list whose elements are of the same types as the input list. Which

Re: [R] rgdal in 3.5 fails(?)

2018-06-15 Thread MacQueen, Don via R-help
In case no one else has made the suggestion yet, take this question to R-sig-geo, where there has already been discussion and sharing of information and experiences about this. You'll need to include the output of sessionInfo(). -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000

Re: [R] Calling r-scripts with arguments from other scripts, or possibly R programming conventions/style guide

2018-06-13 Thread MacQueen, Don via R-help
When I want to run multiple scripts one after the other, and have variables created in a script be still in memory for use by a subsequent script, I normally create a master script (say, "runall.r") and it sources each of the others in turn. For example, my master script (runall.r) would look

Re: [R] Time and date conversion

2018-06-06 Thread MacQueen, Don via R-help
After you've solved the format inconsistency issues, per Peter's advice, you will need to understand that R internally converts and stores the timedate values in UTC. Therefore, it is absolutely essential to give it the correct timezone specification on input. The user does not "convert to UTC

Re: [R] Time-series moving average question

2018-06-01 Thread MacQueen, Don via R-help
For your first question, you’re doing moving averages of 3 points (I assume that’s what order=3 does). For any given time point of your input data, that would be one before, one at, and one after the given time point. Do all of your input times have both one before and one after? Don’t know

Re: [R] Time-series moving average question

2018-06-01 Thread MacQueen, Don via R-help
You are right that there are no NAs in the practice data. But there are NAs in the moving average data. To see this, break your work into two separate steps, like this: tnr.ma <- ma(dat3[1:28], order=3) TNR_moving_average <- forecast(tnr.ma, h=8) I think you will find that the warning

Re: [R] Time-series moving average question

2018-06-01 Thread MacQueen, Don via R-help
My guess would be that if you inspect the output from ma(dat3[1:28], order=3) you will find some NAs in it. And then forecast() doesn't like NAs. But I can't check, because I can't find the ma() and forecast() functions. I assume they come from some package you installed; it would be helpful

Re: [R] how to make the code more efficient using lapply

2018-05-25 Thread MacQueen, Don via R-help
Eric's approach seems reasonable to me, and I agree that it's probably not the use of a "for" loop that makes the original version slow. As Eric mentioned, there are lots of unnecessary things happening in the loop. For example, list.files() was called twice inside the loop, which is

Re: [R] Manipulation of data.frame into an array

2018-05-24 Thread MacQueen, Don via R-help
It would help if you show exactly the structure of your desired result, using the simple example data you supplied (what, exactly, do you mean by "array"?) If you want mydata[[1]] "to provide the values for all three 3 variables (Y, X1 and X2) of the first imputation only" then this will do it: