Re: [R] How to draw different series for different groups in xyplot

2011-03-13 Thread Dennis Murphy
Hi: Try this: xyplot(y ~ x1 | x2 * x3, groups = x4, data = example, type= c(l, p), pch=c(16, 4), col=c(blue, red), col.line = c('blue', 'transparent'), xlim = c(1.04, 1.26), ylim = c(0, 1.1), xlab = X2, ylab = Y, key = list(space = top, text = list(A),

Re: [R] Passing a character argument onto a function

2011-03-12 Thread Dennis Murphy
Hi: One answer is eval(substitute(...)): [Note: it's a good idea NOT to use the same name on different objects - R is pretty good about distinguishing TEST the function from TEST the data frame, but you could be playing with fire if both objects have the same class but have different contents.

Re: [R] Column order in stacking/unstacking

2011-03-12 Thread Dennis Murphy
Hi: It would help if you named your variables such that alphanumeric ordering doesn't disturb your variable ordering. Having been burned by this a few times, I've learned the basics of sprintf() :) Here's your example revisited, along with an alternative stacking/unstacking display with package

Re: [R] Identifying unique pairs

2011-03-12 Thread Dennis Murphy
Hi: This problem came up the other day - see http://stats.stackexchange.com/questions/7884/fast-ways-in-r-to-get-the-first-row-of-a-data-frame-grouped-by-an-identifier/7985#7985 Dennis On Sat, Mar 12, 2011 at 3:20 AM, Vincy Pyne vincy_p...@yahoo.ca wrote: Dear R helpers Suppose I have a

Re: [R] plot generates graph with coordinantes written over it

2011-03-12 Thread Dennis Murphy
Hi: The value of your title is a(n attached) data frame - perhaps you meant 'HMF' instead... HTH, Dennis On Sat, Mar 12, 2011 at 2:41 AM, derek jan.kac...@gmail.com wrote: Hello R users, Im having this strange problem. http://r.789695.n4.nabble.com/file/n3350024/bad2.png I have txt file of

Re: [R] how to label lines

2011-03-12 Thread Dennis Murphy
Hi: Since you didn't provide any data, you're stuck with my example instead. # Generate three sine curves on (0, 2*pi) x - seq(0, 2 * pi, length = 200) df - data.frame(x, y1 = sin(x), y2 = sin(2 * x), y3 = sin(4 * x)) # Create a small function to type (n), where n is a number to be input mytext

Re: [R] pass character vector in instrument field of get.hist.quote function

2011-03-12 Thread Dennis Murphy
Hi: (1) The first argument to get.hist.quote() is instrument, not instruments. I concur with David that get.hist.quote() takes a single character string as an argument. (2) I tried running this with lapply() but got a download error on the last one: getStockData(PBR-B) trying URL '

Re: [R] How to calculate means for multiple variables in samples with different sizes

2011-03-11 Thread Dennis Murphy
Hi: Here are a few one-liners. Calling your data frame dd, aggregate(cbind(height, weight, age) ~ sample, data = dd, FUN = mean) sample heightweight age 1 A 12.2 0.503 6.00 2 B 12.75000 0.715 4.50 3 C 11.35250 0.5125000 3.75 4 D 14.99333

Re: [R] How to get all combinations between two character vectors?

2011-03-11 Thread Dennis Murphy
Hi: This is probably not quite what you had in mind, but both work for your example - I think the second is probably easier. x - c(a, b) y - c(x, y) as.vector(outer(x, y, function(x, y) paste(x, y, sep = ''))) [1] ax bx ay by apply(expand.grid(x, y), 1, paste, collapse = '') [1] ax bx ay by

Re: [R] Ifs in formula

2011-03-11 Thread Dennis Murphy
Hi: Perhaps something like f1(x, y) * I(z 0) + f2(x, y) * I(z = 0) ?? HTH, Dennis On Fri, Mar 11, 2011 at 3:10 AM, Otto Kässi otto.ka...@gmail.com wrote: Dear r-helpers, This might be an elementary question, but I have a hard time getting my head around it, so all help is much

Re: [R] insertion of a row between individuals

2011-03-11 Thread Dennis Murphy
Hi: df - read.table(textConnection( VAR DATETIME CONC COVAR 1 NOV20.2510 group1 1 NOV20.5 20 group1 1 NOV21 5 group1 1 NOV22 1 group1 1 NOV23 0.1 group1 2 NOV20.2510

Re: [R] Fw: random sampling steps in R with replacement

2011-03-10 Thread Dennis Murphy
Hi: To get the samples, here's one approach: df - data.frame(gender = rep(c('F', 'M'), c(165, 42)), y = rpois(207, 20)) # Sampling function to take 20 F and 20 M with replacement # The sample function operates on the rows of df to get idx and then takes # the y's corresponding to those rows sfun

Re: [R] sum of variables in function

2011-03-10 Thread Dennis Murphy
Hi: Assuming that z constitutes the last two elements of your input vector, try2 - function(x) { n - length(x) y - x[1:(n - 2)] z - x[n - 1] + x[n] sum(y) * z } x0 - 1:4# result should be 21 x1 - 1:8# z = 15, result = 21 * 15 = 315 try2(x0) try2(x1) HTH, Dennis On Thu,

Re: [R] Boxplot problem

2011-03-09 Thread Dennis Murphy
Hi: A box plot is based on a five number summary, so you need at a minimum five observations (and preferably at least twice that) to make a box plot a viable summary measure for a continuous variable. Consider other graphical summaries for these data - perhaps a strip chart or a simple

Re: [R] collapse a data column into a row

2011-03-09 Thread Dennis Murphy
Hi: I'm not quite sure what you have in mind, but... Input data strdat.txt: probeID rc_AI104113_at rc_AI178259_f_at rc_AI179134_i_at rc_AI179134_f_at rc_AI104113_at rc_AA819429_f_at strg - paste(scan('strdat.txt', what = '', skip = 1), collapse = ',') strg [1]

Re: [R] Differences per group

2011-03-09 Thread Dennis Murphy
Hi: Here's another approach: purchase_amount - transform(purchase_amount, diff = with(purchase_amount, ave(amount, customer, FUN = function(x) c(0, diff(x) purchase_amount HTH, Dennis On Wed, Mar 9, 2011 at 7:27 AM, rens_1112 tibid...@rocketmail.com wrote: Dear all, Probably a

Re: [R] Sapply for descriptive statistics

2011-03-09 Thread Dennis Murphy
Hi: Perhaps something like this? m - matrix(rnorm(100, m = 10, s = 2), ncol = 5) colnames(m) - paste('V', 1:5, sep = '') # Summary function: summs - function(x) c(mean = mean(x), sd = sd(x), range = diff(range(x))) # Apply to columns of m and transpose the result: t(apply(m, 2, summs)) For

Re: [R] aggregate by part of a field

2011-03-09 Thread Dennis Murphy
Hi: Here's one approach, although I imagine there are more efficient ways. # A function to strip spaces and return the first three non-blank elements of a string keyset - function(x) substr(gsub(' ', '', x)[1], 1, 3) # Apply the function to the data frame to generate the key: a$key -

Re: [R] How to sort using a predefined criterion

2011-03-08 Thread Dennis Murphy
df - read.table(textConnection( category type values 1 treat_A AA 0.38200018 2 treat_A0.10068056 3 treat_A B0.59648427 4 treat_A AAA 0.89910581 5 treat_A BB 0.88460952 6 treat_A 0.95846431 7 treat_ABBB

Re: [R] XYPLOT - GROUPING WITH TWO CATEGORICAL VARIABLES

2011-03-07 Thread Dennis Murphy
David, David, David...you forgot the solid and dashed lines :) It's OK, it gives me an excuse to look at this from a slightly different angle. [The intersection() function is a *really* good trick, BTW - thanks for the reminder.] Back to the OP. Let's re-read the data: dat=data.frame(Age =

Re: [R] still a problem remainingRE: Data lebals xylattice plot: RE: displaying label meeting condition (i.e. significant, i..e p value less than 005) in plot function

2011-03-07 Thread Dennis Murphy
, 2011 10:50 AM *To:* Umesh Rosyara *Cc:* Jorge Ivan Velez; Dennis Murphy; sarah.gos...@gmail.com; R mailing list *Subject:* Re: [R] Data lebals xylattice plot: RE: displaying label meeting condition (i.e. significant, i..e p value less than 005) in plot function This is easy to do

Re: [R] allocating factor levels

2011-03-07 Thread Dennis Murphy
Hi: Here's one way to piece it together. All we need is the first variable, so I'll manufacture a vector of Start.action's and go from there. w - data.frame(Start.action = c(rep('Start.setting', 3), rep('Start.hauling', 4), rep('Start.setting', 4),

Re: [R] sorting subsetting a data.frame

2011-03-06 Thread Dennis Murphy
Hi: One approach is through the data.table package: library(data.table) df - as.data.frame(data.table(iris, key = 'Species')[Sepal.Length == 6.7]) str(df) Using Species as a key variable automatically sorts by Species; the bracketing allows you to subset on Sepal.Length == 6.7. ddply() in the

Re: [R] Parsing question, partly comma separated partly underscore separated string

2011-03-06 Thread Dennis Murphy
Hi: This should get you most of the way there; I'll let you figure out how to assign the BLOCK and RUN numbers. tx - Subject ID,ExperimentName,2010-04-23,32:34:23,Version 0.4, 640 by 960 pixels, On Device M, M, 3.2.4, zz_373_462_488_...@9z.svg,592,820,3.35,zz_032_288_436_...@9z.svg

Re: [R] How to load load multiple text files and order by id

2011-03-05 Thread Dennis Murphy
Hi: This is basically Scott's idea with a few added details. Let's assume your files have similar names - e.g., they differ only by number. The example below creates ten files of similar structure to yours. There are then two paths one can follow: (1) put all the files into a specific directory,

Re: [R] Lepage Test

2011-03-04 Thread Dennis Murphy
Also see the example in the ScaleTests function from package coin, which I found with library(sos) findFn('Lepage test') Dennis On Fri, Mar 4, 2011 at 12:00 PM, mike vassalosmich...@uky.edu wrote: Hey everyone, I am interest in running a Lepage multi sample test and i would like to ask if

Re: [R] Repeating the same calculation across multiple pairs of variables

2011-03-04 Thread Dennis Murphy
Hi: Perhaps you had something like this in mind: df-data.frame(a1=1:10, a2=11:20, a3=21:30, b1=101:110, b2=111:120, b3=121:130) avars - names(df)[grep('^a', names(df))] bvars - names(df)[grep('^b', names(df))] cvars - paste('c', 1:length(avars), sep = '') df - within(df, for(i in

Re: [R] Error in model.frame.default

2011-03-03 Thread Dennis Murphy
Hi: You need the second variable in D1 to be named fGRAZE - the variable names in the newdata data frame (D1) have to be the same as the variable names on the RHS of the model formula, in this case L.AREA and fGRAZE. HTH, Dennis On Thu, Mar 3, 2011 at 7:43 AM, Heike Schmitz

Re: [R] Probabilities greather than 1 in HIST

2011-03-03 Thread Dennis Murphy
Hi: Here's an example: x - rnorm(300) hist(x, breaks = 15, yaxt = 'n', ylab = 'Relative frequency') axis(2, at = seq(0, 50, by = 10), labels = round(seq(0, 50, by = 10)/length(x), 3)) HTH, Dennis On Thu, Mar 3, 2011 at 8:29 AM, jpmaroco jpmar...@gmail.com wrote: Dear David, Thanks for your

Re: [R] how to delete empty levels from lattice xyplot

2011-03-02 Thread Dennis Murphy
Hi: On Wed, Mar 2, 2011 at 1:52 PM, John Smith zmr...@gmail.com wrote: Hello All, I try to use the attached code to produce a cross over plot. There are 13 subjects, 7 of them in for/sal group, and 6 of them in sal/for group. But in xyplot, all the subjects are listed in both subgraphs.

Re: [R] stuk at another point: simple question

2011-03-01 Thread Dennis Murphy
# from pair M3a and M3b 4 # from pair M4a and M4b to the end of the file Thank you in advance Umesh R -- *From:* Dennis Murphy [mailto:djmu...@gmail.com] *Sent:* Friday, February 18, 2011 12:28 AM *To:* Umesh Rosyara *Cc:* r-help@r-project.org

Re: [R] bootstrap resampling - simplified

2011-03-01 Thread Dennis Murphy
Hi: On Tue, Mar 1, 2011 at 8:22 AM, Bodnar Laszlo EB_HU laszlo.bod...@erstebank.hu wrote: Hello there, I have a problem concerning bootstrapping in R - especially focusing on the resampling part of it. I try to sum it up in a simplified way so that I would not confuse anybody. I have a

Re: [R] Speed up sum of outer products?

2011-03-01 Thread Dennis Murphy
...and this is where we cue the informative article on least squares calculations in R by Doug Bates: http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf HTH, Dennis On Tue, Mar 1, 2011 at 10:52 AM, AjayT ajaytal...@googlemail.com wrote: Hey thanks alot guys !!! That really speeds things up

Re: [R] expression help

2011-03-01 Thread Dennis Murphy
Hi: 1. expression() in plotmath ignores control characters such as \n. 2. The workaround, discussed a couple of times on this list (hence in the archives), is to use the atop function, so try something like plot(0:1,0:1,xaxt=n) axis(side=1,at=.3,expression(paste(IFN-, gamma, \n, TNF-, alpha)))

Re: [R] inefficient ifelse() ?

2011-03-01 Thread Dennis Murphy
Hi: As far as I can see, the problem has to do with the way you wrote f and g: f(2) f for 2 [1] 4 g(5) g for 5 [1] 15 You output an unsaved character string with a numeric result, but the character string is not part of the return object since it is neither saved as a name or an attribute. If

Re: [R] lme error: Error in getGroups.data.frame(dataMix, groups)

2011-02-28 Thread Dennis Murphy
Hi: On Mon, Feb 28, 2011 at 8:42 AM, John Sorkin jsor...@grecc.umaryland.eduwrote: R 2.10.0 Windows XP I am trying to run lme. I receive the following error message: My lme code is: fitRandom - lme(values ~ factor(subject), data=withindata) Where's the random factor? Perhaps you mean

Re: [R] help please ..simple question regarding output the p-value inside a function and lm

2011-02-27 Thread Dennis Murphy
Hi: There has to be a better way to do this, but one option is to use pmin() and pmax(). # Function to apply to pairs of columns: ordPair - function(x, y) data.frame(pmin(x, y), pmax(x,y)) u - with(odataframe, cbind(ordPair(X1, X2), ordPair(X3, X4), ordPair(X5, X6)) u pmin.x..y. pmax.x..y.

Re: [R] nested case-control study

2011-02-27 Thread Dennis Murphy
Hi: Using package sos: # install.packages('sos') # if necessary library(sos) findFn('conditional logistic regression') the following appear to be reasonable candidates to start investigating: * clogit() in package survival * clogistic() in package Epi * clogistCalc()in package saws

Re: [R] accuracy of measurements

2011-02-25 Thread Dennis Murphy
And in that vein, the recently released MethComp package by Bendix Carstensen may be of service. HTH, Dennis On Fri, Feb 25, 2011 at 5:39 AM, Marc Schwartz marc_schwa...@me.com wrote: On Feb 24, 2011, at 4:50 PM, Denis Kazakiewicz wrote: Dear R people Could you please help with following

Re: [R] group by in data.frame

2011-02-25 Thread Dennis Murphy
Hi: Here's another way: c1-c(1,2,3,2,2,3,1,2,2,2) c2-c(5,6,7,7,5,7,5,7,6,6) c3-rnorm(10) x - data.frame(c1 = factor(c1), c2 = factor(c2), c3) x - transform(x, mean = ave(c3, c1, c2, FUN = mean)) Yet another with function ddply() in package plyr: ddply(x, .(c1, c2), transform, mean = mean(c3))

Re: [R] means, SD's and tapply

2011-02-25 Thread Dennis Murphy
Hi: On Fri, Feb 25, 2011 at 12:09 PM, Christopher R. Dolanc crdol...@ucdavis.edu wrote: I'm trying to use tapply to output means and SD or SE for my data but seem to be limited by how many times I can subset it. Here's a snippet of my data stems353[1:10,] Time DataSource Plot

Re: [R] data.frame operations

2011-02-25 Thread Dennis Murphy
Hi: Here's one way: d - read.table(textConnection( ind xloc yloc gonad 1 15 2 12 2 15 4 25 3 13 3 30 4 15 4 10 5 11 4 08 10 11 4 11), header = TRUE) closeAllConnections() library(plyr) dsum - ddply(d,

Re: [R] sum data from data.frame in a matrix

2011-02-23 Thread Dennis Murphy
Hi: df - read.table(textConnection( + xloc yloc yield + 1 101295 + 2 111081 + 3 121120 + 4 121110), header = TRUE) with(df, xtabs(yield ~ yloc + xloc)) xloc yloc 10 11 12 10 0 81 0 11 0 0 30 12 95 0 0 HTH, Dennis On Tue, Feb 22, 2011 at

Re: [R] sum data from data.frame in a matrix

2011-02-23 Thread Dennis Murphy
0 0 30 12 95 0 0 Any hints? THANKS!! Nico Original Message Subject:Re: [R] sum data from data.frame in a matrix Date: Wed, 23 Feb 2011 00:10:52 -0800 From: Dennis Murphy djmu...@gmail.com To: Nicolas Gutierrez nicol...@uw.edu

Re: [R] split the data

2011-02-23 Thread Dennis Murphy
Hi: Is this what you want? x$gpvar - rep(c(1, 2), 4) split(x, x$gpvar) $`1` id v1 V2 gpvar 1 1 1 9 1 3 2 3 11 1 5 3 5 13 1 7 4 7 15 1 $`2` id v1 V2 gpvar 2 1 2 10 2 4 2 4 12 2 6 3 6 14 2 8 4 8 16 2 HTH, Dennis On Wed, Feb 23, 2011 at 5:16

Re: [R] Regarding Soft Independent Modeling Computational Analysis

2011-02-21 Thread Dennis Murphy
Hi: If by SIMCA you mean 'Soft Independent Modeling of Class Analogy', a search with package sos indicated that a few functions in the plsRglm package have some support for it. HTH, Dennis On Sun, Feb 20, 2011 at 9:15 PM, reynolds pravindev reynoldspravin...@gmail.com wrote: Hi I'm a B.E

Re: [R] multiple plots using a loop

2011-02-21 Thread Dennis Murphy
Hi: Here's one way with the plyr package and function d_ply(); the hist() function itself is not very elegant, but it 'works' for this example. Factor - rep(factor(letters[1:4]), each = 10) Size - runif(40) * 100 library(plyr) par(mfrow = c(2, 2)) d - data.frame(Factor, Size) # Function to

Re: [R] Subset according to groups NA proportion within specific variables

2011-02-21 Thread Dennis Murphy
Hi: Here's one way with package plyr: df-data.frame(x=c(rep(1,3),rep(2,4),rep(3,5)), y=rnorm(12), z=c(3,4,5,NA,NA,NA,NA,1,2,1,2,1), w=c(1,2,3,3,4,3,5,NA,5,NA,7,8)) library(plyr) fun - function(d) { u - apply(d[, -1], 2, function(y)

Re: [R] difference in pairs ?

2011-02-21 Thread Dennis Murphy
Hi: Assuming dd is the name of your data frame, dd$diff - with(dd, q2 - q1) dd TIME ID q1 q2 diff 11 1187 3 2 -1 21 1706 3 30 31 1741 2 42 42 1187 3 2 -1 52 1706 3 30 62 1741 2 42 is one way to do it. HTH, Dennis On Mon, Feb 21,

Re: [R] Species accumulation curves with differential sampling effort

2011-02-21 Thread Dennis Murphy
Hi: Try the vegan and biodiversityR packages; both contain functions for producing species accumulation curves. On Mon, Feb 21, 2011 at 9:38 AM, Vanessa Francisco vanewa...@gmail.comwrote: Hello! I'm a PhD student working with coral reef fish diversity in Mexico. I want to do species

Re: [R] plot numerous functions in one figure with different values of x per function

2011-02-21 Thread Dennis Murphy
Hi: Here's one way: f - function(x) sin(x) + pi/4 g - function(x) { sin(x) * (x = 0 x = pi) + sin(x) * (x = -2 * pi x = -pi) - (pi/4) * (x -pi x 0) - pi/4 * (x pi x 2 * pi) } x - seq(-2 * pi, 2 * pi, length = 200) plot(x, f(x), type = 'l', col = 'blue', ylim = c(-1, 2))

Re: [R] Equivalent of log file in R?

2011-02-21 Thread Dennis Murphy
Hi: Perhaps another possibility is ?capture.output Dennis On Mon, Feb 21, 2011 at 10:26 AM, Tatyana Deryugina tatya...@mit.eduwrote: Thanks, Ista! I looked at sink() and it looks like it might work. However, it seems as though you need to use it with the print command. I have a lot of

Re: [R] Calculate a mean for several months for several years

2011-02-21 Thread Dennis Murphy
Hi: Here's one way: # Manufacture some monthly data over 20 years df - data.frame(year = rep(1981:2000, each = 12), month = factor(rep(month.abb, 20), levels = month.abb), meantemp = rnorm(240, mean = rep(c(30, 35, 40, 50, 60, 70, 75,

Re: [R] barplot, different color for shading lines and bar

2011-02-20 Thread Dennis Murphy
Hi: This isn't hard to do with ggplot2. Here's a toy example: d - data.frame(gp = LETTERS[1:4], frq = c(10, 25, 30, 20)) library(ggplot2) # The fill aesthetic colors the bars, the colour aesthetic does the same for the borders. # (1) Same color for both, use alpha transparency: ggplot(d,

Re: [R] Plotting individual trajectories from individual growth model

2011-02-20 Thread Dennis Murphy
Hi: These are sometimes called 'spaghetti plots'; here is a variation on an example in the ggplot2 book by Hadley Wickham using the Oxboys data from package nlme: library(ggplot2) data('Oxboys', package = 'nlme') g - ggplot(Oxboys, aes(x = age, y = height)) g + geom_line(aes(group = Subject)) +

Re: [R] xyplot formula

2011-02-18 Thread Dennis Murphy
Hi: Here's another approach using the reshape package: library(reshape) # id defines the grouping variables; the others are stacked, creating # new variables 'variable' (containing the variable names as factor levels) and # 'value' (which holds the corresponding values) dfm - melt(df, id =

Re: [R] How to change dataframe to tables

2011-02-18 Thread Dennis Murphy
This is a built-in dataset in R - see ?HairEyeColor and str() it. I smell homework... Dennis On Thu, Feb 17, 2011 at 11:50 PM, Lao Meng laomen...@gmail.com wrote: The data is in the attachment. What I wanna get is: , , Sex = Male Eye HairBrown Blue Hazel Green Black32

Re: [R] lm without intercept

2011-02-18 Thread Dennis Murphy
Hi: On Fri, Feb 18, 2011 at 2:49 AM, Jan jrheinlaen...@gmx.de wrote: Hi, I am not a statistics expert, so I have this question. A linear model gives me the following summary: Call: lm(formula = N ~ N_alt) Residuals: Min 1Q Median 3Q Max -110.30 -35.80 -22.77

Re: [R] sampling

2011-02-17 Thread Dennis Murphy
Hi: A couple more approaches to consider: # Utility function to extract two rows from a data frame # Meant to be applied to each data subset sampler - function(d) if(nrow(d) 2) d[sample(1:nrow(d), 2, replace = FALSE), ] else d library(plyr) ddply(x, 'id', sampler) id v1 V2 1 1 2 13 2 1

Re: [R] Integrate with an indicator function

2011-02-17 Thread Dennis Murphy
Hi Hannah: You have a few things going on, but the bottom line is that numer and denom are both double integrals. On Thu, Feb 17, 2011 at 1:06 PM, li li hannah@gmail.com wrote: Hi all, I have some some problem with regard to finding the integral of a function containing an indicator

Re: [R] recoding a data in different way: please help

2011-02-17 Thread Dennis Murphy
Hi: This is as far as I could get: df - read.table(textConnection( Individual Parent1 Parent2 mark1 mark2 10 0 12 11 20 0 11 22 30 0 13 22 40 0 13 11 51 2

Re: [R] how to create normalized pdf plot?

2011-02-16 Thread Dennis Murphy
Hi: There are some reasonably efficient ways to do this if you're a little clever. If you use 16 histograms per page, you can get 928 of them onto 58 pages - with 4 per page, with one left over, that would generate 233 pages of output. I could think of something like the following (pseudocode):

Re: [R] Defining functions inside loops

2011-02-15 Thread Dennis Murphy
Hi: If you look at the error message, you'll see that you removed s before evaluating f, and since an element of s is called in the function Try s - c( 0.2, 0.45, 0.38, 0.9) f - lapply(1:10, function(i)local({ force(i) ; function(x)x^2+s[i]})) f[[1]](s) [1] 0.2400 0.4025 0.3444 1.0100 f

Re: [R] Analyzing dissimilarity ratings with Multidimensional Scaling

2011-02-15 Thread Dennis Murphy
Hi: Try the following for examples of the use of MDS in R: http://www.statmethods.net/advstats/mds.html http://cran.r-project.org/web/packages/HSAUR/vignettes/Ch_multidimensional_scaling.pdf http://cran.r-project.org/web/packages/HSAUR2/vignettes/Ch_multidimensional_scaling.pdf # (2nd ed.)

Re: [R] conditional value assignment

2011-02-14 Thread Dennis Murphy
Hi: Wouldn't ifelse() work here? tco - with(df, ifelse(TargetColor == 'B', CannonOriB, CannonOriR)) ifelse() is vectorized, so there should be no need for a loop. Test: df - data.table(TargetColor = c('B', 'R'), CannonOriB = c(5, 5), + CannonOriR = c(3, 3), stringsAsFactors =

Re: [R] summing values for specific variables

2011-02-13 Thread Dennis Murphy
Hi: One (of many) ways is to use the aggregate() function in base R: df - data.frame(g = rep(LETTERS[1:5], each = 5), y = rnorm(25)) aggregate(y ~ g, data = df, FUN = sum) g y 1 A -1.3165949 2 B 1.9850015 3 C -0.6460323 4 D -1.6459293 5 E 6.7001807 HTH, Dennis On Sun, Feb 13,

Re: [R] lattice auto.key gives mismatch colors

2011-02-11 Thread Dennis Murphy
Hi: This seems to work: mykey - list(space = 'top', columns = 4, text = list(as.character(unique(src$s)), col = colors), points = list(pch = 1, col = colors) ) xyplot(v~t, groups=s, type='o', data=src, col=colors, key = mykey) HTH, Dennis

Re: [R] Permuting rows of a matrix

2011-02-10 Thread Dennis Murphy
Hi: One way: x[sample(nrow(x)), ] HTH, Dennis On Wed, Feb 9, 2011 at 10:45 PM, Diogo Almeida dala...@gmail.com wrote: Hi, I need to permute the rows of a matrix, where each row is independently rearranged. A simple solution is this: shuffled - datamatrix - matrix(1:24, ncol = 4) for (i

Re: [R] about prediction with a factor

2011-02-10 Thread Dennis Murphy
Hi: There are a couple of things here. Firstly, if you intend to predict from a model in R, make sure your input to the model is a data frame. You can fit a model piecemeal with a set of vectors, but there's a distinct possibility you'll end up frustrated when trying to make predictions with new

Re: [R] Repeaded sampling

2011-02-10 Thread Dennis Murphy
Hi: On Thu, Feb 10, 2011 at 10:50 AM, Hui Du hui...@dataventures.com wrote: Hi all, I have a dataset. Each time I want to sample N(i) elements from it and I want to repeated sampling M times. N(i) is varied from time to time. For example, dataset = 1:50;

Re: [R] Repeaded sampling

2011-02-10 Thread Dennis Murphy
My apologies to Eik - I didn't see his solution and essentially replicated it. Sorry for the noise.. Dennis On Thu, Feb 10, 2011 at 3:46 PM, Dennis Murphy djmu...@gmail.com wrote: Hi: On Thu, Feb 10, 2011 at 10:50 AM, Hui Du hui...@dataventures.com wrote: Hi all, I have

Re: [R] Calling symbols from dataframe for xyplot

2011-02-10 Thread Dennis Murphy
Hi: It's not a perfect solution, but an 'easy' way out is to recognize that some symbols are open and some are filled. If you want fill, use a filled symbol and then change the color. p - rep(16:18, 4) x-c(1:12) y-c(rpois(12,4)) grp-c(rep(c(3,4), each=6)) z-c(rep(c(1,2), each=6)) dd -

Re: [R] Getting p-value from summary output

2011-02-10 Thread Dennis Murphy
Hi: Try summary(myprobit)$coefficients[, 4] HTH, Dennis On Thu, Feb 10, 2011 at 3:46 PM, Allie818 alice...@gmail.com wrote: I can get this summary of a model that I am running: summary(myprobit) Call: glm(formula = Response_Slot ~ trial_no, family = binomial(link = probit), data =

Re: [R] highest and second highest value in row for each combination

2011-02-10 Thread Dennis Murphy
Hi: And yet another: :) library(plyr) top2 - function(x) { y - sort(x[-c(1, 2)], decreasing = TRUE)[1:2] area - rep(x[1], 2) type - rep(x[2], 2) data.frame(area, type, value = y, variable = names(y)) } v - ldply(apply(df, 1, top2), rbind) The last line does the

Re: [R] Where can I download/install grDevices

2011-02-10 Thread Dennis Murphy
Hi: Here's one way: plot(1~1,ylab=expression(Areas (~mu*m^2~))) The tildes incorporate space between the math and text elements; they're optional, but useful. Another way that also works is plot(1~1,ylab=expression(paste(Areas (, mu*m^2, ), sep = ' '))) HTH, Dennis On Thu, Feb 10, 2011 at

Re: [R] Use glm coefficients for other datasets

2011-02-09 Thread Dennis Murphy
Hi: dat - read.table(textConnection( + response pred1 pred2 + 1 1 01 + 2 0 00 + 3 1 00 + 4 1 11 + 5 1 01 + 6 0 11 + 7 1 10 + 8 1 01 + 9 0 11 + 10 0 01 + ),

Re: [R] Need help merging two dataframes

2011-02-09 Thread Dennis Murphy
Hi: Here's one approach to the problem you posed (don't know if it is what you want for the problem you intend you use it on...) df1 - read.table(textConnection( id sexage area 01 male adult LP 01 male adult LP 01 male

Re: [R] wrong length error

2011-02-09 Thread Dennis Murphy
Hi: On Wed, Feb 9, 2011 at 6:29 AM, Claudio Carneiro claudio_carneiro...@hotmail.com wrote: Hi, all I'm trying to solve an optimization problem with two variables, in wich Iuse the following functions(they all are working perfectly): fxx1=function(x){

Re: [R] Fitting a model with an offset in bigglm

2011-02-08 Thread Dennis Murphy
Hi: Did you try putting the offset in the model formula, as in bigglm( y ~ offset(z) + x, ...) ? I haven't tried bigglm() personally (BTW, it's in the biglm package, which wasn't mentioned), but this syntax works in the standard glm() function, so perhaps it maps to bigglm() as well... (?)

Re: [R] Extrcat selected rows from a list

2011-02-08 Thread Dennis Murphy
Hi: Try some modification of the following toy example: df - data.frame(names = as.character(replicate(100, paste(sample(letters, 3), collapse = ''))), x1 = rnorm(100), x2 = rnorm(100)) tgts - sample(df$names, 20)# target list of names df[df$names %in% tgts, ] #

Re: [R] Grouping by factors in R

2011-02-08 Thread Dennis Murphy
Hi: One approach would be to use dlply() from the plyr package to generate the models and assign the results to a list, something like the following: library(plyr) # function to run the GLM in each data subset - the argument is a generic data subset d gfun - function(d) glm(Stems ~ Time, data =

Re: [R] FW: multivariate regression

2011-02-07 Thread Dennis Murphy
Hi: You don't state the test for which you want the p-value, and to reiterate what Dr. Ligges asked in response to your earlier post, how do you propose to define a single R^2 measure? One may be able to answer your question re an overall significance test using the anova() function:

Re: [R] kernel density

2011-02-07 Thread Dennis Murphy
Hi: y - rnorm(50) kd - sm.density(y, model = Normal) str(kd) List of 10 $ eval.points: num [1:100] -3.79 -3.72 -3.64 -3.57 -3.5 ... $ estimate : num [1:100] 0.00124 0.00169 0.00225 0.00295 0.00381 ... $ h : num 0.53 $ h.weights : num [1:50] 1 1 1 1 1 1 1 1 1 1 ... $ weights:

Re: [R] Perform one-way ANOVA using standard deviation and mean

2011-02-07 Thread Dennis Murphy
Hi: You need to check that you have the sufficient statistics necessary to obtain an ANOVA table corresponding to the model you intend to fit. If you have that, you can use the vectorization and matrix capabilities in R to manually get the ANOVA table, perform the F tests, etc. The only thing

Re: [R] Convert the output of by() to a data frame

2011-02-07 Thread Dennis Murphy
Hi: For this particular task, the aggregate() function and the doBy package provide nicely formatted output, but you may have to do some renaming. Let's try a more expansive toy example with which one can do a bit more. df - data.frame(grp1 = rep(c('x', 'y'), each = 40), grp2

Re: [R] partial evaluation of a function with several arguments

2011-02-07 Thread Dennis Murphy
Hi: Another option is to provide a default value of an argument that can be changed by the user: add - function(x, y = 3) x + y add(4) [1] 7 add(4, 2) [1] 6 There are several options at one's disposal - the trick is to figure out what you want to provide to the user of the function. HTH,

Re: [R] aggregate function - na.action

2011-02-06 Thread Dennis Murphy
Hi: There's definitely something amiss with aggregate() here since similar functions from other packages can reproduce your 'control' sum. I expect ddply() will have some timing issues because of all the subgrouping in your data frame, but data.table did very well and the summaryBy() function in

Re: [R] Can an xyplot() plus legend be saved as an Enhanced Windows Metafile (EMF)?

2011-02-04 Thread Dennis Murphy
Hi: This is FAQ 7.22 (Why do lattice/trellis graphics not work?) You need to print() the plot, as in print(xyplot(a ~ b)) This problem also extends to ggplot2 graphics. (BTW, the legend statement after xyplot() won't work because the legend() function is meant for use in base graphics. Lattice

Re: [R] Finding non-normal distributions per row of data frame?

2011-02-04 Thread Dennis Murphy
Hi: The problem you have, IMO, is the multiplicity of tests and the p-value adjustments that need to be made for them. Here's a little simulation using normal and exponential distributions of the size you're contemplating. # Normal data d - matrix(rnorm(40), nrow = 2) # Function to

Re: [R] R-help

2011-02-03 Thread Dennis Murphy
Hi: u - matrix(sample(LETTERS[1:24]), nrow = 8) u [,1] [,2] [,3] [1,] W J M [2,] K V P [3,] X E U [4,] T B N [5,] I Q F [6,] G R S [7,] A C O [8,] D H L t(apply(u, 1, function(x) sort(x))) [,1] [,2] [,3] [1,] J M W [2,] K P V [3,] E U X [4,] B N T [5,] F I Q

Re: [R] R Data Manipulation - Transposing Data by a Given Column, Like User_ID

2011-02-03 Thread Dennis Murphy
Hi: This also works, except that the result is of class 'table': xtabs(COUNTS ~ USER_ID + SITE, data = RAW) SITE USER_ID SITE1 SITE2 SITE3 1101322 2101212 31344 0 4 0 099 If you need a data frame, then the earlier

Re: [R] Finding the maximum in a particular group in a dataframe

2011-02-02 Thread Dennis Murphy
Hi: Using df as your data frame, max(subset(df, condition == 'GPR119a', select = responce)) [1] 0.6451204 You asked this question yesterday with several correct responses. What was wrong with them? Dennis On Wed, Feb 2, 2011 at 12:48 AM, Asan Ramzan asanram...@yahoo.com wrote: Hello I am

Re: [R] exact logistic regression

2011-02-02 Thread Dennis Murphy
Hi: Try package elrm - its description file says that it performs exact logistic regression by MCMC. I found this as the first set of hits I got from # install.packages('sos') # if necessary library(sos) findFn('exact logistic regression') HTH, Dennis On Wed, Feb 2, 2011 at 8:43 AM, Den

Re: [R] Testing whether a number belong to a set

2011-02-02 Thread Dennis Murphy
Hi: Try this: S1 - sample(10001:98999, 1000) S2 - sample(10001:98999, 1000) intersect(S1, S2) # lists have some overlap [1] 72484 82292 93508 50438 29603 28965 68063 79598 16497 43016 22119 # test list x - sample(10001:98999, 1000) # tests first if x is in S1; if not, test whether it's in

Re: [R] multi-Operations on a matrix

2011-02-01 Thread Dennis Murphy
Hi: Try this: d - read.table(textConnection( + procedure property sensor_data sensor_date + 1S_10 nord626821.0 2002-09-30T00:00:00+0200 + 2 S_10 nord626821.0 2002-12-05T00:00:00+0100 + 3S_10 nord626821.1 2008-07-31T00:00:00+0200 + 4

Re: [R] Scatterplot Question

2011-02-01 Thread Dennis Murphy
Hi: Not sure, but perhaps you had something like this in mind: y - runif(100) w - matrix(y, nrow = 10) # fills in column-wise v - apply(w, 2, cumsum) plot(1:100, y, type = 'l', col = 'blue') lines(1:100, as.vector(v), type = 'l', col = 'red') Reshaping into a matrix and using apply() to

Re: [R] Problems with sample means and standard deviations

2011-02-01 Thread Dennis Murphy
Hi: Here are a few ways to do this. A useful approach is to use replicate() to generate the samples, flatten the resulting matrix into a data frame and call one or more packages that are well capable of handling multiple outputs per data subset. Step 1: Generate the samples and rearrange into a

Re: [R] (no subject)

2011-02-01 Thread Dennis Murphy
Hi: Here are three more 'solutions' using packages doBy, plyr and data.table. ### For illustration: maxima for all mouce/GPR* conditions (1) doBy: library(doBy) dfsub - subsetBy( ~ 1, subset = condition != 'con', data = df) summaryBy(responce ~ mouce + condition, data = dfsub, FUN = max)

Re: [R] Finding a Diff within a Dataframe columns

2011-01-31 Thread Dennis Murphy
Hi: This also works on your example data. Using the 'x' data frame from Jim Holtman's post, subset(transform(x, diffAB = A - B, diffCD = C - D), select = c('diffAB', 'diffCD')) diffAB diffCD 1 -0.6 0.10 2 -0.4 0.10 3 -0.4 -0.06 transform() allows you to do the subtractions in one

Re: [R] leap year and order function

2011-01-31 Thread Dennis Murphy
Hi: There is an additional proviso: If the year is divisible by 100, then the next test is whether it's divisible by 400. If both conditions are met, then the century year is a leap year. Hence, 2000 was a leap year because it is divisible by 400, but 2100, 2200 and 2300 will not be. For Perl

<    1   2   3   4   5   6   7   8   9   10   >