Re: [R] Question with uniroot function

2015-04-16 Thread William Dunlap
Use optimize() to find the minimum and feed that value into uniroot(). Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Apr 16, 2015 at 7:47 AM, li li wrote: > Hi Jeff, > Thanks for the reply. I am aware that the sign needs to be different at > the ends of the starting interval. > >An

Re: [R] generate a list as follows: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, . . . . ., n, n, n, n

2015-04-20 Thread William Dunlap
Look more at help(rep) and help(seq): > n <- 7 > rep(seq_len(n), each=4) [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 Bill Dunlap TIBCO Software wdunlap tibco.com On Sun, Apr 19, 2015 at 6:44 AM, John Sorkin wrote: > Windows 7 64-bit > R 3.1.3 > RStudio 0.98.1103 > > >

Re: [R] misbehavior with extract_numeric() from tidyr

2015-04-20 Thread William Dunlap
The hyphen without a following digit confuses tidyr::extract_numeric(). E.g., > extract_numeric("23 ft-lbs") Warning message: In extract_numeric("23 ft-lbs") : NAs introduced by coercion [1] NA > extract_numeric("23 ft*lbs") [1] 23 Contact the BugReports address for the package

Re: [R] How numerical data is stored inside ts time series objects

2015-04-20 Thread William Dunlap
Use the str() function to see the internal structure of most objects. In your case it would show something like: > Data <- data.frame(theData=round(sin(1:38),1)) > x <- ts(Data[[1]], frequency=12) # or Data[,1] > y <- ts(Data, frequency=12) > str(x) Time-Series [1:38] from 1 to 4.08: 0.8 0.9 0.1

Re: [R] How numerical data is stored inside ts time series objects

2015-04-21 Thread William Dunlap
> Interesting that a 2D matrix of size Nx1 is treated as a different > animal from a length N vector. I think we can call this a bug in stl(). Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Apr 21, 2015 at 6:39 PM, Paul wrote: > William Dunlap tibco.com> writes: >

Re: [R] Predictions on training set shorter than training set

2015-04-23 Thread William Dunlap
Are there missing values in your data? If so, try adding the argument na.action = na.exclude to your original call to glm or lm. It is like the default na.omit except that it records which rows were omitted (because they contained missing values) and fills in the corresponding entries in the p

Re: [R] cbind question, please

2015-04-24 Thread William Dunlap
You could do something tricky like > do.call(cbind, lapply(big.char, as.name)) dog cat tree [1,] 1 25 [2,] 2 36 [3,] 3 47 but you are usually better off creating these things as part of a list and passing that to do.call(cbind, list). There is a slight danger

Re: [R] invalid function value in 'nlm' optimizer

2015-04-28 Thread William Dunlap
Your function ln() does not return a scalar. > ln(theta=c(1,2)) [1] 48.5342640972 48.5342640972 48.5342640972 48.5342640972 48.5342640972 Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Apr 28, 2015 at 6:40 PM, Hanze Zhang wrote: > I still cannot solve the problem: 'invalid functio

Re: [R] R Error: wrong result size (...), expected ... or 1 (minimal example provided)

2015-05-02 Thread William Dunlap
# invoke user defined function myFunc ( newdata=nmpr_nmproid, oridata=xanloid_set, cohort=xanloid_set$cohort_type, value= "NMPR_Cohort", xdate_to_int_time=anl_to_int_time, xflag=xanloid_set$anlflag, idate=xanloid_set$intdate,

Re: [R] order by which the eigenvalues are presented

2015-05-05 Thread William Dunlap
You can sort the eigenvalues in the order you want with o <- order(Re(e$values), decreasing = TRUE) or o <- order(abs(Re(e$values)), decreasing = TRUE) followed by e$values[o] where 'e' is the object that eigen returns. The main argument to order() is what you want to sort by and the s

Re: [R] does segfault mean (always) a bug?

2015-05-06 Thread William Dunlap
It looks like a problem in the Matrix package. I made the file KE.rda containing the Matrix objects K and E constructed in calc.diffusion.kernel by adding a save() call just before where R dies in the original example: K = lam$values[1] * I - M E = I - matrix(1, ncol = ncol(I), nr

Re: [R] nlminb supplying NaN parameters to objective function

2015-05-07 Thread William Dunlap
Your nLL function returns 1e+308 in near-boundary cases. Since 1e+308 is so close to machine infinity, it is easy to get into Inf-Inf (=NaN) or Inf/Inf (=NaN) situations when working with it. Try making that limiting value something smaller, like 1e+30, and you may have better luck. Bill Dunlap

Re: [R] nlminb supplying NaN parameters to objective function

2015-05-07 Thread William Dunlap
Software wdunlap tibco.com On Thu, May 7, 2015 at 4:46 PM, Jean Marchal wrote: > Yes, indeed! Problem solved! > > Thanks a lot! > > Jean > > 2015-05-07 14:06 GMT-07:00 William Dunlap : > > Your nLL function returns 1e+308 in near-boundary cases. Since 1e+308 > is so

Re: [R] Error importing data - wrapping?

2015-05-09 Thread William Dunlap
txt <- c("A B C D E", "A 1232 0.565", "B 2323 0.5656 0.5656 0.5656", "C 2323 0.5656", "D 2323 0.5656", "E 2323 0.5656", "F 2323 0.5656", "G 2323 0.5656", "G 2323 0.5656 0.5656 0.5656") z <- read.table(text=txt, fill=TRUE, header=TRUE) str(z) #'data.frame': 8 obs. of 5 variables: # $ A: Factor

Re: [R] how to update a value in a list with lapply

2015-05-10 Thread William Dunlap
You can do the timing yourself on a dataset which you feel is typical of your usage. E.g., define a function the implements each algorithm > f1 <- function(foo) lapply(foo, function(x) { if (x[1] == 1) x[2] <- 0 ; x }) > f2 <- function(foo) { for(i in seq_along(foo)) if (foo[[i]][1] == 1) foo[[

Re: [R] Deparse substitute assign with list elements

2015-05-14 Thread William Dunlap
You could use a 'replacement function' named 'bar<-', whose last argument is called 'value', and use bar(variable) <- newValue where you currently use foo(variable, newValue). bar <- function(x) { x + 3 } `bar<-` <- function(x, value) { bar(value) } a <- NA bar(a) <- 4 a # [1] 7 b <- list

Re: [R] Determinant

2015-05-14 Thread William Dunlap
Note that when using double precision arithmetic you cannot tell if that number is different from other numbers within 10 billion of it -- they all have the same representation. > 1.677721599849e+29 == 1.677721599849e+29 + 1e10 [1] TRUE You need to use other, time consuming, methods (e.g.

Re: [R] Deparse substitute assign with list elements

2015-05-15 Thread William Dunlap
eval( parse(text=txt), parent.frame() ) > > in `foo()` will do the trick. > > Bests > Sören > > > On 15.05.2015, at 00:02, William Dunlap wrote: > > > > You could use a 'replacement function' named 'bar<-', whose last argument >

Re: [R] print dataframe names in loop

2015-05-15 Thread William Dunlap
You can automate the adding of the names to the list with the following function, so you can replace the dflist<-list(df1,df2,df3) names(dflist)<-c("df1","df2","df3") with dflist <- namedList(df1, df2, df3) If you supply names, such in dflist <- namedList(df1, Second=df2, log(df3)) it will

Re: [R] subsetting question

2015-05-20 Thread William Dunlap
Here is a self-contained example of what you might be trying to do. You would get better answers if you supplied this yourself. dataset1 <- data.frame(Date=as.POSIXct(c("2015-04-01","2015-04-01","2015-04-07", "2015-04-19")), Weight=11:14) datums <- as.POSIXct(c("2015-04-01", "2015-04-08", "2015-04

Re: [R] Subset and 0 replace?

2015-05-20 Thread William Dunlap
Can you show a small self-contained example of you data and expected results? I tried to make one and your expression returned a single number in a 1 by 1 matrix. library(doBy) Generation<-list( data.frame(Wgt=c(1,2,4), SPCLORatingValue=c(10,11,12)), data.frame(Wgt=c(8,16), SPCLORatingValue=

Re: [R] Feature or bug?

2015-05-21 Thread William Dunlap
If you add a print statement to trace the evaluation of the input argument you can see how the lazy evaluation works: > update <- function (newtime) { cat("# update() is changing global ginput's time from", ginput$time, "to", newtime, "\n") ginput <<- list(time = newtime) } > server <-

Re: [R] Subset and 0 replace?

2015-05-21 Thread William Dunlap
I renamed your 'c' to be 'toyData' and your 'e' to be 'desiredResult'. Do you want the following, which uses only base R code? > vapply(toyData, FUN=function(V)with(V, sum(Wgt[SPCLORatingValue>16])), FUN.VALUE=0) V5 V8 V10 V44

Re: [R] How to pass a variable to a function which use variable name as a parameter

2015-05-26 Thread William Dunlap
One way to use variable names in functions like Predict() that do not evaluate their arguments in the standard way is to use do.call() along with as.name(). E.g., varName<-"age" do.call("Predict", list(fit, as.name(varName), np=4))}) gives the same result as Predict(fit, age, np=4) Bill D

Re: [R] Converting unique strings to unique numbers

2015-05-29 Thread William Dunlap
match() will do what you want. E.g., run your data through the following function. f <- function (data) { uniqStrings <- unique(c(data[, 2], data[, 3], data[, 4])) uniqStrings <- setdiff(uniqStrings, "0") for (j in 2:4) { data[[j]] <- match(data[[j]], uniqStrings, nomatch = 0L

Re: [R] Converting unique strings to unique numbers

2015-05-29 Thread William Dunlap
x) where 'x' contains the strings from all the columns > that you want to replace: > > m <- matrix(c( > "X0001", "BYX859",0,0, 2, 1, "BYX859", > "X0001", "BYX894",0,0, 1, 1, &

Re: [R] Arrays of variable dimensionality

2015-05-30 Thread William Dunlap
I don't know for sure what result you want, but it may be that using a length(dim(array))-column matrix as your subscript will do what you want. E.g., > a <- array(101:124, dim=4:2) > subscriptMatrix <- cbind(c(1,3), c(2,2), c(2,1)) > subscriptMatrix [,1] [,2] [,3] [1,]12

Re: [R] puzzling behaviour of identical function

2015-05-30 Thread William Dunlap
Their 'srcref' attributes differ. srcref describes the text that the parser was given when creating the function. > identical(f1 <- function(x)1, f2 <- function(x)1) [1] FALSE > str(f1) function (x) - attr(*, "srcref")=Class 'srcref' atomic [1:8] 1 17 1 28 17 28 1 1 .. ..- attr(*, "srcfile")=

Re: [R] Colour gradient is not working.

2015-06-02 Thread William Dunlap
Points later in the input vectors may be obscuring earlier points. If that is the problem then use pch="." or cex=.2 (or some other small number) to make the plot symbols smaller so they don't overlap as much. Sometimes using transparency helps also - try using adjustcolor(Color(n), alpha.f=0.5)

Re: [R] Greek letters with subscript numbers on x-axis

2015-06-03 Thread William Dunlap
You can use labels=as.expression(lapply(1:9, function(i)bquote(hat(lambda)[.(i)]))) Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Jun 3, 2015 at 12:39 PM, Frank S. wrote: > Hi everyone, I have allocated greek letters with subscript numbers on > x-axis of my R plot this way: > plot(10

Re: [R] building a list in a loop

2015-06-05 Thread William Dunlap
Does the following do what you want? > d <- list() # empty list > for(i in 1:8) if (i %% 2 == 0) { newElement <- structure(list(i), names=LETTERS[i]) d <- c(d, newElement) } > str(d) List of 4 $ B: int 2 $ D: int 4 $ F: int 6 $ H: int 8 Bill Dunlap TIBCO Software wdunlap tibco.c

Re: [R] if else statement for rain data to define zero for dry and one to wet

2015-06-06 Thread William Dunlap
Your f1() has an unneeded for loop in it. f1a <- function(mat) mat > 0.1, 1, 0) would do the same thing in a bit less time. However, I think that a simple mat > 0.1 would be preferable. The resulting TRUEs and FALSEs are easier to interpret than the 1s and 0s that f1a() produces and arithme

Re: [R] Blank spaces are replaced by period in read.csv, I want to replace blacks with an underline

2015-06-08 Thread William Dunlap
> mynames [1] "x.y" "x.y" "x.y" "x.y" > mynames <- paste(mynames, seq_along(mynames), sep="_") In addition, if there were a variety of names in mynames and you wanted to number each unique name separately you could use ave(): > origNames <- c("X", "Y", "Y", "X", "Z", "X") > ave(origNames

Re: [R] subsetting a dataframe

2015-06-08 Thread William Dunlap
Use is.element(elements,set), or its equivalent, elements %in% set: df <- data.frame(dd = c(1, 2, 3), rows = c("A1", "A2", "A3"), columns = c("B1", "B2", "B3"), numbers = c(400, 500, 600)) test_rows <-c("A1","A3") df[ is.element(df$rows, test_rows

Re: [R] Looping Through List of .csv Files to Work with Subsets of the Data

2015-06-08 Thread William Dunlap
participant.files <- list.files("/Users/cdanyluck/Documents/Studies/MIG - Dissertation/Data & Syntax/MIG_RAW DATA & TXT Files/Plain Text Files") Try adding the argument full.names=TRUE to that call to list.files(). Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Jun 8, 2015 at 7:15 PM, C

Re: [R] Unordered combinations with repetition

2015-06-09 Thread William Dunlap
> combnWithRepetition <- function(n, k) combn(n+k-1, k) - seq(from=0, len=k) > combnWithRepetition(2, 2) [,1] [,2] [,3] [1,]112 [2,]122 > combnWithRepetition(3, 2) [,1] [,2] [,3] [,4] [,5] [,6] [1,]111223 [2,]123233

Re: [R] Unordered combinations with repetition

2015-06-09 Thread William Dunlap
. Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Jun 9, 2015 at 8:56 AM, William Dunlap wrote: > > combnWithRepetition <- function(n, k) combn(n+k-1, k) - seq(from=0, > len=k) > > combnWithRepetition(2, 2) > [,1] [,2] [,3] > [1,]11

Re: [R] Read text file

2015-06-17 Thread William Dunlap
I am not sure what you mean by "a block", but you can probably use scan() instead of read.table(): > txt <- "1 21 41 61 81" > d <- data.frame(Numbers=scan(text=txt, what="numeric", quiet=TRUE)) > d Numbers 1 1 2 21 3 41 4 61 5 81 Bill Dunlap TIBCO Sof

Re: [R] R problem

2015-06-18 Thread William Dunlap
Usually, along with that error message, you get a warning that gives a few details about the problem, like the name of the file and the reason it could be opened. E.g., > filename <- "no\\such\\file.blah.blah" > file(filename, "r", encoding="UTF-8") Error in file(filename, "r", encoding = "U

Re: [R] what constitutes a 'complete sentence'?

2015-07-06 Thread William Dunlap
> The CRAN guidelines should be rewritten so that they say what they *mean*. > If a complete sentence is not actually required --- and it seems abundantly clear > that it is not --- then guidelines should not say so. Rather they should say, > clearly and comprehensibly, what actually *is* required

Re: [R] problem with using seq() or rep() inside a for loop

2015-07-07 Thread William Dunlap
for (i in 1:(ncol(MD_dist) - 1)){ ... } Even better, replace 1:(n-1) with seq_len(n-1) The latter does what you want (and empty integer vector) when n is 1; the former would give c(1,0). Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Jul 7, 2015 at 2:11 PM, David Barron wr

Re: [R] How to assign value to a variable dynamically constructed

2015-07-08 Thread William Dunlap
You can use an environment instead of a list using the same [[ syntax. It is like 'get0(..., inherit=FALSE)' on the left side of the <- and like 'assign(...)' on the right side. E.g., myData <- new.env() varName <- "v1" myData[[varName]] <- 1:10 myData[[varName]][4] <- myData[[varNam

Re: [R] ggmap warning

2015-07-09 Thread William Dunlap
Look at your data.frame with str() and see if the variables you think are numeric are actually factors. is.finite(factor()) reports TRUE but lots of functions expecting numeric data will abort when given factor data. E.g. library(ggplot2) > d <- data.frame(X=factor(round(sin(1:1000),1)), Y=factor

Re: [R] How to assign value to a variable dynamically constructed

2015-07-10 Thread William Dunlap
't assign() do the trick? Most > similar threads seem to include this approach (among others, indeed). > > Regards, > Bastien > > > On Wednesday, July 8, 2015 at 7:30:04 PM UTC+2, William Dunlap wrote: > > You can use an environment instead of a list using the same [[

Re: [R] error message in package "FD", function dbFD ()

2015-07-15 Thread William Dunlap
Try doing rownames(x) <- x["Species"] before running dbFD. The function is probably using the row and column names of its inputs as species names, not a certain row or column of the data. Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Jul 15, 2015 at 6:47 AM, lauraRIG wrote: > Dear R

Re: [R] User defined function within a formula

2015-07-16 Thread William Dunlap
Read about the 'makepredictcall' generic function. There is a method, makepredictcall.poly(), for poly() that attaches the polynomial coefficients used during the fitting procedure to the call to poly() that predict() makes. You ought to supply a similar method for your xpoly(), and xpoly() needs

Re: [R] User defined function within a formula

2015-07-16 Thread William Dunlap
~ log(u) + OPoly(u,1), data = clotting, family = Gamma) > > > predict(g3,dc)Error in poly(4 * (rep(x, weight) - > > mean(range(x)))/diff(range(x)), degree) : > missing values are not allowed in 'poly' > > I thought it might be due to the /diff(range(x) in the func

Re: [R] User defined function within a formula

2015-07-16 Thread William Dunlap
fit)[c(3,8)] predict(fit, newdata=data.frame(X=d$X[c(3,8)])) # same result Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Jul 16, 2015 at 4:39 PM, William Dunlap wrote: > OPoly<-function(x,degree=1,weight=1){ > weight=round(weight,0)# weight need to be integer > if(leng

Re: [R] shift by one column given rows in a dataframe

2015-07-23 Thread William Dunlap
You could do something like the following > rowsToShiftLeft <- c(2,4,5) # 4, not the 3 that was in the original post > mat <- as.matrix(df_start) > mat[rowsToShiftLeft, 1:3] <- mat[rowsToShiftLeft, 2:4] > result <- data.frame(mat[, 1:3], stringsAsFactors=FALSE) > str(result) 'data.frame

Re: [R] setwd() command on windows vs. linux/unix

2015-07-27 Thread William Dunlap
You can get a more informative error message from system("bash -c 'cd yourDirectory'"), although it will not take R to that directory if there are no problems. E.g., I did in a shell % mkdir -p dir/subdir % chmod -x dir to make an untraversable directory 'dir' and a subdirectory of it. Then

Re: [R] write.table with append=T after using cat on same file

2015-07-27 Thread William Dunlap
This will happen if you have redefined 'T': > T <- 101:104 > write.table(df, file="junk.txt", sep=",", append=T, quote=F, row.names=F, col.names=F) Error in file(file, ifelse(append, "a", "w")) : invalid 'open' argument Use 'TRUE' and 'FALSE' instead of 'T' and 'F' this sort of problem. Bil

Re: [R] write.table with append=T after using cat on same file

2015-07-27 Thread William Dunlap
Missing words: Use 'TRUE' and 'FALSE' instead of 'T' and 'F' >to avoid< this sort of problem Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Jul 27, 2015 at 1:50 PM, William Dunlap wrote: > This will happen if you have redefined &

Re: [R] R parallel / foreach - aggregation of results

2015-08-01 Thread William Dunlap
If you return just the row that the foreach procedure produces instead of the entire matrix containing that row and use .combine=rbind then you will end up with the matrix of interest. E.g., Simpar3a <- function (n1) { L2distance <- matrix(NA, ncol = n1, nrow = n1) data <- rnorm(n1) d

Re: [R] Environmental Data Connector v1.3

2015-08-03 Thread William Dunlap
> During installation EDC_HOME was set to /home/robert/EDC > and the directory definitely exists. Are you sure that EDC_HOME is set now? What do you get from the following command? Sys.getenv("EDC_HOME") If that is set to something other than "", what do you get from getwd() setwd(Sys.ge

Re: [R] aggravating aggregate

2015-08-05 Thread William Dunlap
# your data, from dump("xxx", file=stdout()) xxx <- structure(list(Registered = c(1327, 2129, 10, 433, 5, 166, 1784, 17, 787), GA_Total = c(127, 150, 0, 32, 1, 76, 153, 7, 31), PCT = c("1120", "1121", "1121", "1122", "1124", "1125", "1125", "1125", "1126" )), .Names = c("Registered", "GA_Total", "P

Re: [R] fpc package - computing silhouette value returns error

2015-08-07 Thread William Dunlap
help(distcritmulti) says that its 2nd argument is "clustering: vector of integers indicating the clustering" and you supplied the output of hclust, which is not a vector of group identifiers. You can make a group identifier vector from hc with cutree(hc, k=4), where k is the number of groups.

Re: [R] result NA , but expected True or False

2015-08-10 Thread William Dunlap
The || operator will always return a result of type 'logical' and length 1. You gave it two operands of length 0, so it returned the logical value NA, meaning it had no idea what the result should be. If you give it operands of length > 1, it will use the only the first elements of them. (S and S

Re: [R] Problem with path.expand("~")

2015-08-11 Thread William Dunlap
In the R GUI the output is > path.expand("~") [1] "~" Did you set the environment variable R_USER to something odd like "~"? Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Aug 11, 2015 at 7:41 AM, Thierry Onkelinx wrote: > Dear all, > > I'm puzzled by the behaviour of path.ex

Re: [R] FW: R Statistics

2014-12-02 Thread William Dunlap
You can do this in 2 steps - have cut() make a factor with a different level for each time period then use levels<-() to merge some of the levels. > z <- cut(.5:3.5, breaks=c(0,1,2,3,4), labels=c("0-1", "1-2", "2-3", "3-4")) > levels(z) [1] "0-1" "1-2" "2-3" "3-4" > levels(z) <- c("betw

Re: [R] How to have NLME return when convergence not reached

2014-12-02 Thread William Dunlap
You could try using R.utils::withTimeout(expr, timeout=10) to cause an error when evaluating the expression expr takes longer than 10 seconds. Wrap that in tryCatch or try to catch the error and examine the outputs of lapply for ones of class "TimeoutException" to find the ones that took too long.

Re: [R] recoding genetic information using gsub

2014-12-05 Thread William Dunlap
Does the following do what you want? > raw <- c("A/B", " /B", "A/", "/ ") > tmp <- sub("^ */", "./", raw) > cleaned <- sub("/ *$", "/.", tmp) > cleaned [1] "A/B" "./B" "A/." "./." (The " *" is to allow optional spaces before or after the slash.) Bill Dunlap TIBCO Software wdunlap tibco.com On F

Re: [R] backslash quirk in paste

2014-12-06 Thread William Dunlap
"\\" is stored as a single backslash, just as "\n" is a single newline character. It is printed with an extra backslash. > nchar("\\") [1] 1 > cat(paste0("\\", "\n")) \ Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Dec 6, 2014 at 11:00 AM, Prof J C Nash (U30A) wrote: > This i

Re: [R] String manipulation

2014-12-08 Thread William Dunlap
strsplit(split=":") does almost what you want, but it omits the colons from the output. You can use perl zero-length look-ahead and look-behind operators in the split argument to get the colons as well: > strsplit(c(":sad", "happy:", "happy:sad"), split="(?<=:)|(?=:)", perl=TRUE) [[1]] [1] ":"

Re: [R] String manipulation

2014-12-08 Thread William Dunlap
":" [[3]] [1] "happy" ":" "sad" [[4]] [1] ":" "happy" ":" "sad" ":" "subdued" ":" Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Dec 8, 2014 at 1:1

Re: [R] Merging two data.frames

2014-12-08 Thread William Dunlap
Have you looked at the merge() function? Here is an example. I don't know if it resembles your problem. > M1 <- data.frame(V1=letters[1:3], V2=LETTERS[26:24], N1=101:103) > M2 <- data.frame(V1=letters[c(3,1,2,3,2)], V2=LETTERS[c(23,26,22,24,24)], N2=c(1003,1001,1002,1003,1002)) > merge(M

Re: [R] Working with data frames

2014-12-11 Thread William Dunlap
Here is a reproducible example > d <- read.csv(text="Name,Age\nBob,2\nXavier,25\nAdam,1") > str(d) 'data.frame': 3 obs. of 2 variables: $ Name: Factor w/ 3 levels "Adam","Bob","Xavier": 2 3 1 $ Age : int 2 25 1 Do you get something similar? If not, show us what you have (you could

Re: [R] Working with data frames

2014-12-11 Thread William Dunlap
s is fundamentally > sound and something I will need to get my head around. For now though, I > think I'll stick to exploring ggplot2 so that I can visualise this data set > more easily. > > Thanks again. > > Best > > Sun > > > On 11/12/14 16:06, William Dunla

Re: [R] list of lists, is this element empty

2014-12-21 Thread William Dunlap
Your 'x' has length 2, so x[[3]] cannot be calculated ('subscript out of bounds' is what I get). You can check for this with length(x)<3. In general, you want to be more precise: 'does not have a value', 'is NULL', and 'is empty' are not synonymous. I'm not sure what 'does not have a value' mean

Re: [R] number of individuals where X=0 during all periods (longitudinal data)

2014-12-22 Thread William Dunlap
Another approach to to make a table and extract your summaries from the table: > tbl <- with(Test, table(ID, IsZero=X==0)) > tbl IsZero ID FALSE TRUE 1 31 2 02 3 03 > sum(tbl[,"FALSE"] == 0) [1] 2 > sum(tbl[,"FALSE"] == 0 & rowSums(tbl)>=3) [

Re: [R] Carrying a value down a data.frame conditionally

2014-12-24 Thread William Dunlap
A while ago I wrote for a questioner on this list a function, 'f1', below, that would give the start and stop times of runs of data that started when then the data went above a threshold and stopped when it first dropped below a different (lower) threshold). It used no loops and was pretty quick.

Re: [R] ave(x, y, FUN=length) produces character output when x is character

2014-12-24 Thread William Dunlap
> ave( as.character(1:5), gl(2,2,5), FUN=length ) [1] "3" "3" "2" "2" "3"The output has character type, but it is supposed to be a collection of vector lengths. ave() uses its first argument, 'x', to set the length of its output and to make an initial guess at the type of its output. The re

Re: [R] attribute and main value

2014-12-29 Thread William Dunlap
as.vector(x) will return x without any attributes and structure(x, attrA=NULL, attrB=NULL) will return x without the named attributes. > z <- f(1:3, 4) > z [1] 14 attr(,"gradient") [1] -6 -4 -2 > as.vector(z) [1] 14 > structure(z, gradient=NULL) [1] 14 as.vector is a gen

Re: [R] Fwd: which is faster "for" or "apply"

2014-12-31 Thread William Dunlap
> But this heterogeneity comes even with only supposed numeric data.frame > (gene expression). here an example > > ibrary(cgdsr) > GeneList <- c("DDR2", "HPGDS", "MS4A2","SSUH2","MLH1" ,"MSH2", "ATM" > ,"ATR", "MDC1" ,"PARP1") > cgds<-CGDS("http://www.cbioportal.org/public-portal/";) > > str(getPr

Re: [R] Scientific Numbers in Boxplot Label

2015-01-02 Thread William Dunlap
You can use options(scipen=n) to control when numbers are printed using scientific notation (the default value is 0). Compare options(scipen=0); plot( 1e-17*(1:10), 1e5*(1:10)) options(scipen=10); plot( 1e-17*(1:10), 1e5*(1:10)) options(scipen=20); plot( 1e-17*(1:10), 1e5*(1:10)) Bill Dunlap

Re: [R] Separating a Complicated String Vector

2015-01-04 Thread William Dunlap
f <- function (x) { isState <- is.element(tolower(x), tolower(state.name)) w <- which(isState) data.frame(State = x[rep(w, diff(c(w, length(x) + 1)) - 1L)], City = x[!isState]) } E.g., V1 <-c("alabama", "bates", "tuscaloosa", "smith", "arkansas", "fayette", "little rock", "alas

Re: [R] Color problem for 2 simple graphs

2015-01-08 Thread William Dunlap
Supply a vector of colors. E.g., barplot(table(mtcars$cyl), col=c("red","yellow","green")) boxplot(with(mtcars, split(wt, cyl)), col=c("red","yellow","green")) Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Jan 8, 2015 at 12:06 PM, varin sacha wrote: > Hi R-experts, > Basic question

Re: [R] how to sort a column with numbers and characters

2015-01-11 Thread William Dunlap
Change that column to be a factor with the levels in the order that you wish. dataFrame$column <- factor(dataFrame$column, levels=c(1:150, "S", "Z")) Then it will sort in that order. E.g., > d <- data.frame(One=c(5,21,10,"Z","S",9), Two=2^(1:6)) > d[order(d$One),] One Two 3 10 8

Re: [R] any r package can handle factor levels not in the test set

2015-01-13 Thread William Dunlap
I think it would be nice if predict methods returned NA in appropriate spots instead of aborting when a categorical predictor contains levels not found in the training set. It should not be that hard to implement, as the 'xlevels' component of the model is already being used to put factor levels i

Re: [R] Iteratively subsetting data by factor level across multiple variables

2015-01-15 Thread William Dunlap
There are lots of ways to do this. You have to decide on how you want to organize the results. Here are two ways that use only core R packages. Many people like the plyr package for this split-data/analyze-parts/combine-results sort of thing. > df <- data.frame(x=1:27,response=log2(1:27),

Re: [R] Re-order levels of a categorical (factor) variable

2015-01-21 Thread William Dunlap
Are you sure the factors of T are in the order you think they are? (Are you sure you are using the expected version of T.) Use print(levels(T)) to make sure. I tried timeCats <- c("Presurgery", "Day 30", "Day 60", "Day 180", "Day 365") d <- data.frame(T = factor(rep(timeCats, 11:15), lev

Re: [R] need help with excel data

2015-01-22 Thread William Dunlap
The following is one way to parse your file using R (using R-3.1.2 on Windows in a US English locale). I downloaded it from Google Docs in tab-separated format. I could not get read.table() to do the job, but I don't completely understand the encoding/fileEncoding business there. > file <- "examp

Re: [R] 'which' statement for recode?

2015-01-22 Thread William Dunlap
You did not show what answer you expected, but does the following do what you want? > match(sc$TERM, TermList) [1] 11 20 30 Making a factor whose levels are TermList may also be useful. (The exclude=NULL is to factor doesn't drop NA from the levels). > sc$fTERM <- factor(sc$fTERM, levels=T

Re: [R] get latest dates for different people in a dataset

2015-01-23 Thread William Dunlap
Here is one way. Sort the data.frame, first by Name then break ties with CheckInDate. Then choose the rows that are the last in a run of identical Name values. > txt <- "NameCheckInDate Temp + John 1/3/2014 97 + Mary 1/3/2014 98.1 + Sam 1/4/2014

Re: [R] get latest dates for different people in a dataset

2015-01-25 Thread William Dunlap
last (as ordered in the original file). Bill Dunlap TIBCO Software wdunlap tibco.com On Sun, Jan 25, 2015 at 1:01 AM, Göran Broström wrote: > On 2015-01-24 01:14, William Dunlap wrote: > >> Here is one way. Sort the data.frame, first by Name then break ties with >> CheckIn

Re: [R] adding an additional column for preserving uniqueness

2015-01-28 Thread William Dunlap
> with(dat1, ave(integer(length(Date)), Date, FUN=seq_along)) [1] 1 1 2 1 1 2 1 2 1 2 1 Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Jan 28, 2015 at 4:54 PM, Morway, Eric wrote: > The two datasets below are excerpts from much larger datasets. Note that > there are duplicate dates in

Re: [R] How to use curve() function without using x as the variable name inside expression?

2015-01-29 Thread William Dunlap
Does help(curve) talk about its 'xname' argument? Try curve(10*foofoo, from=0, to=17, xname="foofoo") You will have to modify your function, since curve() will call it once with a long vector for the independent variable and func(rnorm(10), rnorm(10), mu=seq(0,5,len=501)) won't work right.

Re: [R] lost in plotmath expressions

2015-01-30 Thread William Dunlap
You successfully used an 'expression' object in your first example plot(1,1,xlab=expression(paste("D50 [", mu,"m]"))) so use a vector of expressions in your second example nams<-c(expression(P[205]~"[%]"), expression(paste("D50 [", mu, "m]"))) par(mfrow=c(2,1)) for (i in 1:2) plot(1, 1,

Re: [R] Dropping time series observations

2015-01-31 Thread William Dunlap
For class 'ts' the 'window' function in the base package will do it. > x <- ts(101:117, start=2001.75, frequency=4) > x Qtr1 Qtr2 Qtr3 Qtr4 2001 101 2002 102 103 104 105 2003 106 107 108 109 2004 110 111 112 113 2005 114 115 116 117 > window(x, start=2002.5)

Re: [R] Transform a list of multiple to a data.frame which I want

2015-02-02 Thread William Dunlap
Does the following work for you. The only trick is working around the fact that matrix subscripting does not allow out-of-bounds subscripts but vector subscripting does. We do the subscripting in two steps, relying on the drop=TRUE default in the matrix subscripting operator that converts the sin

Re: [R] rtools fstream error

2015-02-02 Thread William Dunlap
Try adding the line #define R_NO_REMAP to the top of your file (before any #include's) so the R include files don't define length(x). Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Feb 2, 2015 at 1:11 PM, wrote: > Hi, > > when writing a simple cpp function to be run in R I obtain a comp

Re: [R] collapse a list of dataframes

2015-02-03 Thread William Dunlap
I don't think that worked as OP would like it too - all columns of the output are factors. > data.frame(rbind(as.matrix(data.frame(a=1:3,b=letters[1:3])),as.matrix(data.frame(x=1:5,b=LETTERS[1:5] a b 1 1 a 2 2 b 3 3 c 4 1 A 5 2 B 6 3 C 7 4 D 8 5 E > str(.Last.value) 'data.frame': 8 obs. o

Re: [R] Still trying to avoid loops

2015-02-04 Thread William Dunlap
A useful technique when it is easy to compute a vector from an ordered data.frame but you need to do it for an unordered one is to compute the order vector 'ord', compute the vector from df[ord,], and use df[ord,...] <- vector to reorder the vector. In your case you could do: > dat_2<-data.frame

Re: [R] Still trying to avoid loops

2015-02-04 Thread William Dunlap
A potential problem with ave(dat_2$D, dat_2$S, FUN=order) is that it will silently give the wrong answer or give an error if dat_2$D is not numeric. E.g., if D is a Date vector we get > dat_3 <- dat_2[,1:2] > dat_3$D <- as.Date(paste0("2015-02-", dat_2$D)) > with(dat_3, ave(D, S, FUN=orde

Re: [R] Unable to use `eval(parse(text))' in nlme::lme

2015-02-13 Thread William Dunlap
> ff <- reformulate(termlabels=c("time","as.factor(gvhd)"), response=yname, intercept=TRUE) If the right hand side of the formula were more complicated than an additive list of terms, say '~ time * as.factor(gvhd)' or the left side were more than a name, say 'log(yname)' you could use bquote() ins

Re: [R] list of list

2015-02-16 Thread William Dunlap
You can let lapply() do the preallocation and the looping for you with ASL <- lapply(1:5, function(j) lapply(1:5, function(i) i^j)) Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Feb 16, 2015 at 9:46 AM, Jeff Newmiller wrote: > You have two named objects when your goal is to have one

Re: [R] Help with declaring factors in a function

2015-02-18 Thread William Dunlap
You did not put the altered columns back into the data.frame, so glm() never saw them. Does the following work? func <- function(x,y,z) { #x is a data frame #y is a formula for the regression #z vector of names of columns of x to convert to factors for (name in z) { x[[name]] <- facto

Re: [R] Subsetting a list of lists using lapply

2015-02-20 Thread William Dunlap
The elNamed(x, name) function can simplify this code a bit. The following gives the same result as David W's get_shas() for the sample dataset provided: get_shas2 <- function (input) { lapply(input, function(el) elNamed(elNamed(el, "content")[[1]], "sha")[1]) } Bill Dunlap TIBCO Sof

Re: [R] How do I access a specific element of a multi-dimensional list

2015-02-20 Thread William Dunlap
Using lapply() where Jim used sapply() would keep the types right and be a fair bit faster than a solution based on repeatedly appending to a list (like your getFirst). Bill Dunlap TIBCO Software wdunlap tibco.com On Fri, Feb 20, 2015 at 1:52 PM, JS Huang wrote: > Hi, > > Jim's answer is neat

Re: [R] aggregating variables (sum within groups)

2015-02-26 Thread William Dunlap
> Even though I was looking in several r-books > I could not find a suitable function to this problem Which R books did you look through? Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Feb 26, 2015 at 4:02 AM, David Studer wrote: > Hello everybody! > > I have a (probabely very easy) prob

Re: [R] Why does R replace all row values with NAs

2015-02-27 Thread William Dunlap
You could define functions like is.true <- function(x) !is.na(x) & x is.false <- function(x) !is.na(x) & !x and use them in your selections. E.g., > x <- data.frame(a=1:10,b=2:11,c=c(1,NA,3,NA,5,NA,7,NA,NA,10)) > x[is.true(x$c >= 6), ] a b c 7 7 8 7 10 10 11 10 Bill Dun

  1   2   3   4   5   6   7   8   9   10   >