Re: [R] getting data from a "vertical" table into a "2-dimensional" grid

2022-10-21 Thread Bert Gunter
Well, if you know the column subscripts you need, just forget about the names! I would just write a (one-liner) function to do it for any data frame: myfun <- function(dat)tapply(dat[,3], dat[,1:2], sum) ## dat[,1:2] is a list because it's a data frame and all data frames are lists

Re: [R] getting data from a "vertical" table into a "2-dimensional" grid

2022-10-21 Thread Kelly Thompson
Bert, Thanks! I'm pretty sure what you provided gets me to what I was looking for, and is much simpler. I really appreciate your help. A follow-up question: I adjusted the code to not use "hard-coded" column names. mat2 <- with(data_original, tapply( get(names(data_original)[3]), list(

Re: [R] getting data from a "vertical" table into a "2-dimensional" grid

2022-10-21 Thread Bert Gunter
"As my end result, I want a matrix or data frame, with one row for each year, and one column for each category." If I understand you correctly, no reshaping gymnastics are needed -- just use ?tapply: set.seed(1) do <- data.frame(year = rep(1990:1999, length = 50), category = sample(1:5, size =

Re: [R] getting data from a "vertical" table into a "2-dimensional" grid

2022-10-21 Thread Kelly Thompson
As my end result, I want a matrix or data frame, with one row for each year, and one column for each category. On Fri, Oct 21, 2022 at 6:23 PM Kelly Thompson wrote: > > # I think this might be a better example. > > # I have data presented in a "vertical" dataframe as shown below in >

Re: [R] getting data from a "vertical" table into a "2-dimensional" grid

2022-10-21 Thread Kelly Thompson
# I think this might be a better example. # I have data presented in a "vertical" dataframe as shown below in data_original. # I want this data in a matrix or "grid", as shown below. # What I show below seems like one way this can be done. # My question: Are there easier or better ways to do

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Jeff Newmiller
Your selection priorities are inverted: ifelse is vectorised... use it to make many individual choices at once with a single atomic vector result. If-else is a control-flow construct... you can return one object of any type from it based on a single comparison or alter variables and ignore the

Re: [R] getting data from a "vertical" table into a "2-dimensional" grid

2022-10-21 Thread Jeff Newmiller
This operation goes by a variety of names... reshape (stats), cast (reshape2), spread (tidyr), and pivot_wider (tidyr). The stats package reshape function is built-in, but uses terminology that can be confusing, and may not come out sorted the way you want so pre-converting to factor or

[R] getting data from a "vertical" table into a "2-dimensional" grid

2022-10-21 Thread Kelly Thompson
### #I have data presented in a "vertical" data frame as shown below in data_original. #I want this data in a matrix or "grid", as shown below. #What I show below seems like one way this can be done. #My question: Are there easier or better ways to do this, especially in Base R, and also in R

Re: [R] Adding page numbers to existing PDFs

2022-10-21 Thread Gabor Grothendieck
Create a pdf using latex that has only page numbers and then superimpose that with your pdf using the free utility pdftk. The animation R package has an interface to pdftk. Google to locate pdftk and again to locate instructions. There are also freeware GUI Windows programs that are easy to

Re: [R] Adding page numbers to existing PDFs

2022-10-21 Thread J C Nash
It is not automatic, but I've used Xournal for different tasks of editing a pdf. It would certainly allow page numbers to be added, essentially by overlaying a text box on each page. Clumsy, but possibly useful. I tend to use Xournal to blank parts of documents that recipients should not see,

Re: [R] [EXT] Re: Linestring values to vector

2022-10-21 Thread David Stevens
The way I did this recently is to look into the .dbf file that comes with the shapefile group from ESRI or your feature site, and search the feature names for the one I want. This gives you an index to use to search for the feature in the actual shapefile. Then, once the shapefile is loaded

Re: [R] Adding page numbers to existing PDFs

2022-10-21 Thread Dennis Fisher
Bert et al. I searched with slightly different text and found various packages that might be relevant: pdftools staplr QPDF However, as far as I can tell, none of these offers the specific functionality that I need. Dennis Dennis Fisher MD P < (The "P Less Than"

Re: [R] [EXTERNAL] Re: unexpected 'else' in " else"

2022-10-21 Thread Jorgen Harmse via R-help
Thank you. I knew it had nothing to do with the choice of environment, but I thought I had seen such unwrapped code working in files in a previous version. Maybe I misremembered. Incidentally, there is nothing special about braces: anything that makes the statement incomplete will do. Regards,

Re: [R] Linestring values to vector

2022-10-21 Thread Jeff Newmiller
As the Posting Guide would have warned you, you will probably have better luck asking this kind of question on the R-sig-geo mailing list. On October 21, 2022 10:45:30 AM PDT, Nick Wray wrote: >Hello I have downloaded a large shapefile dataset of UK rivers and I want >to isolate (as an ordinary

Re: [R] Adding page numbers to existing PDFs

2022-10-21 Thread Bert Gunter
Have you done a web search on "R packages to edit pdf files" or similar? I got what looked like relevant hits with it. Bert On Fri, Oct 21, 2022 at 10:49 AM Dennis Fisher wrote: > R 4.2.1 > OS X > > Colleagues > > I have multipage PDF files that were created in R — the files do NOT have > page

[R] Linestring values to vector

2022-10-21 Thread Nick Wray
Hello I have downloaded a large shapefile dataset of UK rivers and I want to isolate (as an ordinary R string) the LINESTRING values for particular lines, corresponding to rivers Looking at the first line I can isolate the geometry by Hello I have downloaded a large shapefile dataset of UK rivers

[R] Adding page numbers to existing PDFs

2022-10-21 Thread Dennis Fisher
R 4.2.1 OS X Colleagues I have multipage PDF files that were created in R — the files do NOT have page numbers. I would like to add page numbers after the fact, i.e., read a file into R, add margin text, then output as a PDF. Can this be done in R (base R or a package)? It can be done in

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Ebert,Timothy Aaron
Is there a place where you would use ifelse()? I used it here because it was short and there was no indication of lots of data. In the example I cannot tell the difference of a few hundred milliseconds execution time. Benchmarking code is important for larger problems. Tim -Original

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Andrew Simmons
The code working inside stats::weighted.residuals has nothing to do with being evaluated in a different environment than globalenv() and has nothing to do with being inside a package. The reason the code works inside stats::weighted.residuals is because the function body is wrapped with braces.

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Jorgen Harmse via R-help
Andrew Simmons is correct but doesn't explain why the code works in the package. This is one of only two differences I have found between running code at the command line and running it from a file. (The other difference is that code in a file is often executed in an environment other than

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Jinsong Zhao
Dear John, Thank you very much for the explanation. It cleared up my confusion about the syntax of "if ... else...", which in the help page of "if" said: ``` In particular, you should not have a newline between ‘}’ and ‘else’ to avoid a syntax error in entering a ‘if ... else’

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread John Fox
Dear Jinsong, When you enter these code lines at the R command prompt, the interpreter evaluates an expression when it's syntactically complete, which occurs before it sees the else clause. The interpreter can't read your mind and know that an else clause will be entered on the next line.

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Martin Maechler
> Ebert,Timothy Aaron > on Fri, 21 Oct 2022 12:05:58 + writes: > I can get it to work with > ifelse(is.matrix(r), r[w!=0, , drop=FALSE], r[w!=0]) Note that this is *not* good advice: if(Cnd) A else Bis very much more efficient than ifelse(Cnd, A, B) whenever

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Jinsong Zhao
Thanks a lot! I know the first and third way to correct the error. The second way seems make me know why the code is correct in the function stats::weighted.residuals. On 2022/10/21 17:36, Andrew Simmons wrote: The error comes from the expression not being wrapped with braces. You could

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Ebert,Timothy Aaron
I can get it to work with ifelse(is.matrix(r), r[w!=0, , drop=FALSE], r[w!=0]) With w and r as defined r is not a matrix, so the first part will never execute. The test is for w not equal to zero so it is always true for these vectors. It is usually good to have test code such that all

[R] Opening shapefiles in R

2022-10-21 Thread Nick Wray
Hello I have managed to download and plot the outline for the River Tweed In the Sottish borders) catchment using this code. 21009 is a zipfile downloaded from the FEH website https://fehweb.ceh.ac.uk/Map: require(rgdal) shape <- readOGR(dsn ="C:/Users/nick/Desktop/PhD Oct 22", layer =

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Andrew Simmons
The error comes from the expression not being wrapped with braces. You could change it to if (is.matrix(r)) { r[w != 0, , drop = FALSE] } else r[w != 0] or { if (is.matrix(r)) r[w != 0, , drop = FALSE] else r[w != 0] } or if (is.matrix(r)) r[w != 0, , drop = FALSE] else

[R] unexpected 'else' in " else"

2022-10-21 Thread Jinsong Zhao
Hi there, The following code would cause R error: > w <- 1:5 > r <- 1:5 > if (is.matrix(r)) + r[w != 0, , drop = FALSE] > else r[w != 0] Error: unexpected 'else' in "else" However, the code: if (is.matrix(r)) r[w != 0, , drop = FALSE]

Re: [R] inconsistencies between HLM and clmm results for the signs for the coefficients

2022-10-21 Thread Thierry Onkelinx via R-help
Dear Debbie, Have a look at the vignette of the ordinal package. Look for the equation that defines \eta. And note the minus sign associated with \beta. You'll need to find the equation used in HLM and compare it with the equation from ordinal. Best regards, ir. Thierry Onkelinx Statisticus /