Re: [R] Get means of matrix

2015-11-18 Thread Dennis Murphy
Hi: Here's another way to look at the problem. Instead of manually adding a new column after k datasets have been read in, read your individual data files into a list, as long as they all have the same variable names and the same class (in this case, data.frame). Then create a vector of names for

Re: [R] Exporting column names with spaces with write.csv() XXXX

2015-09-05 Thread Dennis Murphy
...or, when trying to read it back into R, read.csv(header = TRUE, text = ' "no good","no good at all" 1,4 2,5 3,6') no.good no.good.at.all 1 1 4 2 2 5 3 3 6 read.csv(header = TRUE, check.names = FALSE, text = ' "no good","no good at

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

2015-06-06 Thread Dennis Murphy
I'm sorry, but I have to take issue with this particular use case of ifelse(). When the goal is to generate a logical vector, ifelse() is very inefficient. It's better to apply a logical condition directly to the object in question and multiply the result by 1 to make it numeric/integer rather

Re: [R] geom_text in ggplot (position)

2015-05-13 Thread Dennis Murphy
This problem is discussed in Winston Chang's R Graphics Handbook, section 3.9. Adapting his code to this example: test - data.frame(variables = c(PE_35, PE_49), value1=c(13,3), value2=c(75,31), value3=c(7,17),

Re: [R] R code/package for calculation of Wasserstein distance between two densities

2015-04-20 Thread Dennis Murphy
Hi Ranjan: Try this: library(sos) findFn(Wasserstein) It appears there are three packages that might be relevant: HistDAWass, transport and TDA. HTH, Dennis On Sat, Apr 18, 2015 at 8:06 PM, Ranjan Maitra maitra.mbox.igno...@inbox.com wrote: Dear friends, Before reinventing the wheel, I was

Re: [R] transpose a data frame according to a specific variable

2015-02-09 Thread Dennis Murphy
One way is to use the reshape2 package: library(reshape2) dcast(DF, id ~ Year, value.var = Day) Dennis On Mon, Feb 9, 2015 at 7:47 AM, jeff6868 geoffrey_kl...@etu.u-bourgogne.fr wrote: Dear R-users, I would like to transpose a large data.frame according to a specific column. Here's a

Re: [R] color heatmap according to value ranges

2015-02-06 Thread Dennis Murphy
Hi: You get a gradient for your response variable because it is numeric. You need to convert it to factor. (Discrete color sets need to be mapped to discrete variables - a factor is one way to generate a discrete variable.) Here is one approach to get what you appear to be looking for. DF -

Re: [R] Paste every two columns together

2015-01-28 Thread Dennis Murphy
Hi: Don't know about performance, but this is fairly simple for operating on atomic vectors: x - c(A, A, G, T, C, G) apply(embed(x, 2), 1, paste0, collapse = ) [1] AA GA TG CT GC Check the help page of embed() for details. Dennis On Wed, Jan 28, 2015 at 3:55 PM, Kate Ignatius

Re: [R] how to determine power in my analysis?

2014-11-08 Thread Dennis Murphy
Hi Kristi: I think this paper elucidates the problem Bert mentioned. A thorough and careful reading of the last two sections should clarify what post-hoc power is and is not. http://www.stat.uiowa.edu/files/stat/techrep/tr378.pdf Dennis On Sat, Nov 8, 2014 at 11:25 AM, Kristi Glover

Re: [R] ggplot barplot error

2014-05-15 Thread Dennis Murphy
Hi: There are several options, but you shot yourself in the foot by using cbind() to combine objects of different classes into a matrix, so everything ends up being a factor because a matrix is an atomic object with a dim attribute, and atomic objects must have a common class, so Flows and

Re: [R] How to plot data using ggplot

2014-04-04 Thread Dennis Murphy
1. Look into the ggmap package if you want to overlay your data onto a map. 2. Re your color scale representation, define 'appropriately'. Do you mean a continuous range expressible in a colorbar or a discrete range, and if the latter, what intervals did you have in mind? Dennis On Fri, Apr 4,

Re: [R] creating table with sequences of numbers based on the table

2014-03-13 Thread Dennis Murphy
Less coding with plyr: tab - read.table(text=pop Freq 1 1 30 2 2 25 3 3 30 4 4 30 5 5 30 6 6 30 7 7 30,sep=,header=TRUE) # Function to do the work on each row f - function(pop, Freq) data.frame(ind = seq_len(Freq)) library(plyr) u -

Re: [R] Math symbols in ggplot facets

2014-02-22 Thread Dennis Murphy
Hi: You were close... library(ggplot2) m - mpg # Set the factor labels with plotmath code (note the ==) m$drv - factor(m$drv, labels = c(sigma[0] == sqrt(2), sigma[0] == 2 * sqrt(2), sigma[0] == 3 * sqrt(2))) ggplot(m, aes(x =

Re: [R] Legend text populated with calculated values and super script?

2014-02-07 Thread Dennis Murphy
Here's a bquote version: x=c(1,2,3,4); y=c(1,2,3,4); z=c(1.25,1.5,2.5,3.5) # first stats based on data, used to populate legend wdt_n = 50; wdt_mbias = 0.58 wdt_mae = 2.1; wdt_R2 = 0.85 # second stats based on data, used to populate legend spas_n = 50; spas_mbias = 0.58 spas_mae = 2.1;

Re: [R] passing variable names to dplyr

2014-01-28 Thread Dennis Murphy
David: I privately suggested he post to manipulatr because Hadley is more likely to see his question there first than in R-help. He originally posted here, noted the cross-posting and referral at manipulatr and responded back to this list when he got a successful reply from Hadley. I don't see

Re: [R] demonstrating R in introductory class using point-and-click software

2014-01-15 Thread Dennis Murphy
Hi Ranjan: I think this is an important and well-posed question. AFAIR, sciviews provides a fairly sophisticated GUI for R, but I haven't looked at it in a while. The two packages I'd suggest you look at are John Fox's R Commander (package Rcmdr...and note all the plug-ins!) and Ian Fellows'

Re: [R] Help with removing extra legend elements in ggplot

2013-11-19 Thread Dennis Murphy
The additional element comes from this code: geom_point(aes(color = VegType, shape = VegType, size = 10)) Take the size argument outside the aes() statement and the legend will disappear: geom_point(aes(color = VegType, shape = VegType), size = 10) The aes() statement maps a variable to a plot

Re: [R] optimization

2013-11-16 Thread Dennis Murphy
There are lots of errors in your code. In particular, the optimization routines do not like functions that ignore the parameters. I would like to nominate this delicious riposte as a fortune candidate. Anyone to second the motion? Dennis On Sat, Nov 16, 2013 at 1:26 PM, Prof J C Nash (U30A)

Re: [R] matrix values linked to vector index

2013-10-11 Thread Dennis Murphy
Attempting to follow the OP's conditions and assuming I understood them correctly, here is one way to wrap this up into a function: makeMat - function(x) { stopifnot(is.integer(x)) nr - length(x) nc - max(x) # Initialize a matrix of zeros m - matrix(0, nr, nc) #

Re: [R] FW: Library update from version

2013-09-30 Thread Dennis Murphy
In this case the OP probably does need to reinstall contributed packages since going from 2.15.x to 3.0.y entails a major version change in R. Dennis On Mon, Sep 30, 2013 at 11:49 AM, Steve Lianoglou lianoglou.st...@gene.com wrote: Hi, On Mon, Sep 30, 2013 at 11:12 AM, Cem Girit

Re: [R] Combine multiple tables into one

2013-05-01 Thread Dennis Murphy
Isn't this just a block diagonal matrix? library(pracma) blkdiag(table1, table2) [,1] [,2] [,3] [,4] [1,]1100 [2,]1200 [3,]0001 [4,]0004 Dennis On Wed, May 1, 2013 at 5:41 PM, David Winsemius dwinsem...@comcast.net wrote:

Re: [R] Arranging two different types of ggplot2 plots with axes lined up

2013-04-19 Thread Dennis Murphy
library(ggplot2) library(gridExtra) #generate test precipitation data year-c(2000,2001,2002,2003,2004) precip-c(46,100,80,74,20) yp-data.frame(year, precip) #generate test fecal coliform data year2-c(2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,

Re: [R] ggplot2: less than equal sign

2013-03-28 Thread Dennis Murphy
Hi: Here's a way you could do it entirely within ggplot2. The annotation functions have a parse = argument which allows you to pass character string representations of math expressions, but there is no such thing in the scale functions, so you need a different approach. library(ggplot2) df -

Re: [R] merging or joining 2 dataframes: merge, rbind.fill, etc.?

2013-02-27 Thread Dennis Murphy
Hi: The other day I ran 100K simulations, each of which returned a 20 x 4 data frame. I stored these in a list object. When attempting to rbind them into a single large data frame, my first thought was to try plyr: library(plyr) bigD - ldply(L, rbind) # where L is the list object I quit at

Re: [R] Plot a Matrix as an Image with ggplot

2013-02-15 Thread Dennis Murphy
the two values of the game, 0 and 1 , instead of 0, 0.25, 0.50, 0.75, 1? I would like to thank you in advanec for your help Regards Alex - Original Message - From: Dennis Murphy djmu...@gmail.com To: Alaios ala...@yahoo.com Cc: Sent: Friday, February 15, 2013 10:19 AM Subject

Re: [R] Why replacement has length zero? And How can I fix it?

2013-02-02 Thread Dennis Murphy
Hi: No offense, but this is code is inefficient on several levels. Firstly, a loop is unnecessary (see below); secondly, ifelse() is a vectorized function and you're attempting to apply it to each row of DataSet. Try this instead, given dput(DataSet) structure(list(Age = c(-0.552450789929175,

Re: [R] facet plot

2013-02-01 Thread Dennis Murphy
Hi: Try DF - structure(list(Gene = structure(1:5, .Label = c(NM_001001130, NM_001001144, NM_001001152, NM_001001160, NM_001001176 ), class = factor), T0h = c(68L, 0L, 79L, 1L, 0L), T0.25h = c(95L, 1L, 129L, 1L, 0L), T0.5h = c(56L, 4L, 52L, 2L, 0L), T1h = c(43L, 0L, 50L, 0L, 0L), T2h = c(66L, 1L,

Re: [R] How to select a subset data to do a barplot in ggplot2

2012-12-13 Thread Dennis Murphy
Hi: The simplest way to do it is to modify the input data frame by taking out the records not having status live or dead and then redefining the factor in the new data frame to get rid of the removed levels. Calling your input data frame DF rather than data, DF - structure(list(FID = c(1L, 1L,

Re: [R] aggregate() runs out of memory

2012-09-14 Thread Dennis Murphy
Hi: This should give you some idea of what Steve is talking about: library(data.table) dt - data.table(x = sample(10, 1000, replace = TRUE), y = rnorm(1000), key = x) dt[, .N, by = x] system.time(dt[, .N, by = x]) ...on my system, dual core 8Gb RAM running Win7

Re: [R] fill binary matrices with random 1s

2011-11-29 Thread Dennis Murphy
Hi: Here's one approach. I assume that your first 1000 matrices have a single 1 in each matrix, the next set of 1000 have two 1's, ..., and the last one has 99 1's. (No point in doing all 1's since they're all constant.) If that's the case, then try the following. # Each row represents a

Re: [R] Help needed in reproducing a plot

2011-11-29 Thread Dennis Murphy
Hi: This looks like the one: library('lattice') data(Oats, package = 'MEMSS') print(xyplot(yield ~ nitro | Block, Oats, groups = Variety, type = c(g, b), auto.key = list(lines = TRUE, space = 'top', columns = 3), xlab = Nitrogen concentration (cwt/acre),

Re: [R] cumsum in 3d arrays

2011-11-28 Thread Dennis Murphy
Hi: Could you supply a small reproducible example with the output that you expect? For example, what output would you expect from the following: a - array(1:24, c(2, 2, 3)) ? Dennis On Mon, Nov 28, 2011 at 12:32 AM, zloncaric zlonca...@biologija.unios.hr wrote: Thank you for your time and

Re: [R] simplify source code

2011-11-26 Thread Dennis Murphy
Hi: Here's one way you could do it. I manufactured some fake data with a simple model to illustrate. This assumes you are using the same model formula with the same starting values and remaining arguments for each response. dg - data.frame(x = 1:10, y1 = sort(abs(rnorm(10))),

Re: [R] data.table merge equivalent for all.x

2011-11-26 Thread Dennis Murphy
Hi: There may well be a more efficient way to do this, but here's one take. library('data.table') # Want to merge by D in the end, so set D as part of the key: t1 - data.table(ID = 11:20, A = rnorm(10), D = 1:10, key = ID, D) t2 - data.table(ID2 = 1:10, D = rep(1:5, 2), B = rnorm(10), key = ID2,

Re: [R] Copula Fitting Using R

2011-11-25 Thread Dennis Murphy
Hi: This is the type of question for which the sos package can come to the rescue: library('sos') findFn('Gumbel Clayton copula') It appears that the QRMlib package would be a reasonable place to start. Dennis On Thu, Nov 24, 2011 at 7:29 PM, cahaya iman qaisfay...@gmail.com wrote: Hi, Is

Re: [R] Looping and paste

2011-11-24 Thread Dennis Murphy
Hi: There are two good reasons why the loop solution is not efficient in this (and related) problem(s): (i) There is more code and less transparency; (ii) the vectorized solution is four times faster. Here are the two proposed functions: # Vectorized version m1 - function(v) paste(v, ' to ', v

Re: [R] AlgDesign - $D $A $Ge $Dea

2011-11-24 Thread Dennis Murphy
See ?AlgDesign::optFederov and look for the 'Value' section, where these elements of the output list are defined. Dennis On Wed, Nov 23, 2011 at 7:15 PM, 蓁蓁 李 tszhenzh...@hotmail.com wrote: Hi, I am wondering how I should interpreate the output of optFederov() in AlgDesign. Specially I

Re: [R] dataframe indexing by number of cases per group

2011-11-24 Thread Dennis Murphy
A very similar question was asked a couple of days ago - see the thread titled Removing rows in dataframe w'o duplicated values - in particular, the responses by Dimitris Rizopoulos and David Winsemius. The adaptation to this problem is df[ave(as.numeric(df$group), as.numeric(df$group), FUN =

Re: [R] Thank you

2011-11-24 Thread Dennis Murphy
Well said. +1 Dennis On Thu, Nov 24, 2011 at 6:43 AM, Bert Gunter gunter.ber...@gene.com wrote: ... and while I am at it, as this is the U.S. Thanksgiving... My sincere thanks to the many R developers and documenters who contribute large amounts of their personal time and effort to

Re: [R] lines and points in xyplot()

2011-11-23 Thread Dennis Murphy
Hi: Try this: library('lattice') xyplot(y ~ x, type = c('g', 'p'), panel = function(x, y, ...){ panel.xyplot(x, y, ...) panel.lines(x, predict(fm), col = 'black', lwd = 2) } ) HTH, Dennis On Wed, Nov 23, 2011 at

Re: [R] zeros to NA's - faster

2011-11-23 Thread Dennis Murphy
Matrix multiplication, maybe? all_data %*% iu a b c [1,] 1 4 0 [2,] 2 5 0 [3,] 3 6 0 I have no idea if this is a general solution or not, but it works in this case. If you need something else, perhaps a more realistic example would help. Dennis On Wed, Nov 23, 2011 at 11:41 AM, Ben quant

Re: [R] Removing rows in dataframe w'o duplicated values

2011-11-22 Thread Dennis Murphy
Hi: Here's one way: do.call(rbind, lapply(L, function(d) if(nrow(d) 1) return(d))) id value value2 1.1 1 5 1 1.2 1 6 4 1.3 1 7 3 3.5 3 5 4 3.6 3 4 3 HTH, Dennis On Tue, Nov 22, 2011 at 9:43 AM, AC Del Re de...@wisc.edu wrote: Hi, Is

Re: [R] Removing rows in dataframe w'o duplicated values

2011-11-22 Thread Dennis Murphy
Sorry, you need this first: L - split(dat, dat$id) do.call(rbind, lapply(L, function(d) if(nrow(d) 1) return(d))) D. On Tue, Nov 22, 2011 at 10:38 AM, Dennis Murphy djmu...@gmail.com wrote: Hi: Here's one way: do.call(rbind, lapply(L, function(d) if(nrow(d) 1) return(d)))    id value

Re: [R] R ignores number only with a nine under 10000

2011-11-21 Thread Dennis Murphy
Hi: Strictly a guess, but the following might be helpful. The call below assumes that the referent data frame is test1, which consists of a single column named x. Modify as appropriate. test_lab - with(test1, cut(x, c(0, 18954, 37791, 56951, 75944, 84885, 113835), labels =

Re: [R] Adding two or more columns of a data frame for each row when NAs are present.

2011-11-20 Thread Dennis Murphy
Hi: Does this work for you? yy Q20 Q21 Q22 Q23 Q24 1 0 1 2 3 4 2 1 NA 2 3 4 3 2 1 2 3 4 rowSums(yy, na.rm = TRUE) 1 2 3 10 10 12 # Use a subset of the variables in yy: selectVars - paste('Q', c(20, 21, 24), sep = '') rowSums(yy[, selectVars], na.rm = TRUE) 1

Re: [R] Continuasly Compunded Returns with quantmod-data

2011-11-20 Thread Dennis Murphy
Hi: Start by writing a small utility function to compute the CCR (not to be confused with the rock band): ccr - function(x) 100 * (log(x[-1]) - log(x[-length(x)])) # Small test: x - round(rnorm(10, 5, 0.5), 2) ccr(x) # Works on vectors... # Load up the stock data: library(quantmod)

Re: [R] Deleting multiple rows from a data matrix based on exp value

2011-11-20 Thread Dennis Murphy
Without a reproducible example this is just a guess, but try Matrix[apply(Matrix, 1, function(x) any(x 1.11)), ] This will retain all rows of Matrix where at least one value in a row is above the threshold 1.11. If that doesn't do what you want, please provide a small reproducible example and a

Re: [R] Apply functions along layers of a data matrix

2011-11-18 Thread Dennis Murphy
Hi: Here are two ways to do it; further solutions can be found in the doBy and data.table packages, among others. library('plyr') ddply(daf, .(id), colwise(mean, c('v1', 'v2', 'v3', 'v4'))) aggregate(cbind(v1, v2, v3, v4) ~ id, data = daf, FUN = mean) # Result of each: id v1 v2 v3 v4 1 1 6

Re: [R] couting events by subject with black out windows

2011-11-18 Thread Dennis Murphy
Hi: Here's a Q D solution that could be improved. It uses the plyr package. Starting from your data1 data frame, library('plyr') dseq - seq(as.Date('2010-01-01'), as.Date('2011-06-05'), by = '30 days') # Use the cut() function to create a factor whose levels are demarcated # by the dates in

Re: [R] reshape data.frame

2011-11-18 Thread Dennis Murphy
This is very straightforward using the reshape2 package: library('reshape2') dc - dcast(a, name ~ year, value_var = 'amount') name 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1a123456789 10 NA NA NA NA 2b 11 12

Re: [R] split list of characters in groups of 2

2011-11-17 Thread Dennis Murphy
Hi: Here's one way: apply(matrix(var.names, ncol = 2, byrow = TRUE), 1, function(x) paste(x[1], x[2], sep = ',')) [1] a,b c,d e,f HTH, Dennis On Wed, Nov 16, 2011 at 9:46 PM, B77S bps0...@auburn.edu wrote: hi, If i have a list of things, like this var.names - c(a, b, c, d, e, f) how can

Re: [R] Introducing \n's so that par.strip.text can produce multiline strips in lattice

2011-11-17 Thread Dennis Murphy
Hi: This worked for me - I needed to modify some of the strip labels to improve the appearance a bit and also reduced the strip font size a bit to accommodate the lengths of the strings. The main thing was to change \\n to \n. Firstly, I created a new variable called Indic as a character

Re: [R] Theil decomposition

2011-11-16 Thread Dennis Murphy
Looking at the help pages in the ineq package, the Theil() function simply returns the value of the index. You can look at the source code of the relevant functions to see what they actually compute: library('ineq') ineq # the main function Theil# for the Theil index Neither function is

Re: [R] geom_bar with missing data in package ggplot

2011-11-16 Thread Dennis Murphy
Hi: Here's one way, but it puts the two countries side by side rather than stacked (I'm not a big fan of stacked bar charts except in certain contexts). The first version uses the original data, but one can see immediately that there is no distinction between NA and 0: ggplot(g, aes(x = Date, y

Re: [R] create list of names where two df contain == values

2011-11-16 Thread Dennis Murphy
Hi: I think you're overthinking this problem. As is usually the case in R, a vectorized solution is clearer and provides more easily understood code. It's not obvious to me exactly what you want, so we'll try a couple of variations on the same idea. Equality of floating point numbers is a

Re: [R] access sublists by a variable

2011-11-16 Thread Dennis Murphy
Hi: Try dict[[a]] rather than dict$a, and read the section on lists in the Introduction to R manual to learn how to properly index them. HTH, Dennis On Wed, Nov 16, 2011 at 7:16 AM, Antje Gruner n...@allesjetzt.net wrote: Dear list, I'd like to access the elements of a list within another

Re: [R] equal spacing of the polygons in levelplot key (lattice)

2011-11-16 Thread Dennis Murphy
, labels = levs) )) Dennis On Wed, Nov 16, 2011 at 10:27 AM, Andy Bunn andy.b...@wwu.edu wrote: -Original Message- From: Dennis Murphy [mailto:djmu...@gmail.com] Sent: Tuesday, November 15, 2011 8:54 PM To: Andy Bunn Cc: r-help@r-project.org Subject

Re: [R] boxplot strange behavior

2011-11-16 Thread Dennis Murphy
And you got a reply on the ggplot2 list, which is why you're asked not to cross-post. For those who are wondering, geom_boxplot() in the ggplot2 package will by default plot outside points along the same line as the boxplot whiskers at their actual values. The gentleman jittered the original

Re: [R] equal spacing of the polygons in levelplot key (lattice)

2011-11-15 Thread Dennis Murphy
Hi: Does this work? # library('lattice') levs - as.vector(quantile(volcano,c(0,0.1,0.5,0.9,0.99,1))) levelplot(volcano, at = levs, colorkey = list(labels = list(at = levs, labels = levs) )) HTH, Dennis On Tue, Nov 15, 2011 at 1:12

Re: [R] Convert full matrix back to lower triangular matrix

2011-11-15 Thread Dennis Murphy
Hi: (1) Here is why your e-mails look mangled on this list: [[alternative HTML version deleted]] R-help is a text-only list, so please change your mailer's settings to send ASCII text rather than HTML. (2) The print method you see displayed in dd1 is equivalent to the following: ddm *

Re: [R] global optimisation with inequality constraints

2011-11-15 Thread Dennis Murphy
(1) Please do not hijack another thread to ask an off-topic question - start a new one instead. (2) Your question refers to a topic called 'restricted least squares'. Search the R-help archives on that subject and you should get a number of answers. Someone answered a similar question here within

Re: [R] max min values within dataframe

2011-11-14 Thread Dennis Murphy
Groupwise data summarization is a very common task, and it is worth learning the various ways to do it in R. Josh showed you one way to use aggregate() from the base package and Michael showed you one way of using the plyr package to do the same; another way would be ddply(df, .(Patient, Region),

Re: [R] Adding units to levelplot's colorkey

2011-11-14 Thread Dennis Murphy
You don't show code or a reproducible example, so I guess you want a general answer. Use the draw.colorkey() function inside the levelplot() call. It takes an argument key =, which accepts a list of arguments, including space, col, at, labels, tick.number, width and height (see p. 155 of the

Re: [R] Generate the distribution

2011-11-13 Thread Dennis Murphy
Google is an amazing resource for getting information. Try Googling 'simulation in R' - I got several useful hits on the first page. HTH, Dennis On Sun, Nov 13, 2011 at 7:41 AM, Anban nino.z...@gmail.com wrote: Hi everyone, i really need some help with one task. I simply cant understand what

Re: [R] 2^k*r (with replications) experimental design question

2011-11-13 Thread Dennis Murphy
I'm guessing you have nine replicates of a 2^5 factorial design with a couple of missing values. If so, define a variable to designate the replicates and use it as a blocking factor in the ANOVA. If you want to treat the replicates as a random rather than a fixed factor, then look into the nlme or

Re: [R] beanplot without log scale?

2011-11-11 Thread Dennis Murphy
Hi: Try the log = argument to beanplot(). Here's an example: library('beanplot') v - exp(runif(1000, 0, 10)) beanplot(v) beanplot(v, log = ') HTH, Dennis On Thu, Nov 10, 2011 at 1:27 PM, Twila Moon twi...@uw.edu wrote: Is it possible to force beanplot not to use a log scale? I want to be

Re: [R] multivariate modeling codes

2011-11-11 Thread Dennis Murphy
Hi: R-Bloggers picked up on this web site that contains several interesting posts re usage of R, including this one on using a Bayesian approach to multivariate mixed effects models using the MCMCglmm package:

Re: [R] Combining Overlapping Data

2011-11-11 Thread Dennis Murphy
Hi: This doesn't sort the data by strain level, but I think it does what you're after. It helps if strain is either a factor or character vector in each data frame. h - function(x, y) { tbx - table(x$strain) tby - table(y$strain) # Select the strains who have more than one member

Re: [R] ggplot2 - regression statistics how to display on plot

2011-11-10 Thread Dennis Murphy
Hi: Here's an example of how one might do this in a specific example using geom_text(). # Some fake data: df - data.frame(x = 1:10, y = 0.5 + (1:10) + rnorm(10)) # Fit a linear model to the data and save the model object: mod - lm(y ~ x, data = df) # Create a list of character strings - the

Re: [R] newbie's question : xyplot legend with a white background

2011-11-10 Thread Dennis Murphy
Hi: Add a background = 'color' argument to key(): library('lattice') xyplot(1~1, panel = function(x,y, ...) { panel.xyplot(x,y) panel.abline(v=seq(0,1.4,by=0.1)) panel.abline(h=seq(0,1.4,by=0.1)) } ,key = list (x=0.2,

Re: [R] counting columns that match criteria

2011-11-10 Thread Dennis Murphy
Hi: Here's a toy example: # Default var names are V1-V20: u - as.data.frame(matrix(rpois(100, 3), ncol = 20)) u - transform(u, ngt1 = apply(u[, c('V1', 'V4', 'V9', 'V15')], 1, function(x) sum(x 1)) ) u HTH, Dennis On Thu, Nov 10, 2011 at 7:24 AM, JL Villanueva jlpost...@gmail.com wrote:

Re: [R] plotting a function with given formula in ggplot2

2011-11-10 Thread Dennis Murphy
Hi: Borrowing from this thread, courtesy of Brian Diggs: http://groups.google.com/group/ggplot2/browse_thread/thread/478f9e61d41b4678/ed323c497db61156?lnk=gstq=stat_function#ed323c497db61156 ...here's a small reproducible example: ddf - data.frame(x = 1:10, y = 0.4 + 0.6 * (1:10) + rnorm(10)) #

Re: [R] Listing tables together from random samples from a generated population?

2011-11-10 Thread Dennis Murphy
Hi: There are two ways you could go about this: lists or arrays. It's pretty easy to generate an array, a little more work to get the list. I'm assuming the objective is to extract a chi-square statistic from each table, so I'll show a couple of ways to do that, too. library('plyr') ## Start

Re: [R] why NA coefficients

2011-11-08 Thread Dennis Murphy
: Dennis Murphy djmu...@gmail.com To: array chip arrayprof...@yahoo.com Sent: Monday, November 7, 2011 9:29 PM Subject: Re: [R] why NA coefficients Hi John: What is the estimate of the cell mean \mu_{12}? Which model effects involve that cell mean? With this data arrangement, the expected

Re: [R] ggplot2 reorder factors for faceting

2011-11-08 Thread Dennis Murphy
Hi: (1) Here is one way to reorganize the levels of a factor: plotData[['infection']] - factor(plotData[['infection']], levels = c('InfA', 'InfC', 'InfB', 'InfD')) Do this ahead of the call to ggplot(), preferably after plotData is defined. relevel() resets the

Re: [R] lapply to list of variables

2011-11-08 Thread Dennis Murphy
Hi: Here's another way of doing this on the simplified version of your example: L - vector('list', 3) # initialize a list of three components ## populate it for(i in seq_along(L)) L[[i]] - rnorm(20, 10, 3) ## name the components names(L) - c('Monday', 'Tuesday', 'Wednesday') ## replace

Re: [R] Sampling with conditions

2011-11-08 Thread Dennis Murphy
In addition to Dan's quite valid concern, the final sample is not truly 'random' - the first k - 1 elements are randomly chosen, but the last is determined so that the constraint is met. Dennis On Tue, Nov 8, 2011 at 9:59 AM, Nordlund, Dan (DSHS/RDA) nord...@dshs.wa.gov wrote: -Original

Re: [R] nesting scale_manual caracteristics in ggplot

2011-11-08 Thread Dennis Murphy
Hi: The problem is that you use different sets of labels in each scale_*_manual. To get all of the scales to merge into one, you need to have the same title, same breaks and same labels for the legend. This gets you a single legend: ggplot(data = df, aes(x = year, y = total, colour = treatment,

Re: [R] window?

2011-11-08 Thread Dennis Murphy
The ets() function in the forecast package requires either a numeric vector or a Time-Series object (produced from ts()). The frequency argument in ts() refers to the time duration between observations; e.g., frequency = 7 means that the data are weekly; frequency = 12 means that the data are

Re: [R] nesting scale_manual caracteristics in ggplot

2011-11-08 Thread Dennis Murphy
As I mentioned earlier, if you use different labels/breaks/titles for the legends, you'll get separate legends in a ggplot. I'm assuming you want to keep your original labels in scale_shape_manual(); there's no problem if you do that. The code I provided showed you how to align all three legends;

Re: [R] Sorting Panel Data by Time

2011-11-08 Thread Dennis Murphy
Here's another approach using the plyr and data.table packages, where df is the name I gave to your example data: # plyr library('plyr') ddply(df, .(TIME), mutate, L1 = sort(S1)) # Another way with the data.table package: library('data.table') dt - data.table(df, key = 'TIME') dt[, list(X1, S1,

Re: [R] plot separate groups with plotmeans()

2011-11-08 Thread Dennis Murphy
Hi: Here are a couple of ways using the plyr and ggplot2 packages: library('plyr') library('ggplot2') # Create a summary data frame that computes the mean # and standard deviation at each time/group combination xsumm - ddply(x, .(Time, Group), summarise, m = mean(Score), s = sd(Score)) #

Re: [R] logistric regression: model revision

2011-11-07 Thread Dennis Murphy
Since you didn't provide a reproducible example, here are a couple of possibilities to check, but I have utterly no idea if they're applicable to your problem or not: * does costdis1 consist of 0's and 1's? * is costdis1 a factor? In the first model, you treat costdis1 as a pure

Re: [R] Correction in error

2011-11-07 Thread Dennis Murphy
Hi: In your function call, x[1, 1] = theta = 0. In the first line of the loop, your rbinom() call works out to be x[2, 1] - rbinom(x[1, 3], 1, x[1, 1]) = rbinom(10, 1, 0) That likely accounts for the error message: Error in x[t, 1] - rbinom(x[t - 1, 3], 1, x[t - 1, 1]) : replacement

Re: [R] Correlation between matrices

2011-11-06 Thread Dennis Murphy
into a **ply() function in plyr can be tricky. If you continue to have problems, please provide a reproducible example as you did here. HTH, Dennis On Sun, Nov 6, 2011 at 12:53 PM, Dennis Murphy djmu...@gmail.com wrote: Hi: I don't think you want to keep these objects separate; it's better

Re: [R] Correlation between matrices

2011-11-05 Thread Dennis Murphy
Hi: I don't think you want to keep these objects separate; it's better to combine everything into a data frame. Here's a variation of your example - the x variable ends up being a mouse, but you may have another variable that's more appropriate to plot so take this as a starting point. One plot

Re: [R] Is it possible to vectorize/accelerate this?

2011-11-03 Thread Dennis Murphy
Hi: You're doing the right thing in R by pre-allocating memory for the result, but ifelse() is a vectorized function and your loop is operating elementwise, so if-else is more appropriate. Try for (i in 2:100){ b_vec[i] - if(abs(b_vec[i-1] + a_vec[i]) 1) a_vec[i] else b_vec[i-1] + a_vec[i]

Re: [R] round up a number to 10^4

2011-11-01 Thread Dennis Murphy
Works in the newly released 2.14.0: X = c(60593.23, 71631.17, 75320.1) round(X, -4) [1] 6 7 8 Dennis On Tue, Nov 1, 2011 at 10:07 AM, Wendy wendy2.q...@gmail.com wrote: Hi all, I have a list of numbers, e.g., X = c(60593.23, 71631.17, 75320.1), and want to round them  so the

Re: [R] Removal/selecting specific rows in a dataframe conditional on 2 columns

2011-11-01 Thread Dennis Murphy
Does this work? library('plyr') # Function to return a data frame if it has one row, else return NULL: f - function(d) if(nrow(d) == 1L) d else NULL ddply(RV09, .(set, month), f) record.t trip set month stratum NAFO unit.area time dur.set distance 15 913 110 351 3O

Re: [R] Counting entries to create a new table

2011-11-01 Thread Dennis Murphy
Hi: After cleaning up your data, here's one way using the plyr and reshape packages: d - read.csv(textConnection( Individual, A, B, C, D Day1, 1,1,1,1 Day2, 1,3,4,2 Day3, 3,,6,4), header = TRUE) closeAllConnections() d library('plyr') library('reshape') # Stack the variables dm - melt(d, id =

Re: [R] predict for a cv.glmnet returns an error

2011-11-01 Thread Dennis Murphy
Hi: Here's what I got when I ran your code: library('glmnet') x=matrix(rnorm(100*20),100,20) y=rnorm(100) cv.fit=cv.glmnet(x,y) predict(cv.fit,newx=x[1:5,]) 1 [1,] 0.1213114 [2,] 0.1213114 [3,] 0.1213114 [4,] 0.1213114 [5,] 0.1213114 coef(cv.fit) 21 x 1 sparse Matrix of class

Re: [R] building a subscript programatically

2011-11-01 Thread Dennis Murphy
Here's a hack, but perhaps you might want to rethink what type of output you want. # Function: g - function(arr, lastSubscript = 1) { n - length(dim(arr)) commas - paste(rep(',', n - 1), collapse = '') .call - paste('arr[', commas, lastSubscript, ']', sep = '') eval(parse(text =

Re: [R] jarquebera_test_results

2011-10-30 Thread Dennis Murphy
Hi: I'd double check the form of the upper bound of the sequence. In the first case (i = 1), it uses the subvector loghozamok[1:220]; when i = 2, it uses the subvector loghozamok[21:260], etc. In the last case, it uses loghozamok[1181:1420]. If instead you want to split the vector into 60 groups

Re: [R] Normality tests on groups of rows in a data frame, grouped based on content in other columns

2011-10-30 Thread Dennis Murphy
Hi: Here are a few ways (untested, so caveat emptor): # plyr package library('plyr') ddply(df, .(Plant, Tissue, Gene), summarise, ntest = shapiro.test(ExpressionLevel)) # data.table package library('data.table') dt - data.table(df, key = 'Plant, Tissue, Gene') dt[, list(ntest =

Re: [R] Faster process for creating a matrix based on matrix element comparison

2011-10-28 Thread Dennis Murphy
dat [,1] [,2] [,3] [1,]001 [2,]010 [3,]011 [4,] NA11 1 - is.na(dat) [,1] [,2] [,3] [1,]111 [2,]111 [3,]111 [4,]011 D. On Fri, Oct 28, 2011 at 11:40 AM, Evgenia ev...@aueb.gr wrote: I have

Re: [R] show.ci='FALSE' ignored in simple.lm

2011-10-27 Thread Dennis Murphy
Hi: A trip to package sos reveals that simple.lm() is in the UsingR package. Looking at the code of this function, it plots the (x, y) pairs and the fitted least squares line without an option to suppress the plot. Here's a slight hack of the function; it adds a new argument plot with default

Re: [R] help with parallel processing code

2011-10-27 Thread Dennis Murphy
Did you load the class package before calling lda()? Dennis On Thu, Oct 27, 2011 at 10:14 AM, 1Rnwb sbpuro...@gmail.com wrote: my modification gives me error  rows- c(1:nrow(mat))  scores - c()  labels -c()  itr-1000  chnksz-ceiling(itr/getDoParWorkers()) smpopt=list(chunkSize=chnksz)  

Re: [R] Syntax Check: rshape2 melt()

2011-10-27 Thread Dennis Murphy
Try this, based on your small example: tds.a - read.table(textConnection( + site sampdate param quant + 1UDS-O 2006-12-06 TDS 10800 + 4 STC-FS 1996-06-14 Cond 280 + 7UDS-O 2007-10-04Mg 1620 + 9UDS-O 2007-10-04 SO4 7580 + 19 JCM-10B 2007-06-21Ca79 + 20

Re: [R] Help with a scatter plot

2011-10-26 Thread Dennis Murphy
Hi: The sort of thing you appear to want is fairly straightforward to in lattice and ggplot2, as both have ways to automate conditioning plots. Since you were looking at ggpot2, let's consider that problem. You don't really show enough data to provide a useful demonstration, but let's see if we

Re: [R] dotPlot with diagonal

2011-10-26 Thread Dennis Murphy
Let's see: there is a dotPlot() function in each of the following packages: BHH2, caret, mosaic, qualityTools Would you be kind enough to share which of these packages (if any) you are using? Dennis On Wed, Oct 26, 2011 at 4:25 AM, Jörg Reuter jo...@reuter.at wrote: Hi, I want draw a dotPlot.

  1   2   3   4   5   6   7   8   9   10   >