Re: [R] What exactly is an dgCMatrix-class. There are so many attributes.

2017-10-20 Thread William Dunlap via R-help
You should not really have worry about the internal structure of such a thing - just treat it like a matrix. E.g., > train$data[1:3,1:3] # dots mean 0's 3 x 3 sparse Matrix of class "dgCMatrix" cap-shape=bell cap-shape=conical cap-shape=convex [1,] . .

Re: [R] Missing information in source()

2017-11-07 Thread William Dunlap via R-help
Either change your script by adding print(...) calls where you want to see something printed or change the call to source() by adding print.eval=TRUE or echo=TRUE. E.g., > cat(file = tf <- tempfile(), "head(10:1)\ntail(letters)\nx <- gamma(0:4)\nx\n") > source(tf) Warning message: In gamma(0:4)

Re: [R] Calculating frequencies of multiple values in 200 colomns

2017-11-10 Thread William Dunlap via R-help
Use table(factor(x, levels=your3values)) Bill Dunlap TIBCO Software wdunlap tibco.com On Fri, Nov 10, 2017 at 1:32 AM, Allaisone 1 wrote: > > > Thank you for your effort Bert.., > > > I knew what is the problem now, the values (1,2,3) were only an example. > The values I have are 0 , 1, 2 . Tab

Re: [R] Where to get support for Rserve?

2017-11-13 Thread William Dunlap via R-help
> I see everything about the Rseve author...except for a way > to contact him (unless I somehow missed that.) Look at the BugReports and Maintainer components of packageDescription("Rserve"). Use the former if it exists - it is typically a URL. The latter is also given by maintainer("Rserve"). O

Re: [R] lapply and runif issue?

2017-11-15 Thread William Dunlap via R-help
Your lapply is making the call runif(n=3, min=i) for i in 1:3. That runif's 3 argument is 'max', with default value 1 so that is equivalent to calling runif(n=3, min=i, max=1) When i>max, outside the domain of the family of uniform distributions, runif returns NaN's. Bill Dunlap TIBCO Soft

Re: [R] mystery "158"

2017-11-22 Thread William Dunlap via R-help
I think the '[<-' function for atomic types doesn't pay attention to the class of the right hand side, only to its mode. It strips all the attributes of the RHS, including the class. > f <- function(x) { x[2] <- factor("z", levels=letters) ; x } > f(101:103) [1] 101 26 103 > f(c("One","Two","Thr

Re: [R] dplyr - add/expand rows

2017-11-25 Thread William Dunlap via R-help
dplyr may have something for this, but in base R I think the following does what you want. I've shortened the name of your data set to 'd'. i <- rep(seq_len(nrow(d)), d$YEAR_TO-d$YEAR_FROM+1) j <- sequence(d$YEAR_TO-d$YEAR_FROM+1) transform(d[i,], YEAR=YEAR_FROM+j-1, YEAR_FROM=NULL, YEAR_TO=NULL)

Re: [R] dplyr - add/expand rows

2017-11-28 Thread William Dunlap via R-help
69 B > 36 two 70 B > 37 two 71 B > 38 two 72 B > 39 two 73 B > 40 two 74 B > 41 two 75 B > 42 two 76 B > 43 two 77 B > 44 two 78 B > 45 two 79 B > 46

Re: [R] Dynamic reference, right-hand side of function

