similar to: lapply, sapply

Displaying 20 results from an estimated 30000 matches similar to: "lapply, sapply"

2010 Sep 26
1
formatting data for predict()
I'm trying to get predicted probabilities out of a regression model, but am having trouble with the "newdata" option in the predict() function. Suppose I have a model with two independent variables, like this: y=rbinom(100, 1, .3) x1=rbinom(100, 1, .5) x2=rnorm(100, 3, 2) mod=glm(y ~ x1 + x2, family=binomial) I can then get the predicted probabilities for the two values of
2006 Sep 20
1
Simulation help
I'm trying to simulate trend data over a five year period. I want different trend profiles...the simplest being a linear trend. I've been using the following code: patBdta1 <- NULL for(i in 1:100) patBdta1 <- rbind(patBdta1,c(yr1= mean(rbinom(50,1,.50)), yr2 =mean(rbinom(50,1,.51)), yr3 =mean(rbinom(50,1,.52)),
2005 Jun 20
1
(no subject)
R friends, I am using R 2.1.0 in a Win XP . I have a problem working with lists, probably I do not understand how to use them. Lets suppose that a set of patients visit a clinic once a year for 4 years on each visit a test, say 'eib' is performed with results 0 or 1 The patients do not all visit the clinic the 4 times but they missed a lot of visits. The test is considered positive if it
2008 Dec 16
1
simulate binary markov chain
Hi all, I was hoping somebody may know of a function for simulating a large binary sequence (length >10 million) using a (1st order) markov model with known (2x2) transition matrix. It needs to be reasonably fast. I have tried the following; mc<-function(sq,P){ s<-c() x<-row.names(P) n<-length(sq) p1<-sum(sq)/n s[1] <- rbinom(1,1,p1); for ( i in 2:n){ s[i]
2010 Feb 17
1
lapply to apply a function using a vector
Hi, First, thank you all for your help. Here is my problem (simplified): Say I have a list: a=list(matrix(50,nrow=5,ncol=5),matrix(25,nrow=5,ncol=5),matrix(10,nrow=5,ncol=5)) I'd like to use rbinom with a different probability for each matrix. I tried: b=c(.8,.1,.9) brep=rep(b,each=25) lapply(a,function(a) rbinom(25,a,brep)) but that doesn't work-- it just uses the first value of b
2013 Jul 22
4
How to split two levels several times?
Hi, I have a small problem with the function split() and would appreciate your help. I have a table called ?XXX? with 2 columns and 49 rows. The 49 rows belong to 8 different levels (electrode1, ...,electrode8). I want to split the table always at the row where ?electrode1? starts again so that I can export 7 individual dataframes (numbered ?dataframe1? to ?dataframe7?) which contain always
2007 Apr 12
1
making a counter of consecitive positive cases in time series
Hi all..RCounters! I´m working with standarized time series, and i need make a counter of consecutives positves numbers to make a cumulative experimental funtion. I have x: the time series (0,1) and y: my counter, i have this for step. What is wrong?.. any can help me please! x<-rbinom(15,1,.3) y<-NULL;s<-0 for (i in 1: length (x)) {if (x[i]>0) {s<-s+x[i] s=0}
2005 Feb 28
2
A problem about outer()
Dear all, I have something about function outer() that I can't understand. Just see the following example. The two NaNs are due to 0/0, but I can't figure out the cause of the last two errors. I wonder if some one can explain this for me. ___________________________________________________________________ > sx=rbinom(10,1,0.5);ot=rbinom(10,1,0.5);ag <- rbinom(10,100,0.3);ho <-
2007 Aug 09
1
How to apply functions over rows of multiple matrices
Dear ExpRts, I would like to perform a function with two arguments over the rows of two matrices. There are a couple of *applys (including mApply in Hmisc) but I haven't found out how to do it straightforward. Applying to row indices works, but looks like a poor hack to me: sens <- function(test, gold) { if (any(gold==1)) { sum(test[which(gold==1)]/sum(which(gold==1))) } else NA }
2008 Jul 09
3
rbinom for a matrix
I have a large matrix full of probabilities; I would like to convert each probability to a 1 or a 0 using rbinom. How can I do this on the entire matrix? The matrix was converted from a raster ArcMap dataset, so the matrix is essentially a map. Because of this, I have no column headings. Thanks! -- View this message in context: http://www.nabble.com/rbinom-for-a-matrix-tp18366867p18366867.html
2009 Jun 18
1
Running a function through a vector with the scalars as arguments
I have a vector of probabilities that I would like to feed to rbinom() and have it return a 1 or 0 for each element. I have tried simply using the entire vector as the argument hoping R would understand. Instead I get a scalar: theProbs <- runif(10,0,1) youreON <- rbinom(1, 1, theProbs) Next I tried using the apply function but I could not get that to work either: youreON2 <-
2008 Nov 20
3
Error with lapply
I have written following codes, with intention to get a list with values 1,2,9,16 : fn <- function(i) return(i^2) lapply(1:4, fn, i) However I got following error : Error in FUN(1:4[[1L]], ...) : unused argument(s) (1) Can anyone please tell me what will be the correct code here? Regards, -- View this message in context: http://www.nabble.com/Error-with-lapply-tp20605066p20605066.html
2004 Mar 11
3
making operators act on rows of a data frame
Dear R helpers, I wish to use the "sum" operator for each row of a data frame. However, it appears that the operator acts on the entire data frame, over all columns. What is the best way to obtain row- wise operation? The following code shows my attempts so far, and their problems:- test1=array(rbinom(120,1,0.5),c(20,3)) test1[,3]=NA sum(test1[,1:2]) test1[,3][sum(test1[,1:2])>=2]=1
2007 Aug 10
1
[Fwd: Re: How to apply functions over rows of multiple matrices]
[Apologies to Gabor, who I sent a personal copy of the reply erroneously instead of posting to List directly] [...] > Perhaps what you really intend is to > take the average over those elements in each row of the first matrix which correspond to 1's in the second in the corresponding > row of the second. In that case its just: > > rowSums(newtest * goldstandard) /
2012 Jun 18
6
Trying to speed up an if/else statement in simulations
Dear R-help, I am trying to write a function to simulate datasets of size n which contain two time-to-event outcome variables with associated 'Event'/'Censored' indicator variables (flag1 and flag2 respectively). One of these indicator variables needs to be dependent on the other, so I am creating the first and trying to use this to create the second using an if/else statement.
2007 Feb 15
2
How to speed up or avoid the for-loops in this example?
Any advice, tips, clues or pointers to resources on how best to speed up or, better still, avoid the loops in the following example code much appreciated. My actual dataset has several tens of thousands of rows and lots of columns, and these loops take a rather long time to run. Everything else which I need to do is done using vectors and those parts all run very quickly indeed. I spent quite a
2011 Jan 15
3
get list element names within lapply / sapply call
Hi all, I would like to iterate through a list with named elements and access the names within an lapply / sapply call. One way to do this is iterate through the names and index the list with the name. Is there a way to iterate through the list elements themselves and access the element names within in the function? For example, mylist <-
2007 Nov 12
4
a repetition of simulation
Hello, I have a simple (?) simulation problem. I'm doing a simulation with logistic model and I want to reapet it 600 times. The simulation looks like this: z <- 0 x <- 0 y <- 0 aps <- 0 tiss <- 0 for (i in 1:500){ z[i] <- rbinom(1, 1, .6) x[i] <- rbinom(1, 1, .95) y[i] <- z[i]*x[i] if (y[i]==1) aps[i] <- rnorm(1,mean=13.4, sd=7.09) else aps[i] <-
2007 Oct 24
2
analytical solution to Sum of binominal distributed random numbers?
Frede Aakmann T?gersen wrote: > Perhaps > > http://stinet.dtic.mil/cgi-bin/GetTRDoc?AD=ADA266969&Location=U2&doc=GetTRDoc.pdf > > is something that you can use? Thanks a lot - that might help. Rainer > > > > Best regards > > Frede Aakmann T?gersen > Scientist > > > UNIVERSITY OF AARHUS > Faculty of Agricultural Sciences > Dept.
2007 Oct 23
1
Sum of binominal distributed random numbers
Hi I have two vectors, prob and size, and I want to add the random deviates of these two, i.e. sum( sapply( 1:length(prob), function(n){ rbinom(1, size(n), prob(n) } ) ) My problem is that I have to do this for a large number of value combinations. Is there a faster way of doing this? Rainer