[R] [R-pkgs] package JM -- version 1.0-0
Dear R-users, I'd like to announce the release of version 1.0-0 of package JM (already available from CRAN) for the joint modeling of longitudinal and time-to-event data using shared parameter models. These models are applicable in mainly two settings. First, when focus is in the survival outcome and we wish to account for the effect of an endogenous (aka internal) time-dependent covariate measured with error. Second, when focus is in the longitudinal outcome and we wish to correct for nonrandom dropout. Some basic features of JM: * it fits joint models for continuous longitudinal responses and allows for several options for the survival submodel, including PH models with Weibull, piecewise-constant, spline-approximated and unspecified baseline hazard functions. The most complete option is the PH model with the spline-approximated baseline hazard, which also allows for the inclusion of stratification factors, competing risks and (exogenous) time-varying covariates; * it allows for several formulations of the association structure between the longitudinal and survival outcomes; * it computes dynamic individualized predictions for the survival and longitudinal outcomes, which are updated as extra longitudinal information is recorded; * it computes time-dependent sensitivity and specificity, and the corresponding ROCs and AUCs with several options for the prediction rule; * several types of residuals are supported for both outcomes; * fast fitting of these models is facilitated with a pseudo-adaptive Gaussian-Hermite rule. The theory and application of this type of models along with a comprehensive overview of the capabilities of the package can be found in the recently published book Joint Models for Longitudinal and Time-to-Event Data, with Applications in R by Chapman and Hall/CRC (http://www.crcpress.com/product/isbn/9781439872864). The code used in the book and additional material are available in the R-forge web site: http://jmr.r-forge.r-project.org/ Additional information can be found in the corresponding help files, and examples at the R wiki web page devoted to JM: http://rwiki.sciviews.org/doku.php?id=packages:cran:jm As always, any kind of feedback (e.g., questions, suggestions, bug-reports, etc.) is more than welcome. Best, Dimitris -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ ___ R-packages mailing list r-packa...@r-project.org https://stat.ethz.ch/mailman/listinfo/r-packages __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to apply the same function to multiple data set
you will have to check what function pargev() returns as a result. I would guess that is probably a list. In any case, you could use something like the following: estIID50 - lapply(IID50, function (m) pargev(lmom.ub(m))) I hope it helps. Best, Dimitris On 6/27/2012 1:31 PM, Al Ehan wrote: Hi R-users, I'm trying to repeat the same procedure to 1000 data set. I know this is very easy, but I got stuck finding the right and fastest way in running it. IID50=Riidf[1:50,1:1000] #where IID50 is a dataframe consist of 1000 time series(as column) and 50 time scales (row). #what I tried to do: estIID50=rep(NA,1000) for (i in 1:1000) estIID50[i]=pargev(lmom.ub(IID50[1:50,i])) #warning message In estIID50[i] = pargev(lmom.ub(IID50[1:50, i])) : number of items to replace is not a multiple of replacement length #pargev is a function from lmomco package. I would like to apply it to the 1000 set of time series that I have in the IID50, without having to do it manually. #I dont understand what is the warning warns about Can somebody help me? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to apply the same function to multiple data set
try this: sapply(estIID50, [[, 'para') Best, Dimitris On 6/27/2012 2:17 PM, Al Ehan wrote: Sorry, one more simple question. how do I pick, from the generated lapply, the $para for each X. say here I have 2 generated list from previous function. how do I take only the $para for each X? Thank you so much for your kindness. $X1 $X1$type [1] gev $X1$para xi alpha kappa 896.6893825 143.0511714 -0.5062221 $X1$source [1] pargev $X2 $X2$type [1] gev $X2$para xi alpha kappa 955.6826879 160.226 -0.2974729 $X2$source [1] pargev Best, Al On Wed, Jun 27, 2012 at 12:45 PM, Dimitris Rizopoulos d.rizopou...@erasmusmc.nl mailto:d.rizopou...@erasmusmc.nl wrote: you will have to check what function pargev() returns as a result. I would guess that is probably a list. In any case, you could use something like the following: estIID50 - lapply(IID50, function (m) pargev(lmom.ub(m))) I hope it helps. Best, Dimitris On 6/27/2012 1:31 PM, Al Ehan wrote: Hi R-users, I'm trying to repeat the same procedure to 1000 data set. I know this is very easy, but I got stuck finding the right and fastest way in running it. IID50=Riidf[1:50,1:1000] #where IID50 is a dataframe consist of 1000 time series(as column) and 50 time scales (row). #what I tried to do: estIID50=rep(NA,1000) for (i in 1:1000) estIID50[i]=pargev(lmom.ub(__IID50[1:50,i])) #warning message In estIID50[i] = pargev(lmom.ub(IID50[1:50, i])) : number of items to replace is not a multiple of replacement length #pargev is a function from lmomco package. I would like to apply it to the 1000 set of time series that I have in the IID50, without having to do it manually. #I dont understand what is the warning warns about Can somebody help me? [[alternative HTML version deleted]] R-help@r-project.org mailto:R-help@r-project.org mailing list https://stat.ethz.ch/mailman/__listinfo/r-help https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/__posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 tel:%2B31%2F%280%2910%2F7043478 Fax: +31/(0)10/7043014 tel:%2B31%2F%280%2910%2F7043014 Web: http://www.erasmusmc.nl/__biostatistiek/ http://www.erasmusmc.nl/biostatistiek/ -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] data normalization
assuming that the entries for each subject are ordered with respect to TIME and each subject has a measurement at TIME = 0, then you could use the following: Data - read.table(textConnection(ID TIMEDV 1 0 0.880146038 1 1 0.88669051 1 3 0.610784702 1 5 0.75604 2 0 0.456263368 2 1 0.369991537 2 3 0.508798346 2 5 0.441037014 3 0 0.854905349 3 1 0.960457553 3 3 0.609434409 3 5 0.655006334), header = TRUE) closeAllConnections() Data$DVn - with(Data, ave(DV, ID, FUN = function (x) x/x[1])) Data If either of the above assumptions doesn't hold, you'll have to tweak it. I hope it helps. Best, Dimitris On 6/20/2012 2:08 AM, york8866 wrote: I have a dataframe such like the following: ID TIMEDV 1 0 0.880146038 1 1 0.88669051 1 3 0.610784702 1 5 0.75604 2 0 0.456263368 2 1 0.369991537 2 3 0.508798346 2 5 0.441037014 3 0 0.854905349 3 1 0.960457553 3 3 0.609434409 3 5 0.655006334 . . . . . . I would like to generate another column with the normalized values of DV. for each ID, normalize to the value at TIME 0. I was able to use 2 loops to do the normalization, however, as 2 loops took a long time for the calculation, I don't know whether there's a better way to do it. I have the following code with only 1 loop, but it did not work. Can anyone help with this? Thanks, IDS - unique(data.frame$ID) for(WhichID in IDS) { subset - data.frame[,ID] == WhichID DVREAL - data.frame[subset,DV] DVNORM[WhichID] - DVREAL/DVREAL[1] } -- View this message in context: http://r.789695.n4.nabble.com/data-normalization-tp4633911.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to deduplicate records, e.g. using melt() and cast()
you could try aggregate(), e.g., my.df - data.frame(pathway = c(rep(pw.A, 2), rep(pw.B, 3), rep(pw.C, 1)), cond.one = c(0.5, NA, 0.4, NA, NA, NA), cond.two = c(NA, 0.6, NA, 0.9, NA, 0.2), cond.three = c(NA, NA, NA, NA, 0.1, NA)) aggregate(my.df[-1], my.df['pathway'], sum, na.rm = TRUE) or sum. - function(x) if (all(is.na(x))) NA else sum(x, na.rm = TRUE) aggregate(my.df[-1], my.df['pathway'], sum.) I hope it helps. Best, Dimitris On 5/7/2012 11:50 AM, Karl Brand wrote: Esteemed UseRs, This must be embarrassingly trivial to achieve with e.g., melt() and cast(): deduplicating records (pw.X in example) for a given set of responses (cond.Y in example). Hopefully the runnable example shows clearly what i have and what i'm trying to convert it to. But i'm just not getting it, ?cast that is! So i'd really appreciate some ones patience to clarify this, using the reshape package, or any other approach. With sincere thanks in advance, Karl ## Runnable example ## The data.frame i have: library(reshape) my.df - data.frame(pathway = c(rep(pw.A, 2), rep(pw.B, 3), rep(pw.C, 1)), cond.one = c(0.5, NA, 0.4, NA, NA, NA), cond.two = c(NA, 0.6, NA, 0.9, NA, 0.2), cond.three = c(NA, NA, NA, NA, 0.1, NA)) my.df ## The data fram i want: wanted.df - data.frame(pathway = c(pw.A, pw.B, pw.C), cond.one = c(0.5, 0.4, NA), cond.two = c(0.6, 0.9, 0.2), cond.three = c(NA, 0.1, NA)) wanted.df -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Summing list with NA elements
one solution is to set NAs to 0, e.g., m - matrix(1:3, 3, 3) x - list(m, m+3, m+6) x[[1]][1] - NA x. - lapply(x, function (x) {x[is.na(x)] - 0; x} ) Reduce(+, x.) I hope it helps. Best, Dimitris On 5/4/2012 11:19 AM, Evgenia wrote: I have a list ( in my real problem a double list y[[1:24]][[1:15]] but I think the solution would be the same) m- matrix(1:3, 3, 3) x- list(m, m+3, m+6) and as I want to have the sum of elements I use Reduce(`+`, x) having as result Reduce(`+`, x) [,1] [,2] [,3] [1,] 12 12 12 [2,] 15 15 15 [3,] 18 18 18 How can I take the sum of the list elements ignoring NA element For example if I have x[[1]][1]-NA Then I want to have [,1] [,2] [,3] [1,] 11 12 12 [2,] 15 15 15 [3,] 18 18 18 instead of Reduce(`+`, x) [,1] [,2] [,3] [1,] NA 12 12 [2,] 15 15 15 [3,] 18 18 18 Thanks in advance Evgenia -- View this message in context: http://r.789695.n4.nabble.com/Summing-list-with-NA-elements-tp4608167.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Matrix multiplication by multple constants
try this: x - 1:3 y - matrix(1:12, ncol = 3, nrow = 4) y * rep(x, each = nrow(y)) I hope it helps. Best, Dimitris On 4/20/2012 10:51 AM, Vincy Pyne wrote: Dear R helpers Suppose x- c(1:3) y- matrix(1:12, ncol = 3, nrow = 4) y [,1] [,2] [,3] [1,]159 [2,]26 10 [3,]37 11 [4,]48 12 I wish to multiply 1st column of y by first element of x i.e. 1, 2nd column of y by 2nd element of x i.e. 2 an so on. Thus the resultant matrix should be like z [,1] [,2][,3] [1,]11027 [2,]21230 [3,]31433 [4,]41636 When I tried simple multiplication like x*y, y is getting multiplied column-wise x*z [,1] [,2] [,3] [1,]159 [2,]4 12 20 [3,]9 21 33 [4,] 16 32 48 Kindly guide Regards Vincy [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] expand.grid using a matrix and a vector as input
One possibility is to use something like the following: a1 - matrix(1:4, 2, 2) a2 - c(8, 9) cbind(a1[rep(1:nrow(a1), length(a2)), ], rep(a2, each = nrow(a1))) I hope it helps. Best, Dimitris On 3/15/2012 1:33 PM, eugen pircalabelu wrote: Hello R-users, I have the following question, for which my search did not really return any usable result. If I have a matrix a1, and a vector a2 like below a1-matrix(c(1:4),2,2) a2-c(8,9) is there any function like the expand.grid (or some clever calling of the function) such that it outputs a matrix or dataframe where the entire a1 matrix is repeated for each value of a2 (the rbind and cbind solution is not something that i am after). The desired output is: out= 1 3 8 2 4 8 1 3 9 2 4 9 Thank you in advance and have a great day ahead! Eugen [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Numerical Inversion of Cumulative Distribution Function
Check function uniroot(). I hope it helps. Best, Dimitris On 3/6/2012 7:47 PM, Gildas Mazo wrote: Dear R users, Given a user-defined cumulative distribution function F, I want to compute F^{-1}(x). How is that possible with R? Best Regards, -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Function for Generating all Permutations with Repetition
Have a look at expand.grid(), e.g., expand.grid(rep(list(0:1), 3)) I hope it helps. Best, Dimitris On 3/5/2012 9:46 AM, Martin Spindler wrote: Dear all, I am looking for a function in R which returns all possible permutations of an object x with r number of repitions. For example If x- c(0,1) and r-3 the result should be 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 and consist of 2^3=8 elements. Unfortunately, I have found only functions which return the combinations for choose n over k but not the case described above. I would appreciate hints and help highly. Best, Martin -- __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] split array into groups by value
One approach is: l - c(1,1,1,1, 0,0,0,0,0, 1,1,1,1,1,1) r - rle(l)$lengths split(l, rep(seq_along(r), r)) I hope it helps. Best, Dimitris On 3/6/2012 3:53 AM, pavlo wrote: I have an array l- c(1,1,1,1, 0,0,0,0,0, 1,1,1,1,1,1) I would like to get a rugged array [[1]] 1,1,1,1 [[2]] 0,0,0,0,0 [[3]] 1,1,1,1,1,1 catching every group of contiguous repeated values. Any help would be greatly appreciated! -Pavel -- View this message in context: http://r.789695.n4.nabble.com/split-array-into-groups-by-value-tp4448578p4448578.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Two plots with two different Y labels
Try this: set.seed(123) x - 1:5 y1 - rnorm(5) y2 - rnorm(5) par(mar = c(5,4,5,4)) plot(x, y1, xlab = x-axis, ylab = y-axis, type = l) par(new = TRUE) plot(x, y2, axes = FALSE, yaxs = i, type = l, ann = FALSE, col = 2) axis(4, las = 2) mtext(z-axis, 4, 2) I hope it helps. Best, Dimitris On 3/1/2012 9:13 AM, Alaios wrote: Dear all, I would like to place into a same plot two plots. Their y values though are different a lot (first series has around -100 y values and the second one slightly over 1). The x value are the same. I would like to have in the same plot (under the same x,y axis the two plots). Is it possible then to have two y labels, one for example in the left side and an extra y label at the right. Each y label will have its own labeling and its own Font type line. CAn I do something like that in R with the normal plots and lines? I would like to thank you in advance for your help B.R Alex [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Column wise matrix multiplication
Try apply(A, 1, prod) I hope it helps. Best, Dimitris On 2/20/2012 4:21 PM, Graziano Mirata wrote: Hi all, I am trying to multiply each column of a matrix such to have a unique resulting vector with length equal to the number of rows of the original matrix. In short I would like to do what prod(.) function in Matlab does, i.e. A-matrix(c(1:10),5,2) V = A[,1]*A[,2] Thank you Graziano [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Selecting elements from all items in a list
How about: l - list() l[[1]] - c(A1,A2,A3) l[[2]] - c(B1,B2,B3) l[[3]] - c(C1,B2,B3) lapply(l, [[, 2) or sapply(l, [[, 2) I hope it helps. Best, Dimitris On 2/14/2012 2:44 PM, geotheory wrote: Basic question (if you know the answer)... I am dealing with a list of commonly-formatted sub-lists, for example: l- list() l[[1]]- c(A1,A2,A3) l[[2]]- c(B1,B2,B3) l[[3]]- c(C1,B2,B3) Lets say I need to extract every 2nd item (i.e. A2, B2, C2). [[]] cannot be used. l[2] returns practically the same as l[[2]]. Is there a way to achieve this without having to loop or convert to data.frame? Thanks in advance. -- View this message in context: http://r.789695.n4.nabble.com/Selecting-elements-from-all-items-in-a-list-tp4387045p4387045.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Vector manipulation
One way is: vec - c(2,4,6,9,10) c(rbind(vec[1], vec[-1])) I hope it helps. Best, Dimitris On 2/12/2012 6:54 PM, syrvn wrote: Hello, I am stuck with the following problem. Consider the vector: vec- c(2,4,6,9,10) I now want to use R to manipulate the vector as follows: [1] 2, 4, 2, 6, 2, 9, 2, 10 In words, the first element of the vector should be placed in front of each following number. Which R commands do I need to achieve that? Cheers -- View this message in context: http://r.789695.n4.nabble.com/Vector-manipulation-tp4381586p4381586.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Find interval between numbers in list or vector
Have a look at function diff(), e.g., diff(c(1,4,10,30)) I hope it helps. Best, Dimitris On 2/10/2012 1:33 PM, alend wrote: hi, I have a vector as follow: myVec = c(1,4,10,30) How can I get a vector that show the interval of the numbers. I need to get this result: distance 3,6,20 -- View this message in context: http://r.789695.n4.nabble.com/Find-interval-between-numbers-in-list-or-vector-tp4376115p4376115.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] remove NAs from list collectively
Two possibilities are: lapply(df, function (x) x[!is.na(x)]) and lapply(df, na.exclude) I hope it helps. Best, Dimitris On 2/8/2012 11:54 AM, Johannes Radinger wrote: Hi, I am importing dataframe from an Excel file (xlsx package). The columns contain acutally measurements for single species and the column-length is of variable. As it is imported as a dataframe the difference to the longest column is filled with NA. To explain it with an example, my dataframe looks like: A- seq(1:10) B- c(seq(1:5),rep(NA,5)) C- c(seq(1:7),rep(NA,3)) df- data.frame(A,B,C) Now I'd like to transform that to a list of vectors of different length. Therefore I need to remove the NAs collectively from the single columns...I tried for transforming: as.list(df) ...but I don't know how can I remove the NAs now? as.list doesn't take na.rm=TRUE argument. Is there any ready function to perform such tasks? Or is there a better way then to assign the data to a list of vectors with variable length? /johannes -- __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] check for nearest value in a vector
How about this: x - c(1, 6, 12, 28, 33) which.min(abs(x - 14)) I hope it helps. Best, Dimitris On 2/8/2012 1:56 PM, Martin Batholdy wrote: Hi, Is there a way to check which value in a vector is nearest to a given value? so for example I have vector x: x- c(1, 6, 12, 28, 33) and I would like to get the position of the element of x that is nearest to 14 (in this case the third element). thanks! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] logical subsetting, indexes and NAs
how about x[x 10 !is.na(x)] I hope it helps. Best, Dimitris On 1/28/2012 9:36 PM, Federico Calboli wrote: Dear All, just a quick example: x = 1:25 x[12] = NA x [1] 1 2 3 4 5 6 7 8 9 10 11 NA 13 14 15 16 17 18 19 20 21 22 23 24 25 y = x[x10] y [1] 1 2 3 4 5 6 7 8 9 NA Is there any way of NOT getting NA for y = x[x10]? Similarly y = x[x15] y [1] 1 2 3 4 5 6 7 8 9 10 11 NA 13 14 How do I get rid of the NA (not post hoc)? BW F -- Federico C. F. Calboli Neuroepidemiology and Ageing Research Imperial College, St. Mary's Campus Norfolk Place, London W2 1PG Tel +44 (0)20 75941602 Fax +44 (0)20 75943193 f.calboli [.a.t] imperial.ac.uk f.calboli [.a.t] gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Return the matrix location of multiple entries
On 1/23/2012 8:40 PM, David Winsemius wrote: On Jan 23, 2012, at 2:30 PM, Petr Savicky wrote: On Mon, Jan 23, 2012 at 01:08:03PM -0500, R. Michael Weylandt wrote: I'd do something like apply(subER, 1, function(x) which(x %in% sort(x)[1:4])) E.g. subER - matrix(sample(100), 10) Hi. This is OK, if there are four smallest values, which are different from the rest. For the first row in subER - rbind(c(1, 2, 2, 3, 3, 3, 5, 6), 8:1) the function determines the bound 3 and returns the indices of the 6 positions with 1, 2, 3 from the first row. So, the result is not a matrix, but a list. apply(subER, 1, function(x) which(x %in% sort(x)[1:4])) [[1]] [1] 1 2 3 4 5 6 [[2]] [1] 5 6 7 8 The following solves ties by choosing the smaller index. apply(subER, 1, function(x) order(x)[1:4]) [,1] [,2] [1,] 1 8 [2,] 2 7 [3,] 3 6 [4,] 4 5 If the indices should be ordered, then try the following apply(subER, 1, function(x) sort(order(x)[1:4])) [,1] [,2] [1,] 1 5 [2,] 2 6 [3,] 3 7 [4,] 4 8 And if only the lowest four instances were desired (in ascending order) then this would work (and I transposed to bring back to the original structure): t(apply(subER, 1, function(x) x[order(x)][1:4])) [,1] [,2] [,3] [,4] [1,] 1 2 2 3 [2,] 1 2 3 4 Yet another solution is: subER - matrix(sample(100), 10) matrix(subER[order(row(subER), subER)], ncol = ncol(subER), byrow = TRUE)[, 1:4] Cheers, Dimitris -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Removing rows in dataframe w'o duplicated values
one approach is the following: dat - data.frame(id = c(1,1,1,2,3,3), value = c(5,6,7,4,5,4), value2 = c(1,4,3,3,4,3)) ind - ave(dat$id, dat$id, FUN = length) 1 dat[ind, ] I hope it helps. Best, Dimitris On 11/22/2011 6:43 PM, AC Del Re wrote: Hi, Is there an easy way to remove dataframe rows without duplicated values of a specified column ('id')? e.g., dat- data.frame(id = c(1,1,1,2,3,3), value = c(5,6,7,4,5,4), value2 = c(1,4,3,3,4,3)) dat id value value2 1 1 5 1 2 1 6 4 3 1 7 3 4 2 4 3 5 3 5 4 6 3 4 3 This is sample data and the real data has hundreds of rows. In this case, only row 4 does not have a duplicated id and I would like to remove it without using: dat$id[4]- NULL Any help is appreciated! AC [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] ltm: Simplified approach to bootstrapping 2PL-Models?
Note that you can also fit 2PL models using the gpcm() function that fits the Generalized Partial Credit Model, and therefore you can use the GoF.gpcm() function for goodness-of-fit, e.g., library(ltm) fit2PL.1 - ltm(LSAT ~ z1) fit2PL.2 - gpcm(LSAT) fit2PL.1 fit2PL.2 GoF.gpcm(fit2PL.2, B = 199) I hope it helps. Best, Dimitris On 11/20/2011 1:16 PM, Christian Montel wrote: Dear R-List, to assess the model fit for 2PL-models, I tried to mimic the bootstrap-approach chosen in the GoF.rasch()-function. Not being a statistician, I was wondering whether the following simplification (omit the chi-squared-expressed model fit-step) would be appropriate: GoF.ltm- function(object, B = 50, ...){ liFits- list() for(i in 1:B){ rndDat- rmvlogis(nrow(object$X), coef(object)) liFits[[i]]- ltm(rndDat ~ z1) } distr- sort(sapply(liFits, function(x)return(x$log.Lik))) return(max(which(distr= object$log.Lik))/length(distr)) } The rationale behind was to directly use the sorted sequence of (log)likelihoods of models fitted to 2PL-fitting-datasets where the parameters ofobject hold. The return value was intented to roughly mirror how many 2PL-Datasets which demonstrably fit the model fit worse or better than the model in question. Any comments which may help me figure out whether I'm on the right track are greatly appreciated. Thank you in advance, best regards, Christian Dr. Christian Montel E-Mail: christian.mon...@eligo.de eligo GmbH -- Büro Berlin Tel.: +49 (0) 30 695 399 95-2 Arndtstr. 34 Fax:+49 (0) 30 695 399 95-1 10965 Berlin __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Estimating IRT models by using nlme() function
__ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Generate a sequence of vectors of different length
One approach is: sectors - 2 namSec - LETTERS[seq_len(sectors)] nSec - round(3 / runif(sectors)) mapply(paste, namSec, sapply(nSec, seq_len), MoreArgs = list(sep = )) I hope it helps. Best, Dimitris On 11/2/2011 2:56 PM, Christian Langkamp wrote: Hi everyone After the following setup sector=2 # Define Number of Sectors sectors=LETTERS[seq( from = 1, to = sector )] # Name sectors No_ent=round(3/runif(sector)) # Number of entities per sector #Tot_No_ent=sum(No_ent) Goal is to get a List like (A1, A2, A3, B1, B2, B3, B4) where A is denoting an industrial sector and then a numbered sequence of companies within the sector. The step I am missing is how to generate a sequence of vectors (one for each sector) with individual length being determined by No_ent. The goal is to generate a set of entities from different sectors. One simple way out of it would be to set the number of entities equal per sector and have a matrix, but I am quite sure it should also be possible for having a different number of entities in each sector. Once this is done, I can bind them together as vector with as.vector(rbind(?)) (both as an (A,A,A,B,B,B,B) and (1,2,3,1,2,3,4) and then concatenate) Thanks, Christian Trials included the following bits A=for (i in 1:sector){ rep(i,No_ent[i]) } paste(LETTERS[i], seq(from =1, to =No_ent[i]), sep = ) but I don't get the correct object definition right. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Calculate the difference using ave
Maybe one approach could be: set.seed(123) df1 - data.frame(measure = rep(c(A1, A2, A3), each=3), water = sample(c(100:200), 9), tide = sample(c(-10:+10), 9)) 100 * tail(df1$tide, -3) / head(df1$water, -3) I hope it helps. Best, Dimitris On 10/26/2011 12:02 PM, Patrick Hausmann wrote: Dear R users, It may be very simple but it is being difficult for me. I'd like to calculate the difference in percent between to measures. My data looks like this: set.seed(123) df1 - data.frame(measure = rep(c(A1, A2, A3), each=3), water = sample(c(100:200), 9), tide = sample(c(-10:+10), 9)) df1 # What I want to calculate is: # tide_[A2] / water_[A1], # tide_[A3] / water_[A2] # This 'works' for the example, but I am # looking for a more general solution. df1$tide_diff - ave(df1$tide, FUN=function(L) L / c(NA, NA, NA, df1$water)) * 100 df1 Thanks for any help! Patrick __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Create order of numbers based on a given vector
try this: vec - c(FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE) cumsum(!vec) I hope it helps. Best, Dimitris On 10/13/2011 1:15 PM, syrvn wrote: Hello! If I have a vector vec- c(FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE FALSE) I can I create the following order of numbers based on vector vec: 1, 2, 2, 3, 3, 3, 4, 5 Whenever there is a FALSE I increase the number (starting with 1). Whenever there is a TRUE I set the same number as the previous FALSE has been assigned to. I would be happy for any input Cheers, Syrvn -- View this message in context: http://r.789695.n4.nabble.com/Create-order-of-numbers-based-on-a-given-vector-tp3901158p3901158.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] stop()
You could use return(), e.g., myFun - function (x, max.iter = 5) { for (i in 1:10) { result - x + i iter - i if (iter == max.iter) { return(result) } } result } myFun(10, max.iter = 4) I hope it helps. Best, Dimitris On 10/11/2011 7:31 PM, Doran, Harold wrote: Suppose I have a function, such as the toy example below: myFun- function(x, max.iter = 5) { for(i in 1:10){ result- x + i iter- i if(iter == max.iter) stop('Max reached') } result } I can of course do this: myFun(10, max.iter = 11) However, if I reach the maximum number of iterations before my algorithm has finished (in my real application there are EM steps for a mixed model), I actually want the function to return the value of result up to that point. Currently using stop(), I would get myFun(10, max.iter = 4) Error in myFun(10, max.iter = 4) : Max reached But, in this toy case the function should return the value of result up to iteration 4. Not sure how I can adjust this. Thanks, Harold [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] generate sequences for every element of a vector
One option is: x - c(3, 8, 14, 24, 34, 40) c(sapply(x, seq, length = 3)) I hope it helps. Best, Dimitris On 10/7/2011 3:40 PM, Martin Batholdy wrote: Dear R-List, I have the following problem: I have a vector that looks like this: x- c(3, 8, 14, 24, 34, 40) And I would like to make a sequence out of every element, starting at the value of this element. So that I get a vector just like this: x- c(3, 4, 5, 8, 9, 10, 14, 15, 16, 24, 25, 26, 34, 35, 36, 40, 41, 42) How can I do this without looping over the whole vector or something like this? thanks for any suggestions! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Concecutive zeros and ones
Check function rle(). I hope it helps. Best, Dimitris On 10/6/2011 9:18 AM, Alaios wrote: Dear all, I have a data series (might be vector or matrix) which is composed only from zeros and ones like the following example 0 0 0 1 1 0 1 0 0 1 1 1 1 0 0 0 I want to be able to return back the length of concecutive zeros and the length of concecutive ones. For that I want to have something like that returned: zeros= [3 1 2 3]; ones=[2 1 4]; How I can do that simply in R? I would like to thank you in advance for your help B.R Alex [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Duplicate elements of a vector
Check function rep(), e.g., rep(c(1, 5, 23), each = 2) I hope it helps. Best, Dimitris On 10/6/2011 8:58 PM, syrvn wrote: Hi, let's assume I have the following vector a: 1 5 23 How can I use R to duplicate the elements so that my new vector looks like: 1 1 5 5 23 23 Many thanks, Syrvn -- View this message in context: http://r.789695.n4.nabble.com/Duplicate-elements-of-a-vector-tp3879561p3879561.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] [R-pkgs] package JM -- version 0.9-0
Dear R-users, I'd like to announce the release of the new version of package JM (soon available from CRAN) for the joint modeling of longitudinal and time-to-event data using shared parameter models. These models are applicable in mainly two settings. First, when focus is in the survival outcome and we wish to account for the effect of an endogenous (aka internal) time-dependent covariate measured with error. Second, when focus is in the longitudinal outcome and we wish to correct for nonrandom dropout. New features include: * jointModel() with option spline-PH-aGH for the 'method' argument can now also handle competing risks settings. * jointModel() with option spline-PH-aGH for the 'method' argument can now also handle exogenous time-dependent covariates, using the (start, stop] notation of coxph(). * The predict method allows now for both marginal and subject-specific predictions for the longitudinal outcome. * The pseudo adaptive Gauss-Hermite rule is now used by default. More information can be found in the corresponding help files, and examples at http://rwiki.sciviews.org/doku.php?id=packages:cran:jm As always, any kind of feedback (e.g., questions, suggestions, bug-reports, etc.) is more than welcome. Best, Dimitris -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ ___ R-packages mailing list r-packa...@r-project.org https://stat.ethz.ch/mailman/listinfo/r-packages __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] multiplying list with vector
One option is mapply(), e.g., mylist - list(v1=c(1,2,2,1), v2=c(2,2,2,1), v3=c(1,1,1,1)) myvector - c(100, 1, 100) mapply(*, mylist, myvector) mapply(*, mylist, myvector, SIMPLIFY = FALSE) I hope it helps. Best, Dimitris On 9/29/2011 11:43 AM, Marion Wenty wrote: hello everybody, i have got a question about lists: i have got the following commands: mylist- list(v1=c(1,2,2,1),v2=c(2,2,2,1),v3=c(1,1,1,1)) myvector- c(100,1,100) now i would like to multiply each element of the list with the corresponding element of the vector, that is to say: v1*100 v2*1 v3*100 i only could think of lapply which can only be used to multiply each element of the list with the same number, i think. thank you very much in advance for your help, marion [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] need help on melt/cast
You can use function reshape(), e.g., DF - data.frame(ID = LETTERS[1:3], T0 = c(1,4,7), T1 = c(2,5,8), T2 = c(3,6,9)) DF.new - reshape(DF, idvar = ID, direction = long, varying = list(2:4), times = names(DF[-1])) DF.new DF.new[order(DF.new$ID), ] I hope it helps. Best, Dimitris On 9/22/2011 3:54 PM, Eugene Kanshin wrote: Hello, I need to convert dataframe from: ID T0 T1 T2 A1 2 3 B4 5 6 C7 8 9 to: ID Variable Value A T0 1 A T1 2 A T2 3 B T0 4 B T1 5 B T2 6 C T0 7 C T1 8 C T2 9 i tried to use melt cast but it gives me all the time not exactly what I need. Thank you. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] pasting elements of one character vector together
Try this: object - c(Hello, World) paste(object, collapse = ) I hope it helps. Best, Dimitris On 9/19/2011 3:58 PM, Marion Wenty wrote: hello, i am familiar with the paste command with which i can paste for exaple: object- Hello paste(object,World) now i would like to be able to paste all the elements of the same vector together e.g: object- c(Hello,World) getting as a result also: Hello World. Does anyone know the solution to this problem? Thank you very much in advance. Marion [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Automated generation of combinations
one option is the following: varNames - c(varA, varB, varC, varD) f - function (i) { combn(length(varNames), i, function (x) paste(varNames[x], collapse = + )) } lapply(seq_along(varNames), f) However, in case you're interested in performing a linear regression with these subsets, then have also a look at package leaps (http://CRAN.R-project.org/package=leaps). Best, Dimitris On 9/12/2011 11:20 AM, Santiago Guallar wrote: Hello, I'd like to generate automatically all the possible combinations of a set of 8 variables (there are 535, too many to do it by hand). For example: input: varA, varB, varC output: varA+varB+varC varA+varB varA+varC varB+varC varA varB varC Is there any function that produces this option? Thank you [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Regression coefficient constraints
One option is to reparameterize to an unconstrained problem using the transformation: exp(betas_i) / sum(exp(betas_i)), with e.g., beta_1 = 0, and then use optim() to maximize with respect to betas. I hope it helps. Best, Dimitris On 9/4/2011 7:50 AM, andre bedon wrote: Hi Guys, Does anyone know how I could constrain my regression coefficients so that they are positive and add up to one? Any help will be greatly appreciated. Kind Regards, Andre [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Classifying values by interval
Probably you're looking for function findInterval(). I hope it helps. Best, Dimitris On 8/31/2011 10:00 AM, Ted Harding wrote: Greetings All! As is often the case on this list, the answer may well be under my nose but I can't see it! I am looking for a smart way to do the following. Say I have a vector of values, X. I set up bins for X, say with breaks at B = c(b1,b2,...,b11) covering the range of X, i.e. bins numbered 1:10. The value x is in bin i if B[i] x= B[i+1] What I seek is a vector, of the same length as X, which for each x in X gives the number of the bin that x is in. Clearly this can be done in an unsmart way by looping through all of X along with something like which( (B[1:10] X[j]) (X[j]= B[2:11]) ) However, I feel that this naturally occurring task must have received a smarter solution! The hist() function already does this implicitly, since it has to decide which bin a value in X should be counted in. But it apparently then discards this information, since there is nothing relevant in the return values from hist(). So is there a smart function somewhere for this? The motivation here is that I have multivariate data, (X,Y,Z,...) and I wish to study how it behaves in each different bin for X. So the bin index, ixB aY, derived for X can be applied to select corresponding subsets of the other variables. Rather than doing it the clumsy way each time, e.g. according to Y[(B[i] X) (X= B[j+1])] I would like to have the bin index permanently available -- for example it allows easy logical combinations of bins, such as Y[(ixB==j1) | (ixB==j2)], or Y[(ixB %in% ixB0)]. With thanks, Ted. E-Mail: (Ted Harding)ted.hard...@wlandres.net Fax-to-email: +44 (0)870 094 0861 Date: 31-Aug-11 Time: 09:00:27 -- XFMail -- __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] assign group letters to T/F matrix
Have a look at package multcomp at: http://cran.r-project.org/package=multcomp I hope it helps. Best, Dimitris On 8/31/2011 2:30 PM, eldor ado wrote: dear R community, i appologize as this is kind of a newbie question, but: I want to assign group letters (like in anova post-hoc tests) to data with combined means and SDs. It's easy to apply t-test formulas to get a materix like (A - D are treatments | A B C D --- A| TF F B| T F C| F D| where true stands for sign. differences/ F for no sign. differences. is there an alogarithm that converts such a matrix to letters to be assignedto A, B, C,... ? and does maybe somebody know a packages that runs t-tests on means+SD+n data? lukas kohl __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] splitting into multiple dataframes and then create a loop to work
You can do this using function lmList() from package nlme, without having to split the data frames, e.g., library(nlme) mlis - lmList(yvar ~ . - clvar | clvar, data = df) mlis summary(mlis) I hope it helps. Best, Dimitris On 8/29/2011 5:37 PM, Nilaya Sharma wrote: Dear All Sorry for this simple question, I could not solve it by spending days. My data looks like this: # data set.seed(1234) clvar- c( rep(1, 10), rep(2, 10), rep(3, 10), rep(4, 10)) # I have 100 level for this factor var; yvar- rnorm(40, 10,6); var1- rnorm(40, 10,4); var2- rnorm(40, 10,4); var3- rnorm(40, 5, 2); var4- rnorm(40, 10, 3); var5- rnorm(40, 15, 8) # just example df- data.frame(clvar, yvar, var1, var2, var3, var4, var5) # manual splitting df1- subset(df, clvar == 1) df2- subset(df, clvar == 2) df3- subset(df, clvar == 3) df4- subset(df, clvar == 4) df5- subset(df, clvar == 5) # i tried to mechanize it * for(i in 1:5) { df[i]- subset(df, clvar == i) } I know it should not work as df[i] is single variable, do it did. But I could not find away to output multiple dataframes from this loop. My limited R knowledge, did not help at all ! * # working on each of variable, just trying simple function a- 3:8 out1- lapply(1:5, function(ind){ lm(df1$yvar ~ df1[, a[ind]]) }) p1- lapply(out1, function(m)summary(m)$coefficients[,4][2]) p1- do.call(rbind, p1) My ultimate objective is to apply this function to all the dataframes created (i.e. df1, df2, df3, df4, df5) and create five corresponding p-value vectors (p1, p2, p3, p4, p5). Then output would be a matrix of clvar and correponding p values clvar var1 var2 var3 var4 var5 1 2 3 4 Please help me ! Thanks NIL [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] splitting into multiple dataframes and then create a loop to work
well, if a pooled estimate of the residual standard error is not desirable, then you just need to set argument 'pool' of lmList() to FALSE, e.g., mlis - lmList(yvar ~ . - clvar | clvar, data = df, pool = FALSE) summary(mlis) Best, Dimitris On 8/29/2011 9:20 PM, Dennis Murphy wrote: Hi: Dimitris' solution is appropriate, but it needs to be mentioned that the approach I offered earlier in this thread differs from the lmList() approach. lmList() uses a pooled measure of error MSE (which you can see at the bottom of the output from summary(mlis) ), whereas the plyr approach subdivides the data into distinct sub-data frames and analyzes them as separate entities. As a result, the residual MSEs will differ between the two approaches, which in turn affects the significance tests on the model coefficients. You need to decide which approach is better for your purposes. Cheers, Dennis On Mon, Aug 29, 2011 at 12:02 PM, Dimitris Rizopoulos d.rizopou...@erasmusmc.nl wrote: You can do this using function lmList() from package nlme, without having to split the data frames, e.g., library(nlme) mlis- lmList(yvar ~ . - clvar | clvar, data = df) mlis summary(mlis) I hope it helps. Best, Dimitris On 8/29/2011 5:37 PM, Nilaya Sharma wrote: Dear All Sorry for this simple question, I could not solve it by spending days. My data looks like this: # data set.seed(1234) clvar- c( rep(1, 10), rep(2, 10), rep(3, 10), rep(4, 10)) # I have 100 level for this factor var; yvar- rnorm(40, 10,6); var1- rnorm(40, 10,4); var2- rnorm(40, 10,4); var3- rnorm(40, 5, 2); var4- rnorm(40, 10, 3); var5- rnorm(40, 15, 8) # just example df- data.frame(clvar, yvar, var1, var2, var3, var4, var5) # manual splitting df1- subset(df, clvar == 1) df2- subset(df, clvar == 2) df3- subset(df, clvar == 3) df4- subset(df, clvar == 4) df5- subset(df, clvar == 5) # i tried to mechanize it * for(i in 1:5) { df[i]- subset(df, clvar == i) } I know it should not work as df[i] is single variable, do it did. But I could not find away to output multiple dataframes from this loop. My limited R knowledge, did not help at all ! * # working on each of variable, just trying simple function a- 3:8 out1- lapply(1:5, function(ind){ lm(df1$yvar ~ df1[, a[ind]]) }) p1- lapply(out1, function(m)summary(m)$coefficients[,4][2]) p1- do.call(rbind, p1) My ultimate objective is to apply this function to all the dataframes created (i.e. df1, df2, df3, df4, df5) and create five corresponding p-value vectors (p1, p2, p3, p4, p5). Then output would be a matrix of clvar and correponding p values clvar var1 var2 var3 var4 var5 1 2 3 4 Please help me ! Thanks NIL [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to generate a random variate that is correlated with a given right-censored random variate?
Perhaps it could help if you could assume a more flexible model for [T]; for instance, a PH model with a piecewise-constant baseline hazard, which you can simulate as a Poisson variable. Best, Dimitris On 8/26/2011 10:45 PM, Ravi Varadhan wrote: Hi, Here is an update. I implemented this bivariate lognormal approach. It works well in simulations when I generated the marginal [T] from a lognormal distribution and independently censored it. It, however, does not do well when I generate from a marginal [T] that is Weibull or a distribution not well approximated by a lognormal. How to make the approach more robust to distributional assumptions on the marginal of [T]? I am thinking that a bivariate copula approach is called for. The [U] margin can be standard lognormal as before, and the [T] margin needs to be a flexible distribution. What kind of bivariate coupla might work? Then, how to generate from the conditional distribution [U | T]? Any thoughts? Thanks, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edumailto:rvarad...@jhmi.edu From: Ravi Varadhan Sent: Friday, August 26, 2011 2:56 PM To: r-help@r-project.org Subject: How to generate a random variate that is correlated with a given right-censored random variate? Hi, I have a right-censored (positive) random variable (e.g. failure times subject to right censoring) that is observed for N subjects: Y_i, I = 1, 2, ..., N. Note that Y_i = min(T_i, C_i), where T_i is the true failure time and C_i is the censored time. Let us assume that C_i is independent of T_i. Now, I would like to generate another random variable U_i, I = 1, 2, ..., N, which is correlated with T. In other words, I would like to generate U from the conditional distribution [U | T=t]. One approach might be to assume that the joint distn [T, U] is bivariate lognormal. So, the marginals [T] and [U], as well as the conditional [U | T] are also lognormal. I can estimate the marginal [T] using the right-censored data Y (assuming independent censoring). For example, I might use survival::survreg to do this. Then, I assume that U is standard lognormal (mean = 0, var = 1). Now, I only need to assume a value for correlation parameter, r, and I can then sample from the conditional [U | T=t] which is also a lognormal (parametrized by r). Does this sound right? Are there better/simpler ways to do this? Thanks very much for any hints. Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edumailto:rvarad...@jhmi.edu [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Bivariate normal regression in R
One possibility is function gls() from package nlme. Best, Dimitris On 8/25/2011 8:22 PM, DHIMAN BHADRA wrote: Hello everyone, I need to fit a bivariate normal regression model to a dataset where the same covariate (say, X) influences two separate but correlated responses (say, Y1 and Y2). So, the bivariate model would look like : Y1 = a1 + b1*X + e1 Y2 = a2 + b2*X + e2 where e1 and e2 are error terms which can be correlated. Is there any package in R which can help me fit this model ? Any help will be appreciated. Best regards, Dhiman Bhadra Worcester Polytechnic Institute, 100 Institute Road, Worcester, Ma : 01609. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Passing a large amount of parameters to a function
You could use something like the following: paramsList - list(p1 = 1:10, p2 = -5:5, p3 = 5, p4 = 8) params - unlist(as.relistable(paramsList)) myFunc - function (params) { params - relist(params, skeleton = paramsList) p1 - params$p1 p2 - params$p2 p3 - params$p3 p4 - params$p4 ... } For another example have a look at the online help file of relist(). I hope it helps. Best, Dimitris On 8/24/2011 9:28 AM, Eran Eidinger wrote: Hello, I have a function with a long list of parameters (of different types, numeric and string) myFunc-function(p1, p2, p3, p4, p5...etc) { do.something(p1,p2,) } I want to loop over this to provide a different set of parameters to the list every time. for (ii in 1:N) { myFunc(p1(ii), p2(ii),etc) } I would like to simplify the notation and use some kind of structure, maybe a list or dataframe. However when I try, for example, the following: params = data.frame(cbind(p1=c(1,2,3), p2=5, p3=c(3,2,1))) myFunc-function(params) { attach(params) do.something(p1,p2,) detach(params) } for (ii in 1:N) { myFunc(params[ii,]) } do.something fails because p1, p2,... are now factors, and i have to as.numeric() or as.char() each parameter before applying mathematical operators (such as simple multiplication). This defeats the purpose of simplifying the code. Is there any decent, simple way of doing this? Can I unfactor a row of a data.frame? or a list? (BTW, as.numeric() won't work since some of the parameters are strings, same goes for stringsAsFactors in data.frame) Thanks, Eran [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] hide row and column-names in cat()
try function write.matrix() from package MASS, e.g., library(MASS) write.matrix(my.df) I hope it helps. Best, Dimitris On 8/24/2011 1:46 PM, Tim Haering wrote: Dear list, to prepare data as input for a stand-alone groundwater model I use the cat() function to print data. I want to print a table with print() or cat() but without the row- and column-names. Is this possible? Here a small example my.df- rbind(c(Nr,FraSand,FraSilt,FraClay,pH), c(, (kg.kg-1),(kg.kg-1), (kg.kg-1), (-)), c(1, 19, 60, 21, 7.1), c(2, 35, 53, 11, 7.7)) print(my.df, quote=FALSE) [,1] [,2] [,3] [,4] [,5] [1,] Nr FraSand FraSilt FraClay pH [2,] (kg.kg-1) (kg.kg-1) (kg.kg-1) (-) [3,] 11960217.1 [4,] 23553117.7 What I want to have is this: Nr FraSand FraSilt FraClay pH (kg.kg-1) (kg.kg-1) (kg.kg-1) (-) 11960217.1 23553117.7 Thank you. TIM __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Correlation discrepancy
well, you don't have the correct denominator, i.e., n-1, with n denoting the sample size. Have a look at the *Details* section of the online help file for cov(), and try also sum((x-mean(x))*(y-mean(y)))/7 cov(x, y) I hope it helps. Best, Dimitris On 8/23/2011 1:18 PM, Vincy Pyne wrote: Dear R list, I have one very elementary question regrading correlation between two variables. x = c(44,46,46,47,45,43,45,44) y = c(44,43,41,41,46,48,44,43) cov(x, y) [1] -2.428571 However, if I try to calculate the covariance using the formula as covariance = sum((x-mean(x))*(y-mean(y)))/8 # no of of paired obs. = 8 or covariance = sum(x*y)/8-(mean(x)*mean(y)) gives covariance = 2.125 I am not able to figure out where I am going wrong w.r.t. the covariance formula. Kindly guide. Regards Vincy [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to apply a function to subsets of a data frame *and* obtain a data frame again?
Have a look at function ave(), e.g., set.seed(1) (df - data.frame(Group=rep(c(Group1,Group2,Group3), each=10), Value=c(rexp(10, 1), rexp(10, 4), rexp(10, 10)))[sample(1:30,30),]) edf - function(x) ecdf(x)(x) df$edf - with(df, ave(Value, Group, FUN = edf)) df I hope it helps. Best, Dimitris On 8/17/2011 12:42 PM, Marius Hofert wrote: Dear all, First, let's create some data to play around: set.seed(1) (df- data.frame(Group=rep(c(Group1,Group2,Group3), each=10), Value=c(rexp(10, 1), rexp(10, 4), rexp(10, 10)))[sample(1:30,30),]) ## Now we need the empirical distribution function: edf- function(x) ecdf(x)(x) # empirical distribution function evaluated at x ## The big question is how one can apply the empirical distribution function to ## each subset of df determined by Group, so how to apply it to Group1, then ## to Group2, and finally to Group3. You might suggest (?) to use tapply: (edf.- tapply(df$Value, df$Group, FUN=edf)) ## That's correct. But typically, one would like to obtain not only the values, ## but a data.frame containing the original information and the new (edf-)values. ## What's a simple way to get this? (one would be required to first sort df ## according to Group, then paste the values computed by edf to the sorted df; ## seems a bit tedious). ## A solution I have is the following (but I would like to know if there is a ## simpler one): (edf..- do.call(rbind, lapply(unique(df$Group), function(strg){ subdata- subset(df, Group==strg) # sub-data subdata- cbind(subdata, edf=edf(subdata$Value)) })) ) Cheers, Marius __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] too many var in lm
another approach is: Df - as.data.frame(cbind(rcauchy(100), matrix(runif(100*50), ncol = 50))) fit - lm(V1 ~ ., data = Df) fit I hope it helps. Best, Dimitris On 8/17/2011 3:28 PM, R. Michael Weylandt wrote: The most elegant solution is going to depend on where you data comes from, but one way to do it if you have a matrix of data: D = cbind(rcauchy(100), matrix(runif(100*50),ncol=50)) # Some nonsense data lm(D[,1] ~ D[,-1]) If you let us know how your data is set up, a more specific response can be given. Hope this helps, Michael Weylandt On Wed, Aug 17, 2011 at 9:23 AM, carol whitewht_...@yahoo.com wrote: Hello, It might be an easy question but if you have many variables to fit in the lm function, how do you take all without specifying var1+var2+...+var2100 in the terms parameter in response ~ terms? Cheers, Carol __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] round() a data frame containing 'character' variables?
One approach is the following: numVars - sapply(iris, is.numeric) iris[numVars] - lapply(iris[numVars], round, digits = 2) head(iris) You can also put it in one lapply() call if you like. I hope it helps. Best, Dimitris On 8/10/2011 11:34 AM, Liviu Andronic wrote: Dear all It is difficult to use round(..., digits=2) on a data frame since one has to first take care to remove non-numeric variables such as 'character' or 'factor': head(round(iris, 2)) Error in Math.data.frame(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, : non-numeric variable in data frame: Species head(round(iris[1:4], 2)) Sepal.Length Sepal.Width Petal.Length Petal.Width 1 5.1 3.5 1.4 0.2 2 4.9 3.0 1.4 0.2 3 4.7 3.2 1.3 0.2 4 4.6 3.1 1.5 0.2 5 5.0 3.6 1.4 0.2 6 5.4 3.9 1.7 0.4 Is there an elegant way to use round() on a data frame containing 'character' variables without removing them? Thank you Liviu -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] convert 'list' to 'vector'?
Check function unlist(). Best, Dimitris On 8/10/2011 8:58 PM, Liviu Andronic wrote: Dear all How does one convert a non-symmetric list to a vector? See below: x- list() x[[1]]- letters[1:5] x[[2]]- letters[6:10] x[[3]]- letters[11:12] x [[1]] [1] a b c d e [[2]] [1] f g h i j [[3]] [1] k l paste(x) [1] c(\a\, \b\, \c\, \d\, \e\) c(\f\, \g\, \h\, \i\, \j\) [3] c(\k\, \l\) as.vector(x) [[1]] [1] a b c d e [[2]] [1] f g h i j [[3]] [1] k l simplify2array(x) [[1]] [1] a b c d e [[2]] [1] f g h i j [[3]] [1] k l What I would need to get instead is: letters[1:12] [1] a b c d e f g h i j k l Any ideas? Regards Liviu -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Multivariate normal density in C for R
I use the following function which does not uses loops and seems to be pretty fast: dmvnorm - function (x, mu, Sigma, df, log = FALSE) { if (!is.matrix(x)) x - rbind(x) p - nrow(Sigma) ed - eigen(Sigma, symmetric = TRUE) ev - ed$values if (!all(ev = -1e-06 * abs(ev[1]))) stop('Sigma' is not positive definite) ss - if (!is.matrix(mu)) { x - rep(mu, each = nrow(x)) } else { x - mu } inv.Sigma - ed$vectors %*% (t(ed$vectors)/ev) quad - 0.5 * rowSums((ss %*% inv.Sigma) * ss) fact - -0.5 * (p * log(2 * pi) + sum(log(ev))) if (log) as.vector(fact - quad) else as.vector(exp(fact - quad)) } I hope it helps. Best, Dimitris On 6/25/2011 3:58 PM, zerfetzen wrote: Does anyone know of a package that uses C code to calculate a multivariate normal density? My goal is to find a faster way to calculate MVN densities and avoid R loops or apply functions, such as when X and mu are N x K matrices, as opposed to vectors, and in this particular case, speed really matters. I would like to be able to use .C or .Call to pass X, mu, Sigma, and N to a C program and have it return a vector of log densities to R. I'm new to putting C in R, but am sure I'll figure it out. Thanks for any suggestions. -- View this message in context: http://r.789695.n4.nabble.com/Multivariate-normal-density-in-C-for-R-tp3624602p3624602.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Multivariate normal density in C for R
On 6/26/2011 5:53 PM, zerfetzen wrote: IIRC, package mvtnorm will allow an X matrix, but requires mu to be a vector, so although it's close, it won't do it all...but all suggestions are well received. Dimitrius, you don't happen to have the multivariate t form of that function, do you? Well, it's relatively easy to adjust it, e.g., dmvt - function (x, mu, Sigma, df, log = FALSE) { if (!is.matrix(x)) x - rbind(x) p - nrow(Sigma) ed - eigen(Sigma, symmetric = TRUE) ev - ed$values if (!all(ev = -1e-06 * abs(ev[1]))) stop('Sigma' is not positive definite) ss - if (!is.matrix(mu)) { x - rep(mu, each = nrow(x)) } else { x - mu } inv.Sigma - ed$vectors %*% (t(ed$vectors)/ev) quad - rowSums((ss %*% inv.Sigma) * ss)/df fact - lgamma((df + p)/2) - lgamma(df/2) - 0.5 * (p * (log(pi) + log(df)) + sum(log(ev))) if (log) fact - 0.5 * (df + p) * log(1 + quad) else exp(fact) * ((1 + quad)^(-(df + p)/2)) } Best, Dimitris -- View this message in context: http://r.789695.n4.nabble.com/Multivariate-normal-density-in-C-for-R-tp3624602p3626127.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Fastest way of finding if any members of vector x fall in the range of the rows of matrix y
yet another solution is: v - c(1, 2, 6, 5) w - matrix(c(1, 4, 3, 5, 8, 10), ncol = 2, byrow = TRUE) check - outer(w[, 1], v, -) * outer(w[, 2], v, -) v[which(check = 0, arr.ind = TRUE)[, 2]] Best, Dimitris On 6/24/2011 10:05 PM, Martin Morgan wrote: On 06/24/2011 11:12 AM, Dennis Murphy wrote: Hi: Not much different from Peter's approach, but here's another try: v- c(1, 2, 6, 5) w- matrix(c(1, 4, 3, 5, 8, 10), ncol = 2, byrow = TRUE) w [,1] [,2] [1,] 1 4 [2,] 3 5 [3,] 8 10 f- function(x) v[which(v= x[1] v= x[2])] unlist(apply(w, 1, f)) [1] 1 2 5 If you just do the apply() part, the function will return a list of those elements of v that fall within the i-th interval. Maybe v = c(1, 2, 6, 5) start = c(-Inf, 1, 3, 8) end = c(-Inf, 4, 5, 10) v[ v = end[findInterval(v, start)] ] Also IRanges::findOverlaps / countOverlaps in Bioconductor v[countOverlaps(IRanges(v, v), IRanges(start, end)) != 0] http://bioconductor.org/install Martin HTH, Dennis On Fri, Jun 24, 2011 at 9:52 AM, Salih Tunasaliht...@gmail.com wrote: Hi guys, let's assume i have the following 1 x = 2 6 5 1 4 y = 3 5 8 10 i want the code to report back 1, 2 and 5 from x. Basically it shopuld check whether each elements of x falls in the range of each row of x. 1 and 2 falls in between 1-4 and 5 falls in between 3-5. I do this with two for loops but in the case of very large list, it takes ages. best, salih On Fri, Jun 24, 2011 at 4:12 PM, Dennis Murphydjmu...@gmail.com wrote: Hi: That leaves open several possibilities. Could you please supply a small, reproducible example (i.e., one that someone can copy and paste into an R session) that illustrates the problem along with the solution you expect? TIA, Dennis On Fri, Jun 24, 2011 at 2:30 AM, Salih Tunasaliht...@gmail.com wrote: Hi Dimitris, Thanks for your reply. But this is not exactly what i am after. I want to find the probes that falls into certain regions. In your solution it will ignore the second probe if it falls into the same region as the first one. Is there any vector trickb uilt in R to find whether probes fall into certain regions? best, salih On Fri, Jun 24, 2011 at 10:23 AM, Dimitris Rizopoulos d.rizopou...@erasmusmc.nl wrote: One approach is the following: x- rnorm(5) y- matrix(rnorm(5*2), 5, 2) check- y - x check[, 1] * check[, 2] 0 I hope it helps. Best, Dimitris On 6/24/2011 10:57 AM, Salih Tuna wrote: Hi All, What is the fastest way of finding if any members of vector x fall in the range of the rows of matrix y? I do not want to use two for loops as this will take forever. Any help will be appreciated, best, salih [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/**biostatistiek/http://www.erasmusmc.nl/biostatistiek/ [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Fastest way of finding if any members of vector x fall in the range of the rows of matrix y
One approach is the following: x - rnorm(5) y - matrix(rnorm(5*2), 5, 2) check - y - x check[, 1] * check[, 2] 0 I hope it helps. Best, Dimitris On 6/24/2011 10:57 AM, Salih Tuna wrote: Hi All, What is the fastest way of finding if any members of vector x fall in the range of the rows of matrix y? I do not want to use two for loops as this will take forever. Any help will be appreciated, best, salih [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Replace selected columns of a dataframe with NA
I don't know if you have factors in your data frame, but in any case, you can try the following approach: mydata - data.frame(id = 1:10, x = rnorm(10, 80), z = rnorm(10, 80), w = gl(2, 5)) mydata f - function (x) { if (is.numeric(x)) x[x 80] - NA x } mydata[-1] - lapply(mydata[-1], f) mydata I hope it helps. Best, Dimitris On 6/20/2011 10:40 AM, Chris Beeley wrote: I am using the following command to replace all the missing values and assorted typos in a dataframe with NA: mydata[mydata80]=NA The problem is that the first column contains values which should be more than 80, so really I want to do it just for mydata[,2:length(mydata)] I can't seem to re-write the code to fit: mydata[,2:length(mydata)80]=NA # no error message, but doesn't work- doesn't do anything, it would seem I realise I can just keep the first column somewhere safe and copy it back again when I'm done, but I wondered if there was a more elegant solution, which would be much more important, if say I just wanted to replace the odd columns, or something like that. I found this code on the internet too: idx- which(foo80, arr.ind=TRUE) foo[idx[1], idx[2]]- NA But I can't seem to rewrite that either, for the same reason Many thanks! Chris Beeley Institute of Mental Health __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Applying function to all elements of a formula
maybe another way is by reconstructing the formula using paste(), e.g., data - data.frame(y = rnorm(5), x1 = runif(5), z = runif(5), age = runif(5)) nameRsp - y nams - names(data) namsX - nams[!nams %in% nameRsp] form - as.formula(paste(nameRsp, ~ , paste(log(, namsX, ), sep = , collapse = +))) lm(form, data) I hope it helps. Best, Dimitris On 6/18/2011 10:41 AM, Dennis Murphy wrote: Yes, it's possible, but if you want to do prediction on future x-values, you will likely have a problem. One way to do it would be something like (assuming y is the first column of dat) reg- lm(y ~ log(as.matrix(dat[, -1])), dat) but the output would be pretty ugly (see summary(reg)). Another would be to construct the matrix outside the data frame and do something like X- log(as.matrix(dat[, -1])) reg- lm(dat$y ~ X) There may be better ways, though... Dennis On Fri, Jun 17, 2011 at 11:08 PM, Scott Fortmann-Roescot...@gmail.com wrote: Hi, I would like to do a regression like: reg- lm(y~log(.), data) where the log function is applied to . in the form: log(x1)+ log(x2)+ log(x3)... instead of in the form log(x1+x2+x3+...) Is this possible? Thank you, Scott [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] question about split
try this: dat - data.frame(x = runif(100), fac1 = rep(c(a, b, c, d), each = 25), fac2 = rep(c(A, B), 50)) splits - c(fac1, fac2) split(dat, dat[splits]) I hope it helps. Best, Dimitris On 6/17/2011 2:34 PM, carslaw wrote: Dear R-users I seem to be stumped on something simple. I want to split a data frame by factor levels given in one or more columns e.g. given dat- data.frame(x = runif(100), fac1 = rep(c(a, b, c, d), each = 25), fac2 = rep(c(A, B), 50)) I know I can split it by fac1, fac2 by: split(dat, list(dat$fac1, dat$fac2)) which gives a list of length 8 - as desired. My question is, given a vector of column name(s) to split by, how can I supply split with these? I was thinking something like: splits- c(fac1, fac2) split(dat, list(splits)) But that is clearly wrongand I can't see the solution Many thanks David Carslaw Science Policy Group Environmental Research Group MRC-HPA Centre for Environment and Health King's College London Room 4.129 Franklin Wilkins Building Stamford Street London SE1 9NH -- View this message in context: http://r.789695.n4.nabble.com/question-about-split-tp3605281p3605281.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Expand DF with all levels of a variable
here is a solution with expand.grid(), a - c(2,2,3,4,5,6,7,8,9) a.cut - cut(a, breaks=c(0,2,6,9,12,13,15,16), right=FALSE ) x - data.frame(a, a.cut) out - expand.grid(a = 0, a.cut = setdiff(levels(a.cut), unique(a.cut))) rbind(out, x) I hope it helps. Best, Dimitris On 6/14/2011 1:32 PM, Patrick Hausmann wrote: Dear list, I would like to expand a DF with all the missing levels of a variable. a - c(2,2,3,4,5,6,7,8,9) a.cut - cut(a, breaks=c(0,2,6,9,12), right=FALSE ) (x - data.frame(a, a.cut)) # In 'x' the level [0,2) is missing. AddMissingLevel - function(xdf) { xfac - factor( c([0,2), [2,6), [6,9), [9,12)) ) xlevels - levels(xfac) if(length(xlevels) != nlevels(factor(xdf$a.cut))) { v - setdiff(xlevels, factor(xdf$a.cut)) u - data.frame(a = 0, a.cut = v) x - rbind(u, x) } return(x) } AddMissingLevel(x) Does a more general approach exist, e.g. using expand.grid? Thanks for any help!! Patrick __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Surv(): Stop time must be start time, NA created
this sounds like a competing risks problem. Maybe you would be interested in a cause-specific hazard regression or the Fine Gray model (http://cran.r-project.org/package=cmprsk). Recently there was also a special issue in JSS on this topic (http://www.jstatsoft.org/v38). I hope it helps. Best, Dimitris On 6/3/2011 12:17 PM, Federico Calboli wrote: I am writing to get a better handle on a warning I am getting from a coxph analysis I am doing. I am analysing age of onset of dementia *after* the onset of parkinson disease. My data looks like: age.park age.dem age.death censor x1 x2 x3 x4 1 76 8788 0 16 33 E3 E3 2 75 8484 0 33 36 E3 E3 3 77 8181 1 NA NANA NA 4 65 6569 0 NA NA E4 E4 5 56 7679 0 NA NANA NA 6 62 7272 1 NA NANA NA ... Obviously some individuals (lines 1,2,5) will first develop parkinson, then a few years later, dementia. Some individuals will not develop dementia (lines 3 and 6, where age of death and age of dementia correspond, but the censor variable is 1). Some (more) unluky individuals develop parkinson and dementia at the same time (line 4). my coxph model looks like coxph(Surv(age.mot,age.dem, censor) ~ x1 + x2 + x3 + x4, mydata) and I get the warning: In Surv(age.mot, age.dem, censor) : Stop time must be start time, NA created I am almost sure that this is due to the instances where age.park == age.dem, but there is nothing I can really do. So my question: how do I deal with the instances where age.park == age.dem in order to keep those individuals in the analysis and to get sensible results? Best wishes Federico -- Federico C. F. Calboli Department of Epidemiology and Biostatistics Imperial College, St. Mary's Campus Norfolk Place, London W2 1PG Tel +44 (0)20 75941602 Fax +44 (0)20 75943193 f.calboli [.a.t] imperial.ac.uk f.calboli [.a.t] gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Surv(): Stop time must be start time, NA created
On 6/3/2011 12:32 PM, Federico Calboli wrote: On 3 Jun 2011, at 11:27, Dimitris Rizopoulos wrote: this sounds like a competing risks problem. Maybe you would be interested in a cause-specific hazard regression or the Fine Gray model (http://cran.r-project.org/package=cmprsk). I will look into that,but, biologically, parkinson leads to dementia (cause and effect rather than competing risk), and in fact all my subjects do have parkinson. Unless the two competing risks are 'risk of dementia' vs 'risk of death'. Yes, this what I mean. Either you could be interested in the composite event dementia or death, whatever comes first, or you could be interested only in dementia, in which case death could be considered a competing risk. Best, Dimitris Best F Recently there was also a special issue in JSS on this topic (http://www.jstatsoft.org/v38). I hope it helps. Best, Dimitris On 6/3/2011 12:17 PM, Federico Calboli wrote: I am writing to get a better handle on a warning I am getting from a coxph analysis I am doing. I am analysing age of onset of dementia *after* the onset of parkinson disease. My data looks like: age.park age.dem age.death censor x1 x2 x3 x4 1 76 8788 0 16 33 E3 E3 2 75 8484 0 33 36 E3 E3 3 77 8181 1 NA NANA NA 4 65 6569 0 NA NA E4 E4 5 56 7679 0 NA NANA NA 6 62 7272 1 NA NANA NA ... Obviously some individuals (lines 1,2,5) will first develop parkinson, then a few years later, dementia. Some individuals will not develop dementia (lines 3 and 6, where age of death and age of dementia correspond, but the censor variable is 1). Some (more) unluky individuals develop parkinson and dementia at the same time (line 4). my coxph model looks like coxph(Surv(age.mot,age.dem, censor) ~ x1 + x2 + x3 + x4, mydata) and I get the warning: In Surv(age.mot, age.dem, censor) : Stop time must be start time, NA created I am almost sure that this is due to the instances where age.park == age.dem, but there is nothing I can really do. So my question: how do I deal with the instances where age.park == age.dem in order to keep those individuals in the analysis and to get sensible results? Best wishes Federico -- Federico C. F. Calboli Department of Epidemiology and Biostatistics Imperial College, St. Mary's Campus Norfolk Place, London W2 1PG Tel +44 (0)20 75941602 Fax +44 (0)20 75943193 f.calboli [.a.t] imperial.ac.uk f.calboli [.a.t] gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ -- Federico C. F. Calboli Department of Epidemiology and Biostatistics Imperial College, St. Mary's Campus Norfolk Place, London W2 1PG Tel +44 (0)20 75941602 Fax +44 (0)20 75943193 f.calboli [.a.t] imperial.ac.uk f.calboli [.a.t] gmail.com -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Minimum value by ID
have a look at help file of function tapply(), and try this: with(dat, tapply(x, id.var, min)) I hope it helps. Best, Dimitris On 5/17/2011 3:44 PM, Downey, Patrick wrote: Hello, I have a longitudinal dataset where each individual has a different number of entries. Thus, it is of the following structure: x- runif(12) id.var- factor(c(rep(D1,4),rep(D2,2),rep(D3,3),rep(D4,3))) dat- as.data.frame(x) dat$id.var- id.var dat dat x id.var 1 0.9611269 D1 2 0.6738606 D1 3 0.9724301 D1 4 0.9787778 D1 5 0.2468355 D2 6 0.7031734 D2 7 0.2458727 D3 8 0.8439799 D3 9 0.5223196 D3 10 0.6930475 D4 11 0.8887677 D4 12 0.5483756 D4 I want to create a vector with length equal to the number of unique id.var and which has the minimum value for each id.var. That is, I want a vector which holds the minimum value for each person in my dataset. The following works, but I'm sure there is something more efficient. I would assume there is a function for this, but couldn't find anything. id- levels(id.var) min- rep(0,length(id)) for(i in 1:length(id)){ min[i]- min(dat$x[dat$id.var==id[i]]) } min min [1] 0.6738606 0.2468355 0.2458727 0.5483756 Thank you in advance, Mitch __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] conditional rowsums in sapply
assuming that the row entries for the columns with the same name are not all zero, you can try something in the following lines: dfrm - data.frame(a = 1:4, a = 1:4, b = 1:4, b = 1:4, b = 1:4, check.names = FALSE) dfrm[3, 1:3] - NA dfrm vals - unlist(dfrm) res - tapply(vals, names(vals), sum, na.rm = TRUE) res[res == 0] - NA as.data.frame(matrix(res, ncol = 2)) I hope it helps. Best, Dimitris On 5/16/2011 4:25 PM, Assu wrote: Hi all I have a data frame with duplicate columns and i want to remove duplicates by adding rows in each group of duplicates, but have lots of NA's. Data: dfrm- data.frame(a = 1:4, b= 1:4, cc= 1:4, dd=1:10, ee=1:4) names(dfrm)- c(a, a, b, b, b) dfrm[3,2:3]-NA dfrm a a b b b 1 1 1 1 1 1 2 2 2 2 2 2 3 NA NA NA 3 3 4 4 4 4 4 4 I did: sapply(unique(names(dfrm)),function(x){ rowSums(dfrm[ ,grep(x, names(dfrm)),drop=FALSE])}) which works. However, I want rowSums conditional: 1) if there is at least one value non NA in a row of each group of duplicates, apply rowSums to get the value independently of the existence of other NA's in the group row. 2) if all values in a row of duplicates are NA, I get NA In my data dfrm I would get a b 12 3 24 6 3 NA 6 48 12 Can't use na.rm=TRUE or FALSE. I tried: sapply(unique(names(dfrm)),function(x) ifelse(any(!is.na(dfrm[ ,grep(x, names(dfrm))])), rowSums(dfrm[ ,grep(x, names(dfrm)),drop=FALSE],na.rm=TRUE),NA)) and it doesn't work. Can someone please help me? Thanks in advance. -- View this message in context: http://r.789695.n4.nabble.com/conditional-rowsums-in-sapply-tp3526332p3526332.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] first occurrence of a value?
another approach is: df - data.frame(j1999 = c(0,0,0,0,1,0), j2000 = c(NA,1,1,1,0,0), j2001 = c(1,0,1,0,0,0)) years - as.numeric(gsub(^[^0-9]+, , names(df))) ind - apply(sapply(df, ==, 1), 1, function (x) which(x)[1]) df$year - years[ind] I hope it helps. Best, Dimitris On 5/4/2011 1:52 PM, Albert-Jan Roskam wrote: Hello, A simple question perhaps, but how do I, within each row, find the first occurence of the number 1 in the df below? I want to use this position to programmatically create the variable 'year'. I'v come up with a solution, but I find it downright ugly. Is there a simpler way? I was hoping for a useful built-in function that I don;t yet know about. df- data.frame(j1999=c(0,0,0,0,1,0), j2000=c(NA, 1, 1, 1, 0, 0), j2001=c(1, 0, 1, 0, 0, 0), year=c(2001, 2000, 2000, 2000, 1999, NA)) library(gsubfn) x- apply(df==1, 1, which) giveYear- function(df) { return( as.numeric(gsubfn(^[^0-9]+, , names(df)[1])) ) } df$year2- sapply(x, giveYear) Thanks in advance! Cheers!! Albert-Jan ~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~ [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Extract complete rows by group and maximum
two possibilities are: f - factor(DF$cvd_basestudy, unique(DF$cvd_basestudy)) ind - ave(DF$es_time, f, FUN = function (x) x == max(x)) DF[as.logical(ind), ] f - factor(DF$cvd_basestudy, unique(DF$cvd_basestudy)) lis - lapply(split(DF, f), function (d) d[which.max(d$es_time), ]) do.call(rbind, lis) with DF - datares_sinus_variable. I hope it helps. Best, Dimitris On 4/28/2011 1:31 PM, Sandy Small wrote: Hi I'm trying to extract complete rows from a dataframe by group based on the maximum in a column within that group. Thus I have a dataframe: cvd_basestudy ... es_time ... _ study1 ... 0.3091667 study2 ... 0.3091667 study2 ... 0.2625000 study3 ... 0.303 study3 ... 0.2625000 __ etc I can extract the basestudy and the max(es_time) using ddply ddply(datares_sinus_variable, .(cvd_basestudy), function(x){max(x[['es_time']])}) or by by(datares_sinus_variable$es_time, datares_sinus_variable$cvd_basestudy, max) but how do I extract the whole line so that I can get a dataframe with all the data for the maximum line? (dput output from first 5 rows of my actual dataframe follows) Any help would be much appreciated. Thanks in advance Sandy Small structure(list(cvd_basestudy = c(study1, study2, study2, study3, study3), ecd_rhythm = structure(c(5L, 5L, 5L, 5L, 5L), .Label = c(AF, FLUTTER, PACED AF, SCRAP, SINUS, UNSURE), class = factor), cvd_frame_mode = structure(c(2L, 2L, 2L, 2L, 2L), .Label = c(fixed_time, variable_time), class = factor), cvd_part_fmt = structure(c(4L, 4L, 4L, 4L, 4L), .Label = c(first, last, mid, whole), class = factor), cvd_prev_fmt = structure(c(1L, 2L, 1L, 3L, 2L), .Label = c(All, Best, Q1, Q2, Q3, Q4), class = factor), cvd_cur_fmt = structure(c(5L, 5L, 1L, 4L, 4L), .Label = c(All, Best, Q1, Q2, Q3, Q4), class = factor), ps_pt = c(1, 1, 2, 1, 2), es_pt = c(8, 8, 8, 8, 8), ed_pt = c(21, 21, 18, 17, 18), cvd_median_limit = c(1.057, 1.057, 1.048, 1.037, 1.05), cvd_average_beat = c(1.06, 1.06, 1.05, 1.04, 1.05), limit = c(0.9, 0.9, 0.9, 0.9, 0.9), sstd_mi = c(FALSE, FALSE, FALSE, FALSE, FALSE), sstd_hbp = c(FALSE, FALSE, FALSE, FALSE, FALSE), sstd_ptca = c(FALSE, FALSE, FALSE, FALSE, FALSE), sstd_cabg = c(TRUE, TRUE, TRUE, TRUE, TRUE), sstd_norm_perf = c(FALSE, FALSE, FALSE, FALSE, FALSE), sstd_posnegett = structure(c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_), .Label = c(-, +), class = factor), sstd_function = structure(c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_), .Label = c(MODERATE, NORMAL, POOR, VERY POOR), class = factor), cvd_cur_fmt_n = c(3, 3, NA, 2, 2), cvd_prev_fmt_n = c(NA, NA, NA, 1, NA), cvd_cur_fmt2 = structure(c(3L, 3L, 1L, 3L, 3L), .Label = c(All, Best, Quartiles), class = factor), cvd_prev_fmt2 = structure(c(1L, 2L, 1L, 3L, 2L), .Label = c(All, Best, Quartiles), class = factor), es_time = c(0.3091667, 0.3091667, 0.2625, 0.303, 0.2625), es_time_err = c(0.04416667, 0.04416667, 0.04375, 0.0433, 0.04375), ed_time = c(0.5741667, 0.5741667, 0.4375, 0.39, 0.4375)), .Names = c(cvd_basestudy, ecd_rhythm, cvd_frame_mode, cvd_part_fmt, cvd_prev_fmt, cvd_cur_fmt, ps_pt, es_pt, ed_pt, cvd_median_limit, cvd_average_beat, limit, sstd_mi, sstd_hbp, sstd_ptca, sstd_cabg, sstd_norm_perf, sstd_posnegett, sstd_function, cvd_cur_fmt_n, cvd_prev_fmt_n, cvd_cur_fmt2, cvd_prev_fmt2, es_time, es_time_err, ed_time ), row.names = c(651, 655, 656, 661, 663), class = data.frame) This message may contain confidential information. If yo...{{dropped:21}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Second largest element from each matrix row
one way is the following: a - matrix(rnorm(9), 3 ,3) aa - a[order(row(a), -a)] matrix(aa, nrow(a), byrow = TRUE)[, 2] I hope it helps. Best, Dimitris On 4/26/2011 2:01 PM, Lars Bishop wrote: Hi, I need to extract the second largest element from each row of a matrix. Below is my solution, but I think there should be a more efficient way to accomplish the same, or not? set.seed(1) a- matrix(rnorm(9), 3 ,3) sec.large- as.vector(apply(a, 1, order, decreasing=T)[2,]) ans- sapply(1:length(sec.large), function(i) a[i, sec.large[i]]) ans Thanks in advance for your help, Lars. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] all combinations with replacement
probably expand.grid(), e.g., expand.grid(rep(list(0:6), 3)) I hope it helps. Best, Dimitris On 4/21/2011 9:28 PM, Kehl Dániel wrote: Dear all, is there an easy way to get all possible combinations (?) with replacement. If n=6, k=3, i want something like 0 0 6 0 5 1 0 4 2 0 3 3 0 2 4 . . . 5 0 1 5 1 0 6 0 0 I tried to look at combn() but I could not get this done with it. Thank you in advance: Daniel __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] zero fill empty cell in data.frame
you could try something along these lines: DF - data.frame(V1 = c(0, 0.9, , 0.8, 0.1, 0), V2 = c(0.9, , 0.8, 0.7, , 1)) DF sapply(DF, function (x) as.numeric(gsub((^ +)|( +$), 0, x))) I hope it helps. Best, Dimitris On 4/19/2011 11:39 AM, Simone Gabbriellini wrote: Hello List, I have a data frame like: V130V131V132V133V134V135V136 1 0 0 0.9 0 0.9 0 0 2 0 0 0 0 0 0.8 3 0 0 0 00.9 0 0 4 0.9 0 0 00 0 0.9 5 0 0 0 6 0 0 0 0.9 0 0 0.9 7 0 0 0.8 0 0 0 0 8 0.9 0 0 0.9 0.8 0 9 0 0 0 0.9 0.9 0 0 10 0 0 0 0 0 0 0.9 I would like to fill the empty cells with a 0... how to address those empty cells? thanks for your help! best, Simone __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Several factors same levels
one way is the following: DF - data.frame(id = 1:6, sex = gl(2, 3, labels = c(M, F)), x = sample(0:1, 6, TRUE), y = sample(0:1, 6, TRUE), z = rnorm(6)) f - function (x) { if (all(unique(x) %in% 0:1)) factor(x, levels = 0:1, labels = c(No, Yes)) else x } DF[] - lapply(DF, f) I hope it helps. Best, Dimitris On 4/19/2011 10:57 AM, dereksloan wrote: This is probably very simple but I'm new to R so apologies for being stupid. I have some data with No coded as 0 and yes coded as 1. e.g. id sex alcohol smoker 1 M 01 2 F 10 3 M 00 I realise I can covert the numerical variable back to a factor by falcohol-factor(alcohol,levels=0:1) levels-c(No,Yes) fsmoker-factor(smoker,levels=0:1) levels-c(No,Yes) but can I do this for all factors using the same levels in a single command to make it easier (i.e. switch all 0/1, no/yes variables without having to recode the same factor levels for them all)? Derek -- View this message in context: http://r.789695.n4.nabble.com/Several-factors-same-levels-tp3459878p3459878.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Table multiple answers variables
try the following: DF - as.data.frame(matrix(sample(2, 120, TRUE), 10, 12)) Results - data.frame( var = names(DF), count = colSums(DF == 1), percentage = colMeans(DF == 1) ) I hope it helps. Best, Dimitris On 3/15/2011 3:13 PM, Orvalho Augusto wrote: I have a dataset like this: q25_1 q25_2 q25_3 q25_4 q25_5 q25_6 q25_7 q25_8 q25_9 q25_10 q25_11 q25_12 1 2 2 1 1 2 1 2 1 1 2 1 3 2 2 2 2 1 2 1 2 1 1 2 1 2 3 2 1 1 1 2 1 2 1 2 2 1 2 4 2 2 1 1 2 2 2 1 1 1 2 2 5 2 2 1 1 2 3 2 1 2 2 2 2 6 2 2 1 1 2 2 2 1 1 1 2 2 7 2 2 1 1 2 3 2 1 2 2 3 2 8 2 2 1 1 1 1 2 1 1 1 2 2 9 2 2 1 1 2 2 2 1 2 2 2 2 10 2 2 1 1 2 2 2 1 2 1 2 2 where those q25_ variables are answers (coded 1 for Yes 2 for No). Now I need a way to table counting the ones foreach variables like varcountpercentage q25_1 20 80 q25_2 34 85 ... Is it possible ir R? For who used stata might use mrtab. Thank you Orvalho [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] replace with quantile value for a large data frame...
one way is the following: X1 - c(1:10) X2 - c(11:20) X3 - c(21:30) X4 - c(31:40) X5 - c(41:50) DF - data.frame(X1, X2, X3, X4, X5) as.data.frame(sapply(DF, function (x) { qx - quantile(x) cut(x, qx, include.lowest = TRUE, labels = 1:4) })) You may also have a look at function cut2() from package Hmisc. I hope it helps. Best, Dimitris On 3/13/2011 4:49 PM, Ram H. Sharma wrote: Dear R-Experts I am sure this might look simple question for experts, at least is problem for me. I have a large data frame with over 1000 variables and each have different distribution( i.e. have different quantile). I want to create a new grouped data frame, where the new variables where the value falling in first (25%), second (25% to50%), third (50% to75%) and fourth quantiles (75%) are replaced with 1,2,3, 4 respectively. The following example is just to workout. # my example: X1- c(1:10) X2- c(11:20) X3- c(21:30) X4- c(31:40) X5- c(41:50) dataf- data.frame(X1, X2, X3, X4, X5) # my efforts of the last week led me to this point for (i along(length(dataf[1,]))) { qntfun- function (x) { XQ- as.numeric(as.matrix(quantile(x))) Q1- XQ[1] Q2- XQ[2] Q3- XQ[3] Q4- XQ[4] for (i in 1:length(x)){ if (x[i] Q2) { x[i]- 1 } else { if ( x[i] Q2 x[i] Q3){ x[i]- 2 } else { if ( x[i]Q3 x[i]Q4) { x[i]- 3 } else { if (x[i] Q4) { x[i]- 4 } else{ x[i]- 0 } } } } } } apply(dataf, 1:length(dataf), qntfun) } # I got error, I can not fix it. I would be glad to see a more slim solution, but I could not think any. Thanks in advance for your help. Ram Sharma [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] using pre-calculated coefficients and LP in coxph()?
probably you want to use the 'init' argument and 'iter.max' control-argument of coxph(). For example, for the Lung dataset, we fix the coefficients of age and ph.karno at 0.05 and -0.05, respectively: library(survival) coxph(Surv(time, status) ~ age + ph.karno, data = lung, init = c(0.05, -0.05), iter.max = 0) I hope it helps. Best, Dimitris On 3/13/2011 6:08 PM, Angel Russo wrote: I need to force a coxph() function in R to use a pre-calculated set of beta coefficients of a gene signature consisting of xx genes and the gene expression is also provided of those xx genes. If I try to use coxph() function in R using just the gene expression data alone, the beta coefficients and coxph$linear.predictors will change and I need to use the pre-calcuated linear predictor not re-computed using coxph() function. The reason is I need to compute a quantity that uses as it's input the coxph() output but I need this output to be pre-calculated beta-coefficients and linear.predictor. Any one can show me how to do this in R? Thanks a lot. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] using pre-calculated coefficients and LP in coxph()?
On 3/13/2011 7:43 PM, Ravi Varadhan wrote: Like David, I too thought that `offset' is the way to do this. I was actually in the midst of testing the differences between using `offset' and `init' when David's email came. Here is what I could figure out so far: 1. If you want to fix only a subset of regressors, but let others be estimated, then you must use `offset'. The `init' approach will not work. Yes, indeed, this is right. 2. Even when all the regressors are fixed (I have to admit that I do not see the point of this, like David said), there seems to be a difference in using `init' and `offset'. First of all, we cannot interpret or use the standard errors, CIs, abd p-values when iter.max=0. Secondly, there is major disagreement in the predictions between `offset' and `init' with no iterations. You can run the following code to verify this: Of course, indeed, I don't know what Angel has in mind, but there are some cases where you might want to compute the hessian matrix at specific values using vcov() (local approximation of the likelihood for sensitivity analysis), and in this case the `offset' approach will not work. ans1- coxph(Surv(time, status) ~ age + ph.karno, data = lung, init = c(0.05, -0.05), iter.max = 0) ans2- coxph(Surv(time, status) ~ offset(0.05*age) + offset(-0.05*ph.karno), data = lung) lp1- predict(ans1, type=lp) lp2- predict(ans2, type=lp) all.equal(lp1, lp2) all.equal(lp1, lp2) [1] Mean relative difference: 1.463598 in fact both are the same, only in the first one they are centered, e.g., ans1$linear.predictors ans2$linear.predictors - mean(ans2$linear.predictors) The results from `offset' are correct, i.e. lp2 can be readily verified to be equal to 0.05 * (age - ph.karno). I don't know how lp1 is computed. Ravi. Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu - Original Message - From: David Winsemiusdwinsem...@comcast.net Date: Sunday, March 13, 2011 2:29 pm Subject: Re: [R] using pre-calculated coefficients and LP in coxph()? To: Dimitris Rizopoulosd.rizopou...@erasmusmc.nl Cc: r-help@r-project.org, Angel Russoangerusso1...@gmail.com On Mar 13, 2011, at 1:32 PM, Dimitris Rizopoulos wrote: probably you want to use the 'init' argument and 'iter.max' control-argument of coxph(). For example, for the Lung dataset, we fix the coefficients of age and ph.karno at 0.05 and -0.05, respectively: library(survival) coxph(Surv(time, status) ~ age + ph.karno, data = lung, init = c(0.05, -0.05), iter.max = 0) I hope it helps. Best, Dimitris On 3/13/2011 6:08 PM, Angel Russo wrote: I need to force a coxph() function in R to use a pre-calculated set of beta coefficients of a gene signature consisting of xx genes and the gene expression is also provided of those xx genes. I would have guessed (and that is all one can do without an example and better description of what the setting and goal might be) that the use of the offset capablity in coxph might be needed. -- David. If I try to use coxph() function in R using just the gene expression data alone, the beta coefficients and coxph$linear.predictors will change and I need to use the pre-calcuated linear predictor not re-computed using coxph() function. The reason is I need to compute a quantity that uses as it's input the coxph() output but I need this output to be pre-calculated beta-coefficients and linear.predictor. Any one can show me how to do this in R? Thanks a lot. David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Extracting only odd columns from a matrix
one way is using the seq(), e.g., say 'm' is your matrix, then try: m[, seq(1, ncol(m), by = 2)] I hope it helps. Best, Dimitris On 3/9/2011 3:20 PM, Nixon, Matthew wrote: Hi, This might seem like a simple question but at the moment I am stuck for ideas. The columns of my matrix in which some data is stored are of this form: X1 Y1 X2 Y2 X3 Y3 ... Xn Yn with n~100. I would like to look at just the X values (i.e. odd column numbers). Is there an easy way to loop round extracting only these columns? Any help would be appreciated. Thank you. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Replacing values in a data.frame/matrix
how about: m - matrix(c(1,2,3,2,1,3,3,1,2), ncol = 3, byrow = TRUE) perm - c(1, 3, 2) out - perm[m] dim(out) - dim(m) out I hope it helps. Best, Dimitris On 3/8/2011 4:05 PM, Thaler, Thorn, LAUSANNE, Applied Mathematics wrote: Hi all, Suppose we have the following matrix m- matrix(c(1,2,3,2,1,3,3,1,2), ncol = 3, byrow=T) where in each row each number occurs only once. I'd like to define a permutation, e.g. 1 - 2, 2 - 1, 3 - 3 and apply it to the matrix. Thus, the following matrix should result: m.perm- matrix(c(2,1,3,1,2,3,3,2,1), ncol = 3, byrow=T) i.e. each 1 should map to 2 and vice verse while 3 maps to itself. What I've done so far is: permutateMatrix- function(mat, perm=NULL) { values- 1:NCOL(mat) if (is.null(perm)) { perm- sample(values) } newmat- replace(mat, sapply(values, function (val) which(mat==val)), rep(perm, each=NROW(mat))) return(list(mat.perm=newmat, perm=perm)) } perm is the permutation vector: 1 maps to the first element of perm, 2 to the second and so on. Thus, for the example we would use perm- c(2,1,3) all.equal(m.perm, permutateMatrix(m, perm)$mat.perm) # TRUE What do you think of this solution? Are there more elegant ways of doing that? Any comments appreciated. Thanks + BR, Thorn __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] sum of digits or how to slice a number into its digits
one way is using function strsplit(), e.g., x - c(100100110, 1001001, 1101, 00101) sapply(strsplit(x, ), function (x) sum(x == 1)) I hope it helps. Best, Dimitris On 3/4/2011 2:18 PM, drflxms wrote: Dear R colleagues, I face a seemingly simple problem I couldn't find a solution for myself so far: I have to sum the digits of numbers. Example: 1010 -2 100100110 - 4 Unfortunately there seems not to be a function for this task. So my idea was to use sum(x) for it. But I did not figure out how to slice a number to a vector of its digits. Example (continued from above): 1010 - c(1,0,1,0) 100100110 - (1,0,0,1,0,0,1,1,0). Does anyone know either a function for calculating the sum of the digits of a bumber, or how to slice a number into a vector of its digits as described above? I'd appreciate any kind of help very much! Thanx in advance and greetings from cloudy Munich, Felix __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Sweave trim output
Dear All, I'd like to trim the output produced in a Sweave code chunk. For instance, in fit - lm(conc ~ . - Plant, data = CO2) summary(fit) I'd like, skip the info after the coefficients' table, and possibly replace it with '...'. I've created this small function to do this, which is based on capture.output(): trim.output - function (x, lines, above = FALSE) { if (above) cat(\n...\n\n) cat(paste(x[lines], collapse = \n)) cat(\n\n...\n) } out - capture.output(summary(fit)) trim.output(out, 1:13) but I was wondering if there is an *official* way to do this. Thanks in advance. Best, Dimitris -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] if statements on vectors
there are also vectorized logical operators; have a look at the help page ?'', and try this: trend1 - c(1,1,1,1,1,1,-1,-1,-1,-1,-1,-1) trend2 - c(1,1,1,1,1,1,1,1,1,1,1,-1) position - as.numeric((trend1 == 1 trend2 == 1) | (trend1 == -1 trend2 == -1)) position I hope it helps. Best, Dimitris On 2/24/2011 9:41 AM, Kushan Thakkar wrote: I have two vectors: both have possible values of 1,-1, or 0 trend1- c(1,1,1,1,1,1,-1,-1,-1,-1,-1,-1) trend2- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,) i want to create a third vector that is conditional upon these two vectors: if (trend2 == 1 trend1 == 1) {position- 1} elseif (trend2 == -1 trend1== -1) {position- 1} else {position- 0} based on this two conditions, the position vector should have a value: position: 1,1,1,1,1,1,0,0,0,0,0,0 Is there a way to do this in R without explicitly going through a for loop? The if condition doesn't work as it only accept one condition (aka no vectors). [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] identify an element in a column
try this: x - 1:10 y - 11:20 z - cbind(x, y) ind - x == 5 z[ind, y] - z[ind, y] - 1 z I hope it helps. Best, Dimitris On 2/22/2011 6:18 PM, Hongwei Dong wrote: Hi, R users, I'm wondering if I can identify an element in a column by an element in another column. For example: x-1:10 y-11:20 z-cbind(x,y) z x y [1,] 1 11 [2,] 2 12 [3,] 3 13 [4,] 4 14 [5,] 5 15 [6,] 6 16 [7,] 7 17 [8,] 8 18 [9,] 9 19 [10,] 10 20 What I want to do is: when x=5, y=y-1 Anyone can tell me how to do this? Thanks. Gary [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Subset according to groups NA proportion within specific variables
one way is the following: 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) ) na.ind - sapply(DF[-1], is.na) na.ind - ave(na.ind, rep(DF$x, 3), col(na.ind)) 0.5 DF[apply(na.ind, 1, all), ] I hope it helps. Best, Dimitris On 2/21/2011 12:20 PM, D. Alain wrote: Dear R-List, I have a dataframe with one grouping variable (x) and three response variables (y,z,w). 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)) df xyz w 1 0.29306106 3 1 1 0.54797780 4 2 1 -1.38365548 5 3 2 -0.20407986 NA3 2 -0.87322574 NA4 2 -1.23356250 NA3 2 0.43929374 NA5 3 1.16405483 1NA 3 1.07083464 2 5 3 -0.67463191 1NA 3 -0.66410552 2 7 3 -0.02543358 1 8 Now I want to make a new dataframe df.sub comprising only cases pertaining to groups, where the overall proportion of NAs in either of the response variables y,z,w does not exceed 50%. In the above example, e.g., this would be a dataframe with all cases of the groups 1 and 3 (since there are 100% NAs in z for group 2) df.sub xyz w 1 0.29306106 3 1 1 0.54797780 4 2 1 -1.38365548 5 3 3 1.16405483 1NA 3 1.07083464 2 5 3 -0.67463191 1NA 3 -0.66410552 2 7 3 -0.02543358 1 8 Please excuse me if the problem has already been treated somewhere, but so far I was not able to find the right threat for my question in RSeek. Can anyone help? Thanks in advance! D. Alain [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to change dataframe to tables
say, 'Dat' is your data frame, then one way to do it is: with(Dat, tapply(freq, list(hair, eye, sex), c)) I hope it helps. Best, Dimitris On 2/18/2011 8:50 AM, Lao Meng wrote: The data is in the attachment. What I wanna get is: , , Sex = Male Eye HairBrown Blue Hazel Green Black32 1110 3 Brown53 502515 Red 10 10 7 7 Blond 3 30 5 8 , , Sex = Female Eye HairBrown Blue Hazel Green Black369 5 2 Brown66 342914 Red 167 7 7 Blond 4 64 5 8 Then,how to change the dataframe to tables? Many thanks for your help. Best __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] removing lower and upper quantiles from an arry
have a look at the help page for ?'', try also this: i - 1:20 qs - quantile(i, c(.25, 0.75)) i[i qs[1] i qs[2]] I hope it helps. Best, Dimitris On 2/17/2011 11:08 AM, Maas James Dr (MED) wrote: I'm trying to work out the simplest way to remove the upper and lower quantiles, in this case upper and lower 25% from an array. I can do it in two steps but when I try it in one, it fails. Is there something simple missing from my syntax or are there other simple elegant way to accomplish this? Thanks J i-1:20 i2- i[iquantile(i,.75)] i3- i[iquantile(i,.25)] i4- i[quantile(i,.25) i quantile(i,.75)] Error: unexpected '' in i4- i[quantile(i,.25) i === Dr. Jim Maas University of East Anglia __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Count factor if
try this: x - factor(c('a','a','a','a','a','b','b','b','c','d','d','d')) y - c(1,3,6,8,8,3,4,7,5,6,7,10) X - data.frame(x, y) cbind(X, z = ave(X$y, X$x, FUN = function (x) match(x, unique(x)) - 1)) I hope it helps. Best, Dimitris On 2/17/2011 11:15 AM, mathijsdevaan wrote: Here is a method that works despite generating a warning: cbind(X, z = ave(X$y, X$x, FUN = seq) - 1) David Winsemius, MD West Hartford, CT I was happy a bit too early. There's still an error: x-as.factor(c('a','a','a','a','a','b','b','b','c','d','d','d')) y-c(1,3,6,8,8,3,4,7,5,6,7,10) X-data.frame(x,y) X x y 1 a 1 2 a 3 3 a 6 4 a 8 5 a 8 6 b 3 7 b 4 8 b 7 9 c 5 10 d 6 11 d 7 12 d 10 cbind(X, z = ave(X$y, X$x, FUN = seq) - 1) x y z 1 a 1 0 2 a 3 1 3 a 6 2 4 a 8 3 5 a 8 4 6 b 3 0 7 b 4 1 8 b 7 2 9 c 5 0 10 d 6 0 11 d 7 1 12 d 10 2 Using this function provides a value of 4 in row 5, column c, while it should be 3. Is there another way I can solve my problem? Thanks! M -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Axis positions
try using box(), e.g., plot(x, y, axes = FALSE, type = 'l') axis(1, tck = -0.02) axis(2, tck = -0.02) box(bty = l) I hope it helps. Best, Dimitris On 2/16/2011 10:45 AM, Filoche wrote: Hi everyone. I would like to modify the axis on my plot. First, I would like to make a plot without the box. so I use : plot(x,y, axes = FALSE, type = 'l') Then, I call : axis(1, tck = -0.02) axis(2, tck = -0.02) to have X and Y axis appear. However, I would like them to join at the origin instead of having a space between the 2 axis. I can't find the parameter to modify to get this result. Secondly, is it possible to center X and Y axis so the origin is right in the center of the graph. With regards, Phil -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to join matrices of different row length from a list
try this: matLis - list(matrix(1:4, 2, 2), matrix(1:6, 3, 2), matrix(2:1, 1, 2)) n - max(sapply(matLis, nrow)) do.call(cbind, lapply(matLis, function (x) rbind(x, matrix(, n-nrow(x), ncol(x) I hope it helps. Best, Dimitris On 1/6/2011 11:56 AM, emj83 wrote: Hi, I have several matrix in a list, for example: e [[1]] [,1] [,2] [1,]13 [2,]24 [[2]] [,1] [,2] [1,]14 [2,]25 [3,]36 [[3]] [,1] [,2] [1,]21 I would like to join them by column i.e. [,1] [,2] [,3] [,4][,5] [,6] [1,]13 1421 [2,]24 25 NA NA [3,] NA NA 36 NA NA I have tried do.call(cbind,e) but I get this error message as the rows are of different length- Error in function (..., deparse.level = 1) : number of rows of matrices must match (see arg 2) Can anyone advise me please? Thanks Emma -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] [R-pkgs] package JM -- version 0.8-0
Dear R-users, I'd like to announce the release of the new version of package JM (soon available from CRAN) for the joint modeling of longitudinal and time-to-event data using shared parameter models. These models are applicable in mainly two settings. First, when focus is in the survival outcome and we wish to account for the effect of a time-dependent covariate measured with error. Second, when focus is in the longitudinal outcome and we wish to correct for nonrandom dropout. New features include: * for all joint models fitted by JM there is now the option to use a pseudo adaptive Gauss-Hermite rule. This is much faster than the default option and produces results of equal or better quality. It can be invoked via the 'method' argument of jointModel() by specifying aGH instead of GH, e.g., 'method = piecewise-PH-aGH' instead of 'method = piecewise-PH-GH'. * function rocJM() has been added that estimates time-dependent sensitivity and specificity (and the corresponding time-dependent ROCs and AUCs) for longitudinal markers under the joint modeling framework. The function also allows for several predictions rules. * the new argument 'interFact' added in jointModel() allows the specification of interaction terms between the longitudinal outcome and baseline covariates. * the new arguments 'parameterization' and 'derivForm' added in jointModel() allow the specification of more general association structures between the longitudinal marker and the risk for an event. For instance, if a random intercepts and random slopes mixed model has been postulated for the longitudinal outcome, then this argument can be used to also associate the risk for an event with the subject-specific slopes. * a predict method has been added. Currently this only calculates fitted average longitudinal evolutions based on the information provided in the 'newdata' argument. More information can be found in the corresponding help files, and examples at http://rwiki.sciviews.org/doku.php?id=packages:cran:jm As always, any kind of feedback (e.g., questions, suggestions, bug-reports, etc.) is more than welcome. Best, Dimitris -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ ___ R-packages mailing list r-packa...@r-project.org https://stat.ethz.ch/mailman/listinfo/r-packages __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Concat two rows
have a look at function paste(), e.g., Dat - data.frame(A = c(aaa, bbb, ccc, ddd), B = c(111, 222, 333, 444)) Dat$C - paste(Dat$A, Dat$B, sep = .) Dat I hope it helps. Best, Dimitris On 11/30/2010 9:30 AM, alcesgabbo wrote: I have a table like this: A B aaa 111 bbb 222 ccc 333 ddd 444 and I would like a new row C: A B C aaa 111 aaa.111 bbb 222 bbb.222 ccc 333 ccc.333 ddd 444 ddd.444 How can I do this ??? Thanks Alberto -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Applying function to elements of matrices in a list
try this: Reduce(+, x) / length(x) Best, Dimitris On 11/25/2010 3:42 PM, statmobile wrote: Hello R-help, Please cc me on all responses, as I only receive summary emails from this list. I'm wondering if anybody has any tips on how to accomplish this efficiently. I have a list of matrices, and I'm trying to get the mean of the [i,j]'th element of each matrix in a list. So if I have a list of matrices, say x - list(a=matrix(rnorm(4),nrow=2),b=matrix(rnorm(4),nrow=2)) How would I get a 2x2 matrix, where the i,j'th element would be the mean across the the list of each of the i,j'th elements in the list? That is, where the [1,2] element would be the average of a[1,2] and b[1,2]. Of course my list and matrices are much larger, and I was hoping there would be some trick with lapply that I may be missing here. Thanks, Brian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] help: program efficiency
one way is the following: a - c(2,1,1,3,3,3,4) d - unlist(sapply(rle(a)$length, function (x) if (x 1) seq(0.01, by = 0.01, len = x) else 0)) a + d I hope it helps. Best, Dimitris On 11/25/2010 3:49 PM, randomcz wrote: hey guys, I am working on a function to make a duplicated value unique. For example, the original vector would be like : a = c(2,1,1,3,3,3,4) I'll like to transform it into: a.nodup = 2, 1.01, 1.02, 3.01, 3.02, 3.03, 4 basically, find the duplicates and assign a unique value by adding a small amount and keep it in order. I come up with the following codes, but it runs slow if t is large. Is there a better way to do it? nodup = function(t) { t.index=0 t.dup=duplicated(t) for (i in 2:length(t)) { if (t.dup[i]==T) t.index=t.index+0.01 else t.index=0 t[i]=t[i]+t.index } return(t) } -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Change the class of columns in a data frame
you can do it like this: # example data DF - data.frame(nominal = rep(2, 5), ordinal = c(1,2,2,1,2)) DF$nominal - factor(DF$nominal) DF$ordinal - ordered(DF$ordinal) DF str(DF) I hope it helps. Best, Dimitris On 11/24/2010 8:27 PM, Άγνωστος μέσα στο πλήθος wrote: Hi. First of all, excuse me if I do any mistakes, but English is not a language I use very often. I have a data frame with numbers. A small part of the data frame is this: nominal ordinal 2 2 2 1 2 1 2 2 So, I want to use the gower distance function on these numbers. Here ( http://rgm2.lab.nig.ac.jp/RGM2/R_man-2.9.0/library/StatMatch/man/gower.dist.html) says that in order to use gower.dist, all nominal variables must be of class factor and all ordinal variables of class ordered. By default, all the columns are of class integer and mode numeric. In order to change the class of the columns, i use these commands: DF=read.table(clipboard,header=TRUE,sep=\t) *# I select all the cells and I copy them to the clipboard. Then R, with this command, reads the data from there.* MyHeader=names(DF) *# I save the headers of the data frame to a temp matrix* for (i in 1:length(DF)) {if (MyHeader[[i]]==nominal) DF[[i]]=as.factor(DF[[i]])} for (i in 1:length(DF)) {if (MyHeader[[i]]==ordinal) DF[[i]]=as.ordered(DF[[i]])}* * The first for/if loop changes the class from integer to factor, which is what I want, but the second changes the class of ordinal variables to: ordered factor. I need to change all the columns with the header ordinal to ordered, as the gower.dist function says. Thanks in advance, B.T. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] apply over list of data.frames
try this: DF.lis - list( one = data.frame(x = c(1,2,3), y = c(1,2,2), z = c(3,1,1)), two = data.frame(x = c(2,5,2), y = c(2,3,1), z = c(4,1,2)) ) Reduce(+, DF.lis) / length(DF.lis) I hope it helps. Best, Dimitris On 11/24/2010 8:37 PM, Tim Howard wrote: R users, This probably involves a simple incantation of one of the flavors of apply... that I can't yet figure out. Consider a list of data frames. I'd like to apply a function (mean) across the list and return a dataframe of the same dimensions where each cell represents the mean of that cell across all dataframes. # set up the list x- vector(list,2) names(x)- c(one,two) # add data to the list for(i in 1:2){ y = i^2 x[[i]]- data.frame(a=c(y,2*y,3*y),b=c(y+1,y+2,y+3),c=c(2*y+1,2*y+2,2*y+3)) } #show the list x $one a b c 1 1 2 3 2 2 3 4 3 3 4 5 $two a b c 1 4 5 9 2 8 6 10 3 12 7 11 #the result should be a b c 1 2.5 3.5 6 2 5 4.5 7 3 7.5 5.5 8 Can anyone direct me down the right path? Thanks in advance Tim Howard [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] [beginner] how to run *.r script from graphic interface (R Console window) ?
Have a look at function source(), i.e., type in the R console ?source I hope it helps. Best, Dimitris On 11/21/2010 1:23 PM, madr wrote: Is there a fuction like open(path/to/file), readfile or sth like that that would run code from R file just like I was it typing myself into R Console window ? -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] slicing list with matrices
try this: m - matrix(1:9, nrow=3, dimnames=list(LETTERS[1:3], letters[1:3])) l - list(m1=m, m2=m*2, m3=m*3) lapply(l, [, 1, 1) # or sapply(l, [, 1, 1) I hope it helps. Best, Dimitris On 11/17/2010 3:01 PM, soeren.vo...@eawag.ch wrote: A list contains several matrices. Over all matrices (list elements) I'd like to access one matrix cell: m- matrix(1:9, nrow=3, dimnames=list(LETTERS[1:3], letters[1:3])) l- list(m1=m, m2=m*2, m3=m*3) l[[3]] # works l[[3]][1:2, ] # works l[[1:3]][1, 1] # does not work How can I slice all C-c combinations in the list? Sören -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] stacking consecutive columns
one approach is the following: # say 'Data' is your data frame DataNew - reshape(Data, direction = long, varying = list(2:length(Data))) DataNew$year - rep(2000:2009, each = 12) DataNew I hope it helps. Best, Dimitris On 11/17/2010 3:03 PM, Graves, Gregory wrote: I have a file, each column of which is a separate year, and each row of each column is mean precipitation for that month. Looks like this (except it goes back to 1964). monthX2000 X2001 X2002 X2003 X2004 X2005 X2006 X2007 X2008 X2009 11.600 1.010 4.320 2.110 0.925 3.275 3.460 0.675 1.315 2.920 22.960 3.905 3.230 2.380 2.720 1.880 2.430 1.380 2.480 2.380 31.240 1.815 1.755 1.785 1.250 3.940 10.025 0.420 2.845 2.460 43.775 1.350 2.745 0.170 0.710 2.570 0.255 0.425 4.470 1.250 54.050 1.385 5.650 1.515 12.005 6.895 7.020 4.060 7.725 2.775 68.635 8.900 15.715 12.680 16.270 12.605 7.095 7.025 10.465 7.345 75.475 7.955 7.880 6.670 7.955 7.355 5.475 5.650 7.255 7.985 88.435 5.525 7.120 6.250 7.150 7.610 5.525 6.500 6.275 10.405 95.855 7.830 7.250 7.355 9.715 7.850 6.385 7.960 4.485 7.250 10 7.965 11.915 6.735 8.125 7.855 10.465 4.340 6.165 2.400 3.240 11 1.705 1.525 0.905 1.670 1.840 2.100 0.255 2.830 4.425 1.645 12 2.335 0.840 0.795 1.890 0.145 1.700 0.260 2.160 2.300 2.220 What I want to do is to stack 2008 data underneath 2009 data, 2007 under 2008, 2006 under 2007, etc. I have so far figured out that I can do this with the following clumsy approach: a=stack(yearmonth,select=c(X2009,X2008)) b=stack(yearmonth,select=c(X2008,X2007)) x=as.data.frame(c(a,b)) write.table(x,clipboard,sep= ,col.names=NA) #then paste this back into Excel to get this values ind values.1ind.1 1 0.275 X2009 1.285 X2008 2 0.41X2009 3.85X2008 3 1.915 X2009 3.995 X2008 4 1.25X2009 3.845 X2008 5 8.76X2009 2.095 X2008 6 8.65X2009 8.29X2008 7 7.175 X2009 9.405 X2008 8 7.19X2009 13.44 X2008 9 8.13X2009 7.245 X2008 10 1.46X2009 5.645 X2008 11 2.56X2009 0.535 X2008 12 5.01X2009 1.225 X2008 13 1.285 X2008 0.72X2007 14 3.85X2008 1.89X2007 15 3.995 X2008 1.035 X2007 16 3.845 X2008 2.86X2007 17 2.095 X2008 3.785 X2007 18 8.29X2008 9.62X2007 19 9.405 X2008 9.245 X2007 20 13.44 X2008 5.595 X2007 21 7.245 X2008 8.4 X2007 22 5.645 X2008 6.705 X2007 23 0.535 X2008 1.47X2007 24 1.225 X2008 1.665 X2007 Is there an easier, cleaner way to do this? Thanks. Gregory A. Graves, Lead Scientist Everglades REstoration COoordination and VERification (RECOVER) Restoration Sciences Department South Florida Water Management District Phones: DESK: 561 / 682 - 2429 CELL: 561 / 719 - 8157 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Calculate Mean from List
one way is the following: A - replicate(10, cor(matrix(rnorm(30), 10, 3)), simplify = FALSE) triA - sapply(A, function (m) m[upper.tri(m)]) rowMeans(triA, na.rm = TRUE) I hope it helps. Best, Dimitris On 11/9/2010 9:23 AM, Suphajak Ngamlak wrote: Dear all, I have a list of correlation coefficient matrixes. Each matrix represents one date. For example A[[1]] A B C A 1 0.2 0.3 B 0.2 1 0.4 C 0.3 0.4 1 A[[2]] A B C A 1 0.5 0.6 B 0.5 1 0.7 C 0.6 0.7 1 A[[n]] I would like to calculate the mean of correlation coefficient from the whole time series, i.e. Average cor(A,B) = (A[[1]][2,1] + A[[2]] [2,1] + ... + A[[n]] [2,1])/n Average cor(A,C) = (A[[1]][3,1] + A[[2]] [3,1] + ... + A[[n]] [3,1])/n Average cor(B,C) = (A[[1]][3,2] + A[[2]] [3,2] + ... + A[[n]] [3,2])/n Please note that some cells are NA; so I need to remove them when calculating average. How could I get this efficiently? Thank you Best Regards, Suphajak Ngamlak Equity and Derivatives Trading Phatra Securities Public Company Limited Tel: (66)2-305-9179 Email: supha...@phatrasecurities.com [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Row-wise recurive function call
try this: apply(data.matrix(a), 1, playFn) I hope it helps. Best, Dimitris On 11/9/2010 8:32 AM, Santosh Srinivas wrote: Dear Group, I have a following dataset: a A B C D 1 22 3 31 40 2 26 31 36 32 3 3 7 49 16 4 24 40 27 26 5 20 45 47 0 6 34 43 11 18 7 48 48 24 2 8 3 16 39 48 9 20 49 7 21 10 17 36 47 10 dput(a) structure(list(A = c(22L, 26L, 3L, 24L, 20L, 34L, 48L, 3L, 20L, 17L), B = c(3L, 31L, 7L, 40L, 45L, 43L, 48L, 16L, 49L, 36L), C = c(31L, 36L, 49L, 27L, 47L, 11L, 24L, 39L, 7L, 47L), D = c(40L, 32L, 16L, 26L, 0L, 18L, 2L, 48L, 21L, 10L)), .Names = c(A, B, C, D), class = data.frame, row.names = c(NA, -10L)) I have a function that works off EVERY individual ROW to throw a result. Like playFn- function (x){ + result = ((x$A+6*x$B)/(3*x$C)+20)*x$D + return(result) + } I want to apply the function for every row can I use an apply function ... tried but not been able to ... e.g. print(rapply(a,playFn)) Please advise. Thanks, S __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] regular exprs
Dear All, I would appreciate any help with the following: given the vector 'x' x - c(Ass1, Ass.s1, Ass2, Ass.s2) I would like to pick up the positions where the character string contains Ass but does not contain Ass.s, so for 'x' that would be positions 1 and 3. I guess this could be programmed around grep() using a suitable regular expression, but I haven't managed to succeed. Thanks in advance. Best, Dimitris -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] individual intercept and slope
Have a look at function(s) lmList() from packages lme4 or nlme. I hope it helps. Best, Dimitris On 11/2/2010 3:14 PM, Rosario Garcia Gil wrote: Hello I would like to extract the estimates for the intercept and slope by individual for growth from a lm fit. Any advice? Individual Time point Height 1 1 10 1 2 11 1 3 23 1 4 15 1 5 21 1 6 23 2 1 24 2 2 12 2 3 9 2 4 10 2 5 11 2 6 10 ... Thanks __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Fitting GLM with BFGS algorithm
for instance, for logistic regression you can do something like this: # simulate some data x - cbind(1, runif(100, -3, 3), rbinom(100, 1, 0.5)) y - rbinom(100, 1, plogis(c( x%*% c(-2, 1, 0.3 # BFGS from optim() fn - function (betas, y, x) { -sum(dbinom(y, 1, plogis(c(x %*% betas)), log = TRUE)) } optim(rep(0, ncol(x)), fn, x = x, y = y, method = BFGS) # IWLS from glm() glm(y ~ x[, -1], family = binomial) You can also improve it by providing the minus score vector as a third argument to optim(). I hope it helps. Best, Dimitris On 10/26/2010 1:38 PM, justin bem wrote: Dear all, By default the glm function in the stats package use IWLS. How can I fit a glm model using BFGS algorithm ? Justin BEM BP 1917 Yaoundé Tél (237) 76043774 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] create sequence of numbers
one way is to use the sign() function, e.g., a - 2 b - 3 seq(a, b, by = sign(b-a)*0.5) a - 3 b - 2 seq(a, b, by = sign(b-a)*0.5) I hope it helps. Best, Dimitris On 10/22/2010 11:58 AM, Alaios wrote: Hello. I want to create some sequence of numbers . So far I used sequence which does not work always seq(CRagent[[1]]$xy[1],CRagent[[2]]$xy[1],by=0.01) Error in seq.default(CRagent[[1]]$xy[1], CRagent[[2]]$xy[1], by = 0.01) : wrong sign in 'by' argument Calls: seq - seq.default if the parameters are in descending form. The ideal would be to be able to use seq like this seq(2,3,by=0.5) -- 2,2.5,3 seq(3,2,by=0.5)--3,2.5,2 Could you please help me implement this? I wouldl ike to thank you in advance for your help Best Regards Alex [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how fit linear model with fixed slope?
yes, you can use an offset, e.g., x - runif(100, -3, 3) y - 2 + x + rnorm(100) lm(y ~ x) lm(y ~ offset(x)) I hope it helps. Best, Dimitris On 10/22/2010 4:13 PM, Czerminski, Ryszard wrote: I want to fit a linear model with fixed slope e.g. y = x + b (instead of general: y = a*x + b) Is it possible to do with lm()? Regards, Ryszard -- Confidentiality Notice: This message is private and may ...{{dropped:11}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to check for missing report pages per client
one approach is the following: DF - read.table(textConnection( client page 730001 46101 730001 46102 730019 46101 730035 46101 730040 46101 730040 46102 730040 46104 730040 46105 730052 46101 730052 46102 730074 46101 730074 46102 730074 46103), header = TRUE) closeAllConnections() DF$diff - with(DF, ave(page, client, FUN = function (x) c(0, diff(x))) ) DF[DF$diff 1, ] unique(DF$client[DF$diff 1]) I hope it helps. Best, Dimitris On 10/21/2010 8:47 AM, Pauline wrote: Hi, Not sure how to go about checking for missing report pages per client. See example below; I like to extract client 730040, because page 46103 is missing. client page 730001 46101 730001 46102 730019 46101 730035 46101 730040 46101 730040 46102 730040 46104 730040 46105 730052 46101 730052 46102 730074 46101 730074 46102 730074 46103 I appreciate any help, Pauline [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.