2017-12-04 Thread William Dunlap via R-help
Note that in for (year in 2000:2007){ varname <- paste0("aa_",year) assign(paste0(varname), as.vector(eval(as.name(varname } the paste0(varname) is redundant - varname was just computed as the return value of paste0(). People are trying to steer you towards making a lis

Re: [R] Dynamic reference, right-hand side of function

2017-12-04 Thread William Dunlap via R-help
By the way, R 'vectors' are not the equivalents of mathematical 'vectors'. In R, a vector is something that can have arbitrary length and which has no 'attributes', other than perhaps element names. Vectors can be numeric, character, complex, lists, etc. Functions, names, and NULL are not vector

Re: [R] Curiously short cycles in iterated permutations with the same seed

2017-12-08 Thread William Dunlap via R-help
Landau's function gives the maximum cycle length of a permutation. See.,e.g., http://mathworld.wolfram.com/LandausFunction.html. Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Dec 7, 2017 at 9:34 PM, Eric Berger wrote: > Hi Boris, > Do a search on "the order of elements of the symmetric g

Re: [R] Remove

2017-12-09 Thread William Dunlap via R-help
You could make numeric vectors, named by the group identifier, of the contraints and subscript it by group name: > DM <- read.table( text='GR x y + A 25 125 + A 23 135 + A 14 145 + A 35 230 + B 45 321 + B 47 512 + B 53 123 + B 55 451 + C 61 521 + C 68 235 + C 85 258 + C 80 654',header = TRUE, stri

Re: [R] inefficient for loop, is there a better way?

2017-12-12 Thread William Dunlap via R-help
Try using stats::filter (not the unfortunately named dplyr::filter, which is entirely different). state>elev is a logical vector, but filter(), like most numerical functions, treats TRUEs as 1s and FALSEs as 0s. E.g., > str( stats::filter( x=examp$stage>elev1, filter=rep(1,7), method="convolution

Re: [R] Add vectors of unequal length without recycling?

2017-12-13 Thread William Dunlap via R-help
Without recycling you would get: u <- c(10, 20, 30) u + 1 #[1] 11 20 30 which would be pretty inconvenient. (Note that the recycling rule has to make a special case for when one argument has length zero - the output then has length zero as well.) Bill Dunlap TIBCO Software wdunlap ti

Re: [R] match and new columns

2017-12-13 Thread William Dunlap via R-help
Use the stringsAsFactors=FALSE argument to read.table when making your data.frame - factors are getting in your way here. Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Dec 13, 2017 at 3:02 PM, Val wrote: > Thank you Rui, > I did not get the desired result. Here is the output from your sc

Re: [R] match and new columns

2017-12-13 Thread William Dunlap via R-help
Try the following (which won't work with factors): > i <- match(tdat$B, tdat$A) > newColumns <- tdat[i, c("B", "C")] > newColumns[is.na(newColumns)] <- "0" > transform(tdat, D=newColumns[["B"]], E=newColumns[["C"]]) A B CY D E 1 A12 B03 C04 0.70 0 0 2 A23 B05 C06 0.05 0 0 3

Re: [R] help with recursive function

2017-12-14 Thread William Dunlap via R-help
Your code contains the lines stopifnot(!(any(data1$norm_sd >= 1))) if(!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } stop() "throws an error", causing the current function and all functions in the call stack to abort and return n

Re: [R] help with recursive function

2017-12-14 Thread William Dunlap via R-help
recursive_funlp <- function(dataset = dat1, func = funlp2) { ... if (!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() # GIVE SOME ARGUMENTS HERE return(df2) } } Whe

Re: [R] something weird has happened....!!!!!!!!!!

2017-12-15 Thread William Dunlap via R-help
You can see if your function uses R's random number generator with the following function. isRandom <- function(expr) { randomSeedBefore <- get0(".Random.seed") force(expr) !identical(randomSeedBefore, get0(".Random.seed")) } isRandom(1:10) #[1] FALSE isRandom(runif(3)>.4) #[1] TRUE I

Re: [R] httr::content without message

2018-01-02 Thread William Dunlap via R-help
You can suppress all messages from that command with junk <- suppressMessages(httr::content(r1)) If you only want to suppress that specific message you can use withCallingHandlers: junk <- withCallingHandlers( httr::content(r1), message=function(e){ if (grepl("P

Re: [R] Replace NAs in split lists

2018-01-08 Thread William Dunlap via R-help
I don't get exactly that error message, > ifelse(is.na(z$Value),z$Value[!is.na(z$Value)][1],z$Value)z}) Error: unexpected symbol in "ifelse(is.na(z$Value),z$Value[!is.na (z$Value)][1],z$Value)z" The 'symbol' in "unexpected symbol" refers to a "name" ('z' in this case). The problem is usually at

Re: [R] R minor version

2018-01-12 Thread William Dunlap via R-help
> .expand_R_libs_env_var("poof/%p/%v") [1] "poof/x86_64-w64-mingw32/3.4" Bill Dunlap TIBCO Software wdunlap tibco.com On Fri, Jan 12, 2018 at 4:49 AM, Loris Bennett wrote: > Hi, > > When I install a package as a non-root user, it gets saved in a path > such as > > ~/R/x86_64-unknown-linux-g

Re: [R] Geometry delaunayn and deldir results, differing results from Octave due to decimal precision?

2018-01-24 Thread William Dunlap via R-help
All three results give the same collection of triangles. They don't all agree on whether the points in a triangle are in clockwise or counterclockwise order. If the orientation matters, it is a simple matter to reverse the order of points in those that described in the "wrong" orientation. Bill

Re: [R] Geometry delaunayn and deldir results, differing results from Octave due to decimal precision?

2018-01-25 Thread William Dunlap via R-help
I just looked at the data at the URL you posted and it looks like it consists of all the points in a rectangular grid. When you triangulate a rectangle it is arbitrary whether you use the SW-NE or the SE-NW diagonal and that looks like the only difference between the various algorithms. Bill Dun

Re: [R] Newbie wants to compare 2 huge RDSs row by row.

2018-01-27 Thread William Dunlap via R-help
If your two objects have class "data.frame" (look at class(objectName)) and they both have the same number of columns and the same order of columns and the column types match closely enough (use all.equal(x1, x2) for that), then you can try which( rowSums( x1 != x2 ) > 0) E.g., > x1 <- data.fr

Re: [R] Add ablines

2018-01-29 Thread William Dunlap via R-help
I assume you are using the qcc package's qcc function. example(qcc) gives an example of adding abline's to its plot: # add warning limits at 2 std. deviations q <- qcc(diameter[1:25,], type="xbar", newdata=diameter[26:40,], plot=FALSE) (warn.limits <- limits.xbar(q$center, q$std.dev, q$sizes,

Re: [R] Calculating angle of a polyline

2018-01-30 Thread William Dunlap via R-help
I like to use complex numbers for 2-dimensional geometry. E.g., > polyAngles2 function (xV, yV) { stopifnot((length(xV) == length(yV)) && (length(xV) >= 3)) z <- complex(re = xV, im = yV) c(NA, diff(Arg(diff(z))), NA) # radians, positive is counter-clockwise } > x <- c(0:3) > y <- c(0

Re: [R] Aggregate behaviour inconsistent (?) when FUN=table

2018-02-06 Thread William Dunlap via R-help
Don't use aggregate's simplify=TRUE when FUN() produces return values of various dimensions. In your case, the shape of table(subset)'s return value depends on the number of levels in the factor 'subset'. If you make B a factor before splitting it by C, each split will have the same number of leve

Re: [R] using cat to log to file with sapply

2018-02-14 Thread William Dunlap via R-help
testit<-function(x,...){ paste0('this is x: ',x)->y return(y) catf("++test=",...) } You return from the function before calling catf(). Remove the 'return(y)' and make 'y' the last expression in the function. Bill Dunlap TIBCO Software wdunlap tibco.com On We

Re: [R] deparseDots to get names of all arguments?

2018-02-20 Thread William Dunlap via R-help
Does substitute(...()) do what you want? > myFunc <- function(x, ...) substitute(...()) > myFunc(y=1/(1:10), x=sin(3:1), z=stop("Oops"), "untagged arg") $y 1/(1:10) $z stop("Oops") [[3]] [1] "untagged arg" > names(.Last.value) [1] "y" "z" "" Bill Dunlap TIBCO Software wdunlap tibco.com On Tu

Re: [R] change location of temporary files

2018-02-23 Thread William Dunlap via R-help
Does setting the environment variable TMPDIR, before starting R, to a directory on a bigger file system help? On Linux I get % mkdir /tmp/RTMP-BILL % env TMPDIR=/tmp/RTMP-BILL R --quiet --vanilla > tempdir() [1] "/tmp/RTMP-BILL/Rtmppgowz4" > tempfile() [1] "/tmp/RTMP-BILL/Rtmppgowz4/f

Re: [R] include

2018-02-24 Thread William Dunlap via R-help
x1 = rbind(unique(preval),mydat) x2 <- x1[is.na(x1)] <- 0 x2 # gives 0 Why introduce the 'x2'? x1[...] <- 0 alters x1 in place and I think that altered x1 is what you want. You asked why x2 was zero. The value of the expression f(a) <- b and assignments are processed right to left

Re: [R] include

2018-02-24 Thread William Dunlap via R-help
chr [1:2] "C" "E" ' > x1 <- (2:5) + 1i * (5:2) > x2 <- x1[ Mod(x1) == 5 ] <- c(17L, 19L) > str(x1) cplx [1:4] 2+5i 17+0i 19+0i ... > str(x2) int [1:2] 17 19 Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Feb 24, 2018 at 3:04 PM, Dunc

Re: [R] Precision in R

2018-02-26 Thread William Dunlap via R-help
In the R expression x[1] + x[2] the result must be stored as a double precision number, because that is what R "numerics" are. sum() does not have to keep its intermediate results as doubles, but can use quad precision or Kahan's summation algorithm (both methods involve more than a simple doub

Re: [R] Random Seed Location

2018-02-26 Thread William Dunlap via R-help
If your computations involve the parallel package then set.seed(seed) may not produce repeatable results. E.g., > cl <- parallel::makeCluster(3) # Create cluster with 3 nodes on local host > set.seed(100); runif(2) [1] 0.3077661 0.2576725 > parallel::parSapply(cl, 101:103, function(i)runif(2, i,

Re: [R] how to make row.names based on column1 with duplicated values

2018-03-01 Thread William Dunlap via R-help
You can do this with ave(): gene <- c("a","b","c","d","c","d","c","f") ave(gene, gene, FUN=function(x)if(length(x)>1)paste(x,seq_along(x),sep="-") else x) # [1] "a" "b" "c-1" "d-1" "c-2" "d-2" "c-3" "f" You can probably speed it up a bit by pulling the paste() out of FUN and doing it later.

Re: [R] Capturing warning within user-defined function

2018-03-06 Thread William Dunlap via R-help
You can capture warnings by using withCallingHandlers. Here is an example, its help file has more information. dataList <- list( A = data.frame(y=c(TRUE,TRUE,TRUE,FALSE,FALSE), x=1:5), B = data.frame(y=c(TRUE,TRUE,FALSE,TRUE,FALSE), x=1:5), C = data.frame(y=c(FALSE,FALSE,TRUE,TRUE,TRUE)

Re: [R] Capturing warning within user-defined function

2018-03-06 Thread William Dunlap via R-help
tryCatch() is good for catching errors but not so good for warnings, as it does not let you resume evaluating the expression that emitted the warning. withCallingHandlers(), with its companion invokeRestart(), lets you collect the warnings while letting the evaluation run to completion. Bill Dunl

Re: [R] Equivalent of gtools::mixedsort in R base

2018-03-12 Thread William Dunlap via R-help
1- mixedorder does not work in a "do.call(mixedorder, mydataframe)" call like the order function does This is tangential, but do.call(order, mydataframe) is not safe to use in a general purpose function either - you need to remove the names from the second argument: > d <- data.frame(meth

Re: [R] Bug report: override stopifnot() ?

2018-03-12 Thread William Dunlap via R-help
Why don't you use stopifnot( all(m1 == m2) ) ? Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Mar 12, 2018 at 8:15 AM, Stepan Kasal wrote: > Hello, > I stumbled over a problem: >stopifnot(m1 == m2) > > It works with vector or matrix, but does not work for classes from Matrix > pack

Re: [R] Possible Improvement to sapply

2018-03-13 Thread William Dunlap via R-help
Wouldn't that change how simplify='array' is handled? > str(sapply(1:3, function(x)diag(x,5,2), simplify="array")) int [1:5, 1:2, 1:3] 1 0 0 0 0 0 1 0 0 0 ... > str(sapply(1:3, function(x)diag(x,5,2), simplify=TRUE)) int [1:10, 1:3] 1 0 0 0 0 0 1 0 0 0 ... > str(sapply(1:3, function(x)diag(x,

Re: [R] Possible Improvement to sapply

2018-03-13 Thread William Dunlap via R-help
Could your code use vapply instead of sapply? vapply forces you to declare the type and dimensions of FUN's output and stops if any call to FUN does not match the declaration. It can use much less memory and time than sapply because it fills in the output array as it goes instead of calling lappl

Re: [R] Sum of columns of a data frame equal to NA when all the elements are NA

2018-03-21 Thread William Dunlap via R-help
Use rowSums(is.na(d))==ncol(d) to pick out the rows will no good values. E.g., > d <- data.frame(x1=c(NA,2:4), x2=c(NA,NA,13:14), x3=c(NA,NA,NA,44)) > d x1 x2 x3 1 NA NA NA 2 2 NA NA 3 3 13 NA 4 4 14 44 > s <- rowSums(d, na.rm=TRUE) > s [1] 0 2 16 62 > s[ rowSums(is.na(d))==ncol(d) ] <- NA

Re: [R] GlobalEnv error

2018-03-22 Thread William Dunlap via R-help
You haven't given much context, but these error message can come from load() when loading a *.RData file that contains objects reference packages not installed in the current installation of R. When R starts it will load a ".RData" file if one exists, this file is typically created when R shuts do

Re: [R] GlobalEnv error

2018-03-22 Thread William Dunlap via R-help
Find the .RData file and rename it to anything else. Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Mar 22, 2018 at 7:09 PM, Haida wrote: > Previously, the R software was perfectly run without error and I am not > sure why this happens. I have not installed any new R. If the *RData file >

Re: [R] convert numeric variables to factor

2018-04-09 Thread William Dunlap via R-help
Or use cut(): > Num <- c(2.2, 2.4, 3.5, 5, 7) > cut(Num, breaks=c(0,2,4,6), labels=c("Low","Medium","High")) [1] Medium Medium Medium High Levels: Low Medium High Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Apr 9, 2018 at 10:00 AM, Bert Gunter wrote: > Just cast it! > > ?factor >

Re: [R] Spectral analisys for for R version 3.4.3

2018-04-10 Thread William Dunlap via R-help
stats::spectrum for starters. Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Apr 10, 2018 at 5:49 AM, Danniel Lafeta Machado < danniel.laf...@bcb.gov.br> wrote: > Dear all, > Is there any spectral analisys functionality available for R version 3.4.3? > Series() functionality doesn't work i

Re: [R] WGCNA package installation segmentation fault

2018-04-12 Thread William Dunlap via R-help
I get the seg-fault on exiting R after loading WGCNA into R-3.4.3 on Linux. Valdgrind shows the problem occurs when tearing down an Rstreambuf object: % R --debugger valgrind --vanilla --quiet ==30889== Memcheck, a memory error detector ==30889== Copyright (C) 2002-2015, and GNU GPL'd, by Julian

Re: [R] Fwd: R Timeseries tsoutliers:tso

2018-04-13 Thread William Dunlap via R-help
You can record the time to evaluate each line by wrapping each line in a call to system.time(). E.g., expressions <- quote({ # paste your commands here, or put them into a file and use exprs <- parse("thatFile") d.dir <- '/Users/darshanpandya/xx' FNAME <- 'my_data.csv' d.input <- fre

Re: [R] Fwd: Re: Reading xpt files into R

2018-04-14 Thread William Dunlap via R-help
Does read.xport read both version 5 and version 8 xpt files? This link to the Library of Congress can get you started on how to interpret the header. (It states that Version 8 was introduced in 2012 but was not in wide use as of early 2017.) https://www.loc.gov/preservation/digital/formats/fdd/f

Re: [R] Fwd: Re: Reading xpt files into R

2018-04-14 Thread William Dunlap via R-help
> When I look at the SASxport::read.xport function code, it is in fact, _not_ the > same function. But it does have the R statement about what it thinks > qualifies as a SAS xprot file: > > xport.file.header <- "HEADER RECORD***LIBRARY HEADER > RECORD!!!00 " >

Re: [R] How would I color points conditional on their value in a plot of a time series

2018-05-01 Thread William Dunlap via R-help
The ts method for plot() is quirky. You can use the default method: plot(as.vector(time(ttt)), as.vector(ttt), type = "p", col=ifelse(ttt<8, "black", "red")) Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, May 1, 2018 at 1:17 PM, Christopher W Ryan wrote: > How would I color points con

Re: [R] Converting a list to a data frame

2018-05-02 Thread William Dunlap via R-help
> x1 <- do.call(rbind, c(x, list(make.row.names=FALSE))) > x2 <- cbind(type=rep(names(x), vapply(x, nrow, 0)), x1) > str(x2) 'data.frame': 4 obs. of 3 variables: $ type: Factor w/ 2 levels "A","B": 1 1 2 2 $ x : int 1 2 5 6 $ y : int 3 4 7 8 Bill Dunlap TIBCO Software wdunlap tibco.co

Re: [R] Converting a list to a data frame

2018-05-03 Thread William Dunlap via R-help
If you require that the 'type' column be a factor with a level for each element of the input list, then you need to do that after calling dplyr::bind_rows(), just as with the base-R solutions. > l <- list( A = data.frame(X=1:2, Y=11:12), B = data.frame(X=integer(), Y=integer()), C = data.frame(X=3

Re: [R] NAs produced by integer overflow, but only some time ...

2018-05-09 Thread William Dunlap via R-help
Printing a number does not show whether it is stored as a 32-bit integer or as a 64-bit floating point value. Use. e.g., str() or class() to see. > str(length(runif(3))) int 3 > str(length(runif(3)) + 1) num 4 > str(length(runif(3)) + 1L) int 4 > str( 3L * 3L ) int 9 > str( 3

Re: [R] Bilateral matrix

2018-05-16 Thread William Dunlap via R-help
Make current_location and previous_location factors with the same set of levels. The levels could be the union of the values in the two columns or a predetermined list. E.g., > x <- data.frame(previous_location=c("Mount Vernon","Burlington"), current_location=c("Sedro Woolley","Burlington")) > a

Re: [R] Installing an Archived Package

2018-05-16 Thread William Dunlap via R-help
install.packages("/polycor_0.7-8.tar.gz", repos=NULL, type="source") The initial slash would be a problem - it means to find poglycor_0.7.8.tar.gz in the root directory. Remove the slash, find where the tar.gz file was put, and try using that location. Recall the destdir setting you used in

Re: [R] remove rows of a matrix by part of its row name

2018-05-22 Thread William Dunlap via R-help
I think it is simpler to use !grepl() instead of -grep() here, since subscripting with logicals works properly when there are no matches. Also, since mat is a matrix, add the argument drop=FALSE so the result is a matrix when all but one rows are omitted. E.g., > mat <- matrix(1:6, nrow=3, ncol=2

Re: [R] Recoding variables in R

2018-05-23 Thread William Dunlap via R-help
It looks like your data has class "factor". If you call factor() on a factor variable, without supplying an explicit 'levels' argument it produces a new factor variable without any levels not present in the input factor. E.g., > fOrig <- factor(c(NA,"A","B","D",NA,"D"), levels=c("D","C","B","A")

Re: [R] values of list of variable names

2018-06-01 Thread William Dunlap via R-help
One way is values <- lapply(lis, get) You can do names(values) <- lis to attach the object names to the values in the list returned by lapply. Bill Dunlap TIBCO Software wdunlap tibco.com On Fri, Jun 1, 2018 at 7:25 AM, Christian wrote: > Hi, > > I have searched the documentations of eva

Re: [R] shaded area with polygon

2018-06-11 Thread William Dunlap via R-help
Does polygon(c(x,rev(x)), c(y, rev(z)), col="orange") do what you want? Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Jun 11, 2018 at 12:35 PM, L... L... wrote: > Dear All, I know this is a trivial question .. but .. I want to shade the > area between 2 curves. For example: > > x <- 1:

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

2018-06-13 Thread William Dunlap via R-help
Use functions calling functions instead of scripts invoking scripts. Put all your functions in a 'package'. Then your scripts would be very small, just passing command line arguments to R functions. (It is possible to call scripts from scripts, but I think it quickly leads to headaches.) Bill D

Re: [R] How to modify data frame stored in a list

2018-06-18 Thread William Dunlap via R-help
>Thanks! >Why have to add “x” at the end of function, which was what I missed. You can see for yourself with some tests: f1 <- function(x) { x[1] <- 10 } f2 <- function(x) { x[1] <- 10 ; x } print(f1(1:3)) # 10 print(f2(1:3)) # 10 2 3 An assignment returns the value of its right hand side but

Re: [R] Correctly executing system code using R in Ubuntu server

2018-06-25 Thread William Dunlap via R-help
Each call to system() starts and finishes a new shell so your approach will not work. Does the following do what you need? > system('Xvfb :10 -ac &') > Sys.setenv(DISPLAY=":10") > x11() Warning message: In x11() : cairo-based types may only work correctly on TrueColor visuals Bill Dunlap TIBCO

Re: [R] inconsistency in list subsetting in R in linux

2018-06-29 Thread William Dunlap via R-help
> args(parallel::mclapply) function (X, FUN, ..., mc.preschedule = TRUE, mc.set.seed = TRUE, mc.silent = FALSE, mc.cores = 1L, mc.cleanup = TRUE, mc.allow.recursive = TRUE) You gave it 'fun=forecast' instead of 'FUN=forecast'. Case matters in R. Bill Dunlap TIBCO Software wdunlap tibco.com

Re: [R] R maintains old values

2018-07-02 Thread William Dunlap via R-help
Do you have a ".RData" file in your home directory or the current working directory? If so, R will load it at startup. It can be made by you answering 'yes' to the 'Save workspace image?' question when you quit R. Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Jul 2, 2018 at 5:02 AM, Mor

Re: [R] ggplot2 version 3

2018-07-03 Thread William Dunlap via R-help
One way to test the new ggplot2 is to make a new directory to use as an R library and to install the new ggplot2 there. newLibrary <- "C:/tmp/newRLibrary" dir.create(newLibrary) install.packages("ggplot2", lib=newLibrary) Then you can run two R sessions at once, starting one with .libPa

Re: [R] Using write.csv as a connection for read.csv

2018-07-09 Thread William Dunlap via R-help
>I tried something like read.csv(write.csv(df,row.names=FALSE)) but got the error > > Error in read.table(file = file, header = header, sep = sep, quote = quote, : > 'file' must be a character string or connection To diagnose this without reading the help(write.csv) look at the return value of

Re: [R] Making objects global in a package

2018-07-13 Thread William Dunlap via R-help
What the OP is doing looks fine to me. The environment holding the data vectors is not necessary, but it helps organize things - you know where to look for this sort of data vector. I would avoid the *.rda file, since it is not text, hence not readily editable or trackable with most source contro

Re: [R] Locating data source error in large file

2018-07-20 Thread William Dunlap via R-help
The problem occurs because no commonly used format works on all your date strings. If you give as.POSIXlt the format you want to use then items that don't match the format will be treated as NA's. Use is.na() to find them. > d <- c("2017-12-25", "2018-01-01", "10/31/2018") > as.POSIXlt(d) Error i

Re: [R] Locating data source error in large file

2018-07-20 Thread William Dunlap via R-help
Which format did you use when you used is.na on the output of as.POSIXlt(strings, format=someFormat) and found none? Did the resulting dates look OK? Perhaps all is well. Note the the common American format month/day/year is not one that is tested when you don't supply a format - xx/yy/ i

Re: [R] Locating data source error in large file

2018-07-20 Thread William Dunlap via R-help
> And each dataframe row has this format: >2015-10-01,00:00,90.6689 >2015-10-01,01:00,90.6506 >2015-10-01,02:00,90.6719 >2015-10-01,03:00,90.6506 You mean each line in the file, not row in data.frame, has the form "year-month-day,hour:min,numericValue". Try the following, where tfile names your

Re: [R] Locating data source error in large file

2018-07-20 Thread William Dunlap via R-help
To find the lines in the file, tfile, with bogus dates, try readLines(tfile)[ is.na(dataFrame$DateTime) ] Bill Dunlap TIBCO Software wdunlap tibco.com On Fri, Jul 20, 2018 at 1:30 PM, Rich Shepard wrote: > On Fri, 20 Jul 2018, David Winsemius wrote: > > I don't think you read Bill's message

Re: [R] initiate elements in a dataframe with lists

2018-07-25 Thread William Dunlap via R-help
If you need to make a list of long but unknown length you can save time by adding the items to an environment, with names giving the order, then converting the environment to a list when you are done filling the environment. E.g., > makeData function (container, n) { for (i in seq_len(n)) con

Re: [R] how to locate specific line?

2018-07-27 Thread William Dunlap via R-help
If Sys.which("grep") says that grep is available then system("grep -n ...") will do it. > cat(c("One","Two","Three","Four"),sep="\n",file=tf<-tempfile()) > system(paste("grep --line-number", shQuote("^T"), shQuote(tf)), intern=TRUE) [1] "2:Two" "3:Three" > as.integer(sub(":.*$", "", .Last.value)

Re: [R] Taking the sum of only some columns of a data frame

2017-03-31 Thread William Dunlap via R-help
> dat <- data.frame(Group=LETTERS[1:5], X=1:5, Y=11:15) > pos <- c(2,3) > rbind(dat, Sum=lapply(seq_len(ncol(dat)), function(i) if (i %in% pos) > sum(dat[,i]) else NA_real_)) Group X Y 1 A 1 11 2 B 2 12 3 C 3 13 4 D 4 14 5 E 5 15 Sum 15 65 > str(.Last.val

Re: [R] R hangs on startup

2017-04-03 Thread William Dunlap via R-help
Starting R with the --vanilla flag will cause it to ignore startup files. This is usually a quicker way to rule out such issues than tracking down where the startup files are stored. 'R --help' tells about other command line arguments that help home in on which file may be the problem. --no-en

Re: [R] R hangs on startup

2017-04-04 Thread William Dunlap via R-help
Does R work if started with the --vanilla flag? (Add it after ...\Rgui.exe in the Target line of the Windows shortcut or on the command line.) Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Apr 4, 2017 at 1:53 PM, John wrote: > On Mon, 3 Apr 2017 18:06:06 +0100 > "Vineet Gupta" wrote: > >

Re: [R] colorspace namespace problem with R CMD check --as-cran

2017-04-11 Thread William Dunlap via R-help
Does one of the objects in pkg/data (or pkg/R) include a function made by one of the functions in package:colorspace? Such a function would have the environment getNamespace("colorspace"). Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Apr 11, 2017 at 2:47 PM, stephen sefick wrote: > I am

Re: [R] Setting .Rprofile for RStudio on a Windows 7 x64bit

2017-04-15 Thread William Dunlap via R-help
I think the site-specific R profile should be, using R syntax file.path(R.home("etc"), "Rprofile.site") # no dot before the capital R The personal R profile will be file.path(Sys.getenv("HOME"), ".Rprofile") # there is a dot before capital R but if a local R profile, file.path(getwd(), ".R

Re: [R] Setting .Rprofile for RStudio on a Windows 7 x64bit

2017-04-17 Thread William Dunlap via R-help
I believe someone already mentioned it, but notepad makes it hard to save a file without the ".txt" extension to its name. R does not do anything with .Rprofile.txt, only .Rprofile, so you must figure out a way to work around notepad's mangling of the file name. Bill Dunlap TIBCO Software wdunlap

Re: [R] Setting .Rprofile for RStudio on a Windows 7 x64bit

2017-04-17 Thread William Dunlap via R-help
Use the R command dir(c(".", Sys.getenv("HOME"), R.home("etc")), pattern="Rprofile") to see if there are any file names with the unwanted ".txt" and use file.rename() to fix them up. Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Apr 17, 2017 at 8:06 AM, William Dunlap wrote: > I believe

Re: [R] Setting .Rprofile for RStudio on a Windows 7 x64bit

2017-04-17 Thread William Dunlap via R-help
Not file exention ".R" - no file exention at all. Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Apr 17, 2017 at 8:13 AM, BR_email wrote: > Bill: > I did workaround. I created the Rprofile files with file type R, not txt. > Bruce > > > > William Dunlap wrote: >> >> I believe someone alread

Re: [R] Setting .Rprofile for RStudio on a Windows 7 x64bit

2017-04-17 Thread William Dunlap via R-help
I should haved added full.names=TRUE to the dir() call. I you add that I'd expect that you would see ".../etc/Rprofile.site". What do you see when you do writeLines(readLines(".../etc/Rprofile.site") and source(echo=TRUE, ".../etc/Rprofile.site") (replace the ellipsis by whatever dir

Re: [R] Setting .Rprofile for RStudio on a Windows 7 x64bit

2017-04-17 Thread William Dunlap via R-help
Use another editor or go to file explorer, turn on the 'show file extensions' option and rename the file so it does not have the .R extension. Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Apr 17, 2017 at 8:26 AM, BR_email wrote: > I used not extension, but R extension shows up. > > Bruce

Re: [R] Setting .Rprofile for RStudio on a Windows 7 x64bit

2017-04-17 Thread William Dunlap via R-help
No calls please. Just show the group what .../etc/Rprofile-site contained. Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Apr 17, 2017 at 8:36 AM, BR_email wrote: > Bill: > I feel you are nailing it for me. > Can I please call you, but I am getting a little lost, and losing your > valuabl

Re: [R] Return value from function with For loop

2017-04-17 Thread William Dunlap via R-help
A long time ago (before the mid-1990's?) with S or S+ ff <- function(n){ for(i in 1:n) (i+1) op <- ff(3) would result in 'op' being 4, since a for-loop's value was the value of the last expression executed in the body of the loop. The presence of a 'next' or 'break' in the loop body would af

Re: [R] Question on accessing foreign files

2017-04-18 Thread William Dunlap via R-help
I've attached data.restore4.txt, containing the function data.restore4(), which has the same argument list as foreign::data.restore() and is mean to be called by the latter if the first line of the file is "## Dump S Version 4 Dump". It can read version 4 of the 'S data dump' format, which for wh

Re: [R] Interesting quirk with fractions and rounding

2017-04-20 Thread William Dunlap via R-help
Use all.equal(tolerance=0, aa, bb) to check for exact equality: > aa <- 100*(23/40) > bb <- (100*23)/40 > all.equal(aa,bb) [1] TRUE > all.equal(aa,bb,tolerance=0) [1] "Mean relative difference: 1.235726e-16" > aa < bb [1] TRUE The numbers there are rounded to 52 binary dig

Re: [R] R Date Time

2017-04-25 Thread William Dunlap via R-help
> z <- as.POSIXct("01-01-2016T14:02:23.325", format="%d-%m-%YT%H:%M:%OS") > dput(z) structure(1451685743.325, class = c("POSIXct", "POSIXt"), tzone = "") > z [1] "2016-01-01 14:02:23 PST" > format(z, "%H:%M:%OS3 on %b %d, %Y") [1] "14:02:23.325 on Jan 01, 2016" (Don't separate the date and time pa

Re: [R] Different output for "if else" and "ifelse" that I don't understand

2017-04-28 Thread William Dunlap via R-help
ifelse's vectorization messes this up. You could replace your original if (class(try(list(...), silent=TRUE))=="try-error") p3p <- list() else p3p <- list(...) with p3p <- if (class(try(list(...), silent=TRUE))=="try-error") list() else list(...) instead of using ifelse, since the return value

Re: [R] Different output for "if else" and "ifelse" that I don't understand

2017-04-28 Thread William Dunlap via R-help
> > 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 Fri, Apr 28, 2017 at 8:34 AM, William Dunlap v

Re: [R] Example of the use of the "crt" graphical parameter?

2017-05-01 Thread William Dunlap via R-help
Perhaps R does what S+ does with par("crt"). S+'s help(par) says: crt=x character rotation in degrees measured counterclockwise from horizontal. When srt is set, crt is automatically set to the same value, unless crt appears later in the command than srt. Many graphi

Re: [R] Formatting column displays

2017-05-05 Thread William Dunlap via R-help
Use Google: a search for "R Reporter package" shows that the package is named "ReporteRs". Bill Dunlap TIBCO Software wdunlap tibco.com On Fri, May 5, 2017 at 2:50 PM, BR_email wrote: > Jeff: > I cannot install the ReporteR package. Is there a work-around, or is the > error message correct? > T

Re: [R] setting the values of a secondary "y" axis

2017-05-06 Thread William Dunlap via R-help
Does the following do what you want? > plot(log2(1:40), sin(1:40)) > yTickPositions <- axTicks(2) > axis(side=4, at=yTickPositions, lab=format(yTickPositions*3.5)) Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, May 6, 2017 at 2:50 PM, Antonio Silva wrote: > Hello > > I want to make a pl

Re: [R] [FORGED] p-value=0 running log-rank test

2017-05-19 Thread William Dunlap via R-help
This is a minor problem with the print method for "survdiff" objects. It acts like it computes the p-value with 1 - pchisq( chisq, df) instead of pchisq( chisq, df, lower.tail = FALSE) The former will give zero when the latter gives a number less than about 10^-16. Most people would accept

Re: [R] had non-zero exit status

2017-05-19 Thread William Dunlap via R-help
Try using a more recent version of R - 3.4.0 is the current version and 3.1.0 is considered pretty old now. 'R_DoubleColonSymbol' is in RHOME/include/Rinternals.h in R-3.3.0 but not in R-2.15.0. I don't know exactly when it was added. Bill Dunlap TIBCO Software wdunlap tibco.com On Fri, May 19,

Re: [R] Matching values between 2 data.frame.

2017-05-20 Thread William Dunlap via R-help
merge() may be useful here: > merge(OriginalData[1:3], TargetValue, by.x="AA1", by.y="AA", sort=FALSE)[-1] Value1 Value2 BB Value 1 1 11 B 7 2 3 13 B 7 3 11 21 B 7 4 2 12 B25 5 12 22 B25 6 9 19 B25 7 10

Re: [R] R truncating decimal places

2017-05-22 Thread William Dunlap via R-help
Do not compute the log likelihood as the log of the product of probabilities. Instead compute it as the sum of logs of probabilities. The latter is less likely to underflow (go below c. 0^-309). Most (all?) of the built-in probability density functions have a 'log' argument; when log=TRUE you get

Re: [R] Data import R: some explanatory variables not showing up correctly in summary

2017-06-01 Thread William Dunlap via R-help
Check for leading or trailing spaces in the strings in your data. dput(dataset) would show them. Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Jun 1, 2017 at 8:49 AM, Ulrik Stervbo wrote: > Hi Tara, > > It seems that you categorise and count for each category. Could it be that > the met

  1   2   3   4   5   >