Re: [R] sapply function and poisson distribution

2015-01-05 Thread peter dalgaard
On 05 Jan 2015, at 00:21 , Pete Brecknock wrote: > n <- c(1,2,3,4,5) > lambda <- c(0.1,0.8,1.2,2.2,4.2) > > mapply(function(x,y) rpois(x,y), n, lambda) Yes. I'd throw in a SIMPLIFY=FALSE to avoid getting results in a different format if n is constant (then again, sapply() in the original q

Re: [R] sapply function and poisson distribution

2015-01-04 Thread Pete Brecknock
dimnik wrote > thank you for your answer.Yes,that sounds right.I thought the same thing > but the problem is how can i generalize the command for every vector of > numbers not only for the specific example?not only for c(1,2),c(0.1,0.8). > > 2015-01-04 0:45 GMT+00:00 Pete Brecknock [via R] < > ml

Re: [R] sapply function and poisson distribution

2015-01-04 Thread dimnik
thank you for your answer.Yes,that sounds right.I thought the same thing but the problem is how can i generalize the command for every vector of numbers not only for the specific example?not only for c(1,2),c(0.1,0.8). 2015-01-04 0:45 GMT+00:00 Pete Brecknock [via R] < ml-node+s789695n4701358...@n

Re: [R] sapply function and poisson distribution

2015-01-03 Thread Pete Brecknock
dimnik wrote > i want to find a functionthattakes in two vectors of numbers > thathave > the same > length.The output should be a listof vectors, where each vector > is a > sequence of > randomly generated Poisson variableswher

Re: [R] sapply returning list instead of matrix

2014-02-03 Thread S Ellison
> I can read the documentation, I see why it happens, but who in their right > mind would design a function this way? I think you're possibly starting from the wrong perspective, or at least it might be useful to look at it from a different perspective. In many cases, such as simulations, lappl

Re: [R] sapply returning list instead of matrix

2014-02-02 Thread chris warth
Can I follow-up with what I've learned about my own myopia regarding sapply()? First, I appreciate all the feedback. After thinking about it for a while I realized R designers have often chosen to accommodate interactive usage, and in that context, sapply() returning different types makes perfe

Re: [R] sapply returning list instead of matrix

2014-01-31 Thread Jeff Newmiller
Pot, meet kettle. You claim to be able to read documentation, yet you don't reference knowledge gained or clarity lost from such activity in your question. I think this is a case of inertia of history that we all have to live with at this point. If you thoroughly read the documentation for ?sapp

Re: [R] sapply returning list instead of matrix

2014-01-31 Thread William Dunlap
p TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of chris warth > Sent: Friday, January 31, 2014 2:22 PM > To: r-help@r-project.org > Subject: Re: [R] sapply returning list

Re: [R] sapply returning list instead of matrix

2014-01-31 Thread chris warth
Hey thanks for the helpful snark, Bert. To everyone else, I apologize for neglecting to actually include the examples. a <- function(i) { list(1) } b <- function(i) { list(1,2) } ll <- sapply(seq(3), a, simplfy="list") mm <- sapply(seq(3), b) class(ll) class(mm) > class(ll) [1] "list" > class(mm)

Re: [R] sapply returning list instead of matrix

2014-01-31 Thread Bert Gunter
As you ignored the posting guide and posted in HTML, your "below" didn't get through. So one can only guess that it has something to do with (see ?sapply) "Simplification in sapply is only attempted if X has length greater than zero and if the return values from all elements of X are all of the sa

[R] sapply returning list instead of matrix

2014-01-31 Thread chris warth
Can anyone suggest a rationale for why sapply() returns different types (list and matrix) in the two examples below? Is there any way to get sapply() or any other apply() function to return a matrix in both cases? simplify=TRUE doesn't change the outcome. I understand why it is happening, I just

Re: [R] SAPPLY function for COLUMN NULL

2013-05-26 Thread arun
colnames(dd) #[1] "col1" "colb" null_vector<- colnames(dd) sapply(null_vector,makeNull,dd) # col1 colb #[1,]   NA    4 #[2,]    2   NA #[3,]    3    2 #[4,]    4   NA #[5,]    1    4 #[6,]   NA    5 #[7,]    1    6 A.K. >I am trying to make a column value in a dataframe = NA if there is a 0

Re: [R] sapply error produced by grid search

2013-05-16 Thread Berend Hasselman
On 16-05-2013, at 17:31, "Patel, Shreena" wrote: > Dear R User, > > I'm trying to perform a grid-search for the ML estimator of the Box-Cox > parameter for a linear mixed model. However using sapply to perform the grid > search returns an error message. Here's a small example to demonstrate:

[R] sapply error produced by grid search

2013-05-16 Thread Patel, Shreena
Dear R User, I'm trying to perform a grid-search for the ML estimator of the Box-Cox parameter for a linear mixed model. However using sapply to perform the grid search returns an error message. Here's a small example to demonstrate: library(lme4) # Function to fit model for a given lambda: bc

Re: [R] sapply and matrix command

2012-08-09 Thread R. Michael Weylandt
On Wed, Aug 8, 2012 at 10:37 AM, alijk1989 [via R] wrote: > > > Hi Michael, > > Thanks for your response. Here is a simple example of what I am trying to > do: > > w=rep(0.02,10) > Q=rep(0.02,10) > rho=matrix(0.5,nrow=10,ncol=10) > m=10 > LGD=0.45 > > M1=sum(sapply(1:m, > function(k){sum(sapply(1:

Re: [R] sapply and matrix command

2012-08-08 Thread alijk1989 [via R]
Hi Michael, Thanks for your response. Here is a simple example of what I am trying to do: w=rep(0.02,10) Q=rep(0.02,10) rho=matrix(0.5,nrow=10,ncol=10) m=10 LGD=0.45 M1=sum(sapply(1:m, function(k){sum(sapply(1:m,function(j){w[k]*w[j]*LGD^2*(pmnorm(c(qnorm(Q[k]),qnorm(Q[j])),c(0,0),equicorr(2,r

Re: [R] sapply and matrix command

2012-08-08 Thread R. Michael Weylandt
On Wed, Aug 8, 2012 at 9:17 AM, alijk1989 [via R] wrote: > > > Hi, > > I have made some progress speeding up my code. This is what I have at the > moment: > > > M1=sum(sapply(1:m, function(k){sum(sapply(1:m,function(j){w[k]*w[j]*LGD^2 > (pmnorm(c(qnorm(Q[k]),qnorm(Q[j])),c(0,0),equicorr(2,rho[k,j]

Re: [R] sapply and matrix command

2012-08-08 Thread alijk1989 [via R]
Hi, I have made some progress speeding up my code. This is what I have at the moment: M1=sum(sapply(1:m, function(k){sum(sapply(1:m,function(j){w[k]*w[j]*LGD^2 (pmnorm(c(qnorm(Q[k]),qnorm(Q[j])),c(0,0),equicorr(2,rho[k,j]))-Q[k]*Q[j])}))})) I tried setting up a function as so: f1 <- function

Re: [R] sapply() and by()

2012-08-06 Thread David Winsemius
On Aug 6, 2012, at 7:34 AM, Dominic Roye wrote: Hello everyone, I have a dataset with 5 colums (4 colums with thresholds of weather stations and one with month - data of 5 years). Now I would like to calculate the average for each month. I tried this unsuccessfully: lf.med <- sapply(LF[,1:4

Re: [R] sapply() and by()

2012-08-06 Thread Jean V Adams
Dominic, It's great that you provided some example data, but a much smaller data frame would have sufficed. For example, 10 randomly selected rows from your data ... LF <- structure(list(Serra.da.Foladoira = c(27.335652173913, 25.4632608695652, 24.464652173913, 22.550652173913, 22.217782

Re: [R] sapply and matrix command

2012-08-04 Thread R. Michael Weylandt
Take a look at ?expand.grid Michael On Aug 4, 2012, at 5:03 PM, "alijk1989 [via R]" wrote: > > > Thanks again for the help looks like this will be useful for what I'm doing. > Is there any way to use combn to return combinations of values with > themselves: > > e.g. > >> combn(1:3,2) > >

Re: [R] sapply and matrix command

2012-08-04 Thread alijk1989 [via R]
Thanks again for the help looks like this will be useful for what I'm doing. Is there any way to use combn to return combinations of values with themselves: e.g. >combn(1:3,2) [,1] [,2] [,3] [,4] [,5] [,6] [1,]111 2 2 3 [2,]1 232 3 3

Re: [R] sapply and matrix command

2012-08-01 Thread arun kirshna [via R]
HI, You can also try this: d<-1:25 A<-sample(combn(20:30,2)) B<-sample(combn(20:30,2)) lapply(d,function(x) matrix(c(1,A[x],B[x],1),2,2)) [[1]] [,1] [,2] [1,]1 23 [2,] 271 [[2]] [,1] [,2] [1,]1 21 [2,] 211 [[3]] [,1] [,2] [1,]1 29 [2,] 231 [[4]

Re: [R] sapply question

2012-07-11 Thread jim holtman
Go back and reread the section about the scoping of variables and that functions do not have side effects; they only return values. You are changinga "local" copy of df1 within the function which is returning the changed values to df3. On Wed, Jul 11, 2012 at 7:36 PM, Charles Stangor wrote: > Wh

[R] sapply question

2012-07-11 Thread Charles Stangor
Why does this sapply code change df3 but not df1? Thanks df1 <- read.table(text=" cola colb colc cold cole 1NA59 NA 17 2NA6 NA 14 NA 3 3NA 11 15 19 4 48 12 NA 20 ", header=TRUE) df2 <-df1*2 df1 df2 df3 <-sapply(names(df1),function(x) {df

Re: [R] sapply help

2012-02-04 Thread Milan Bouchet-Valat
Le vendredi 03 février 2012 à 18:51 +, William Dunlap a écrit : > Instead of colSums(t(aMatrix)), why not the more > direct rowSums(aMatrix)? Because I felt it was more didactic. The question was about counting occurrences per column, so using rowSums() could be a little confusing without an ex

Re: [R] sapply help

2012-02-03 Thread Filoche
Thank you sire. You explained it very well. This give ma a good point to start using sapply more frequently. Cordially, Phil -- View this message in context: http://r.789695.n4.nabble.com/sapply-help-tp4355092p4355376.html Sent from the R help mailing list archive at Nabble.com. __

Re: [R] sapply help

2012-02-03 Thread William Dunlap
potfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Milan Bouchet- > Valat > Sent: Friday, February 03, 2012 10:17 AM > To: Ernest Adrogué > Cc: r-help@r-project.org

Re: [R] sapply help

2012-02-03 Thread Milan Bouchet-Valat
Le vendredi 03 février 2012 à 18:27 +0100, Ernest Adrogué a écrit : > 3-02-2012, 08:37 (-0800); Filoche escriu: > > Hi every one. > > > > I'm learning how to use sapply (and other function of this family). > > > > Here's what I'm trying to do. > > > > I have a vector of lets say 5 elements. I al

Re: [R] sapply help

2012-02-03 Thread Ernest Adrogué
3-02-2012, 08:37 (-0800); Filoche escriu: > Hi every one. > > I'm learning how to use sapply (and other function of this family). > > Here's what I'm trying to do. > > I have a vector of lets say 5 elements. I also have a matrix of nX5. I would > like to know how many element by column are infe

[R] sapply help

2012-02-03 Thread Filoche
Hi every one. I'm learning how to use sapply (and other function of this family). Here's what I'm trying to do. I have a vector of lets say 5 elements. I also have a matrix of nX5. I would like to know how many element by column are inferior to each element of my vector. On this example: v = c(

Re: [R] sapply Call Returning " the condition has length > 1" Error

2011-12-27 Thread Alex Zhang
n Fox ; "r-help@r-project.org" Sent: Tuesday, December 27, 2011 6:59 PM Subject: Re: [R] sapply Call Returning " the condition has length > 1" Error Your puzzle comes from a collision of two somewhat subtle facts that i) sapply() is a wrapper for lapply(), not apply() a

Re: [R] sapply Call Returning " the condition has length > 1" Error

2011-12-27 Thread R. Michael Weylandt
function to do it where * stands > for any function. > > Thanks a lot! > > > ____ >  From: John Fox > To: 'Alex Zhang' > Cc: r-help@r-project.org > Sent: Tuesday, December 27, 2011 4:06 PM > Subject: RE: [R] sapply Call R

Re: [R] sapply Call Returning " the condition has length > 1" Error

2011-12-27 Thread Alex Zhang
> "how" not just "what" ;). Is there a way using�a "*apply" function to >> do it where * stands for any function. >> >> Thanks a lot!� >> >> >> >> From: John Fox >> To: 'Alex Zhang

Re: [R] sapply Call Returning " the condition has length > 1" Error

2011-12-27 Thread Jeff Newmiller
uot;*apply" function to >do it where * stands for any function. > >Thanks a lot!� > > >________ > From: John Fox >To: 'Alex Zhang' >Cc: r-help@r-project.org >Sent: Tuesday, December 27, 2011 4:06 PM >Subject: RE: [R] sapply C

Re: [R] sapply Call Returning " the condition has length > 1" Error

2011-12-27 Thread Alex Zhang
a lot!  From: John Fox To: 'Alex Zhang' Cc: r-help@r-project.org Sent: Tuesday, December 27, 2011 4:06 PM Subject: RE: [R] sapply Call Returning " the condition has length > 1" Error Dear Alex, > -Original Message- > From

Re: [R] sapply Call Returning " the condition has length > 1" Error

2011-12-27 Thread John Fox
Dear Alex, > -Original Message- > From: Alex Zhang [mailto:alex.zh...@ymail.com] > Sent: December-27-11 3:34 PM > To: John Fox > Cc: r-help@r-project.org > Subject: Re: [R] sapply Call Returning " the condition has length > 1" > Error > > John

Re: [R] sapply Call Returning " the condition has length > 1" Error

2011-12-27 Thread jim holtman
ole columns? Thanks. > > > >  From: John Fox > To: 'Alex Zhang' > Cc: r-help@r-project.org > Sent: Tuesday, December 27, 2011 3:10 PM > Subject: RE: [R] sapply Call Returning " the condition has length > 1" Error > >

Re: [R] sapply Call Returning " the condition has length > 1" Error

2011-12-27 Thread Alex Zhang
s? That explains the behavior. Is there an "*apply" function that will fee elements of the input data.frame into "FUN" instead of whole columns? Thanks.  From: John Fox To: 'Alex Zhang' Cc: r-help@r-project.org Sent: Tuesday, Dece

Re: [R] sapply Call Returning " the condition has length > 1" Error

2011-12-27 Thread John Fox
Dear Alex, > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Alex Zhang > Sent: December-27-11 2:14 PM > To: r-help@r-project.org > Subject: [R] sapply Call Returning " the condition has length >

Re: [R] sapply Call Returning " the condition has length > 1" Error

2011-12-27 Thread R. Michael Weylandt
You are right that the problem is that "DummyFunc" isn't vectorized. R looks for a single logical value in an "if" statement but "x>0" gives it a whole vector's worth -- as the warning indicates, it only uses the first and pushes the whole vector through the loop in the return(-x) branch, which exp

[R] sapply Call Returning " the condition has length > 1" Error

2011-12-27 Thread Alex Zhang
Dear all, Happy new year! I have a question re using sapply. Below is a dummy example that would replicate the error I saw.  ##Code Starts here DummyFunc <- function(x) { if (x > 0) { return (x) } else { return (-x) } } Y = data.frame(val = c(-3:7)) sapply(Y, FUN = DummyFunc) ##Code ends her

Re: [R] sapply(pred,cor,y=resp)

2011-10-09 Thread Joshua Wiley
Hi, It is probably more confusing with several steps combined, but you are correct that it is because there are NAs. It is fairly common for R functions to return NA if there are any NA values unless you explicitly set an argument on what to do with missing values. A quick look at ?cor clearly s

[R] sapply(pred,cor,y=resp)

2011-10-09 Thread William Claster
Hello. I am wondering why I am getting NA for all in cors=sapply(pred,cor,y=resp). I suppose that each column in pred has NAs in them. Is there some way to fix this? Thanks > str(pred) 'data.frame':   200 obs. of  13 variables:  $ mnO2: num  9.8 8 11.4 4.8 9 13.1 10.3 10.6 3.4 9.9 ...  $ Cl  :

Re: [R] sapply to bind columns, with repeat?

2011-08-13 Thread Katrina Bennett
Hi Weidong Gu, This works! For my clarity, and so I can repeat this process if need be: The 'mat' generates a matrix using whatever is supplied to x (i.e. coop.dat) using the columns from position 9:length(x) of 6 columns (by row). The 'rem.col' generates a matrix of the first 1:8 columns of 8 c

Re: [R] sapply to bind columns, with repeat?

2011-08-12 Thread Weidong Gu
On Fri, Aug 12, 2011 at 5:08 PM, Katrina Bennett wrote: > Hi Weidong Gu, > > This works! For my clarity, and so I can repeat this process if need be: > > The 'mat' generates a matrix using whatever is supplied to x (i.e. > coop.dat) using the columns from position 9:length(x) of 6 columns (by > ro

Re: [R] sapply to bind columns, with repeat?

2011-08-12 Thread Weidong Gu
Katrina, try this. reorg<-function(x){ mat<-matrix(x[9:length(x)],ncol=6,byrow=T) rem.col<-matrix(rep(x[1:8],nrow(mat)),byrow=T,ncol=8) return(data.frame(cbind(rem.col,mat))) } co<-do.call('rbind',apply(coop.dat,1,function(x) reorg(x))) You may need to tweak a bit to fit exactly what you want.

[R] sapply to bind columns, with repeat?

2011-08-12 Thread Katrina Bennett
Hi R-help, I am working with US COOP network station data and the files are concatenated in single rows for all years, but I need to pull these apart into rows for each day. To do this, I need to extract part of each row such as station id, year, mo, and repeat this against other variables in the

Re: [R] sapply( ) a loop function

2011-08-11 Thread R. Michael Weylandt
The previous two posters basically covered everything, but since I'm on the train with not too much to do, here's a more detailed response building on what they said. The following code is "shovel-ready" and can be pasted directly to your command line if you have your main data frame called "d" ava

Re: [R] sapply( ) a loop function

2011-08-11 Thread Dennis Murphy
Hi: samp_func() doesn't return anything. Either (1) type test as the last line of the function body or (2) don't assign the last sum to an object. HTH, Dennis On Thu, Aug 11, 2011 at 1:59 PM, Sean Bignami wrote: > Hello R-universe... > > I am having trouble writing a function which contains a l

Re: [R] sapply( ) a loop function

2011-08-11 Thread Daniel Malter
I am not sure what purpose the while loop has. However, the main problem seems to be that you need to put: i<-sample(1:(n-40),1) #This sample from 1 to n-40 rather than i<-sample(1:n-40,1) #this samples one 1:n and then subtracts 40 Otherwise, you may get negative index values Best, Daniel

[R] sapply( ) a loop function

2011-08-11 Thread Sean Bignami
Hello R-universe... I am having trouble writing a function which contains a loop so I can sapply() it to a list of data frames Each data frame has 241 observations of 15 variables. My loop takes a random sample of one row until the 40 consecutive rows after the sample have a d2p(variable) sum

Re: [R] SAPPLY function XXXX

2011-05-04 Thread Petr PIKAL
Hi r-help-boun...@r-project.org napsal dne 04.05.2011 22:26:59: > Erik Iverson > Odeslal: r-help-boun...@r-project.org > > 04.05.2011 22:26 > > Komu > > Dan Abner > > > > Ultimately, I would like for this to be 1 conponent in a larger function > > that will produce PROC CONTENTS style ou

Re: [R] SAPPLY function XXXX

2011-05-04 Thread Dan Abner
Perfect Erik! Thank you! On Wed, May 4, 2011 at 4:22 PM, Erik Iverson wrote: > Dan, > > > > I am attempting to write a function to count the number of non-missing >> values of each column in a data frame using the sapply function. I have >> the >> following code which is receiving the error me

Re: [R] SAPPLY function XXXX

2011-05-04 Thread Erik Iverson
Ultimately, I would like for this to be 1 conponent in a larger function that will produce PROC CONTENTS style output. Something like... data1.contents<-data.frame(Variable=names(data1), Class=sapply(data1,class), n.valid=sapply(data1,sum(!is.na)), n.miss=sapply(data1,sum(is.na))) data1.cont

Re: [R] SAPPLY function XXXX

2011-05-04 Thread Erik Iverson
Dan, I am attempting to write a function to count the number of non-missing values of each column in a data frame using the sapply function. I have the following code which is receiving the error message below. n.valid<-sapply(data1,sum(!is.na)) Error in !is.na : invalid argument type Tha

[R] SAPPLY function XXXX

2011-05-04 Thread Dan Abner
Hello everyone, I am attempting to write a function to count the number of non-missing values of each column in a data frame using the sapply function. I have the following code which is receiving the error message below. > n.valid<-sapply(data1,sum(!is.na)) Error in !is.na : invalid argument ty

Re: [R] Sapply for descriptive statistics

2011-03-09 Thread David Winsemius
Sent from my iPhone On Mar 9, 2011, at 6:13 PM, David Winsemius wrote: > > > Sent from my iPhone > > On Mar 9, 2011, at 5:59 PM, Tomii wrote: > >> I try to calculate descriptive statistics for one of the variables in the >> data frame, however command sapply calculates these statistics fo

Re: [R] Sapply for descriptive statistics

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

Re: [R] Sapply for descriptive statistics

2011-03-09 Thread David Winsemius
Sent from my iPhone On Mar 9, 2011, at 5:59 PM, Tomii wrote: > I try to calculate descriptive statistics for one of the variables in the > data frame, however command sapply calculates these statistics for every > value of the variable separately. How to make it calculate range (as well as > o

[R] Sapply for descriptive statistics

2011-03-09 Thread Tomii
I try to calculate descriptive statistics for one of the variables in the data frame, however command sapply calculates these statistics for every value of the variable separately. How to make it calculate range (as well as other statistics) for all column? Here are commands and results: > as1$tr

Re: [R] sapply puzzlement

2011-01-27 Thread Dario Strbenac
:45 +0100 >From: r-help-boun...@r-project.org (on behalf of nfdi...@gmail.com (Ernest >Adrogué i Calveras)) >Subject: [R] sapply puzzlement >To: r-help@r-project.org > >Hi, > >I have this data.frame with two variables in it, > >> z > V1 V2 >1 10 8 >2 NA

Re: [R] sapply puzzlement

2011-01-27 Thread Pete Brecknock
In addition to what has already been suggested you could use .. mapply(function(x,y) x-y, z,means) which returns V1 V2 [1,] 0.333 -2.7142857 [2,] NA 7.2857143 [3,] -0.667 -3.7142857 [4,] -6.667 NA [5,] NA -0.7142857 [6,] 1.333

Re: [R] sapply puzzlement

2011-01-27 Thread David Winsemius
On Jan 27, 2011, at 7:16 PM, Ernest Adrogué i Calveras wrote: Hi, I have this data.frame with two variables in it, z V1 V2 1 10 8 2 NA 18 3 9 7 4 3 NA 5 NA 10 6 11 12 7 13 9 8 12 11 and a vector of means, means <- apply(z, 2, function (col) mean(na.omit(col))) means V1

Re: [R] sapply puzzlement

2011-01-27 Thread David A. Johnston
sapply(z, function(row) ...) does not actually grab a row at a time out of 'z'. It grabs a column (because 'z' is a data.frame) You may want: t(apply(z, 1, function(row) row - means)) or: t(t(z) - means) Hope that helps, -David Johnston -- View this message in context: http://r.789695.n4.n

[R] sapply puzzlement

2011-01-27 Thread Ernest Adrogué i Calveras
Hi, I have this data.frame with two variables in it, > z V1 V2 1 10 8 2 NA 18 3 9 7 4 3 NA 5 NA 10 6 11 12 7 13 9 8 12 11 and a vector of means, > means <- apply(z, 2, function (col) mean(na.omit(col))) > means V1V2 9.67 10.714286 My intention was substracting mean

[R] sapply

2010-11-08 Thread Dr. Alireza Zolfaghari
y=sapply(1:nrow(x), function(i) sapply(1:ncol(x), function(j) getrc(i, j))) Arvin Zolfaghari, Ph.D. Aon Benfield | Aon Benfield Analytics 55 Bishopsgate, London, EC2N 3BD, UK t: +44 20 7522 8241 | f: +44 20 7522 3846| M: +44 78 8940 6637 e: arvin.zolfagh...@aonbenfield.com | w:

Re: [R] SApply versus for loop for list of data.frames

2010-10-11 Thread David Winsemius
On Oct 12, 2010, at 12:33 AM, David Winsemius wrote: On Oct 12, 2010, at 12:16 AM, rivercode wrote: Hi, I am trying to find the total number of rows for a list of data.frames and want to know if there is a better way than using a loop like: df = { list of data.frame with varying number

Re: [R] SApply versus for loop for list of data.frames

2010-10-11 Thread David Winsemius
On Oct 12, 2010, at 12:16 AM, rivercode wrote: Hi, I am trying to find the total number of rows for a list of data.frames and want to know if there is a better way than using a loop like: df = { list of data.frame with varying number of rows...each one has a column called "COL" } r =

[R] SApply versus for loop for list of data.frames

2010-10-11 Thread rivercode
Hi, I am trying to find the total number of rows for a list of data.frames and want to know if there is a better way than using a loop like: >df = { list of data.frame with varying number of rows...each one has a column called "COL" } >r = 0 > for (i in 1:length(df)) { + r = r + length(n[[i]]$CO

Re: [R] sapply/lapply instead of loop

2010-08-10 Thread GL
That works great, and is ever so much simpler. Thanks much! -- View this message in context: http://r.789695.n4.nabble.com/sapply-lapply-instead-of-loop-tp2320265p2320317.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-projec

Re: [R] sapply/lapply instead of loop

2010-08-10 Thread Henrique Dallazuanna
So try: format(as.matrix(temp)) On Tue, Aug 10, 2010 at 4:13 PM, GL wrote: > > Both of those approaches seem to return (" v75") instead of ("v75 "). > -- > View this message in context: > http://r.789695.n4.nabble.com/sapply-lapply-instead-of-loop-tp2320265p2320305.html > Sent from the R help

Re: [R] sapply/lapply instead of loop

2010-08-10 Thread GL
Both of those approaches seem to return (" v75") instead of ("v75 "). -- View this message in context: http://r.789695.n4.nabble.com/sapply-lapply-instead-of-loop-tp2320265p2320305.html Sent from the R help mailing list archive at Nabble.com. __ R-

Re: [R] sapply/lapply instead of loop

2010-08-10 Thread Henrique Dallazuanna
Try this: formatC(as.matrix(temp)) On Tue, Aug 10, 2010 at 3:55 PM, GL wrote: > > Using the input below, can I do something more elegant (and more efficient) > than the loop also listed below to pad strings to a width of 5? The true > matrix is about 300K rows and 31 columns. > > #

Re: [R] sapply/lapply instead of loop

2010-08-10 Thread jim holtman
will this do what you want: > newTemp[] <- lapply(newTemp, function(.col){ + # convert to character and pad to 5 space + sprintf("%5s", as.character(.col)) + }) > > str(newTemp) 'data.frame': 5 obs. of 3 variables: $ DX1: chr "13761" "63371" "51745" "64081" ... $ DX2: chr " 8125" "

[R] sapply/lapply instead of loop

2010-08-10 Thread GL
Using the input below, can I do something more elegant (and more efficient) than the loop also listed below to pad strings to a width of 5? The true matrix is about 300K rows and 31 columns. ### #INPUT ### > temp DX1 DX2 DX3 1 13761 8125 49178 2 6

Re: [R] sapply or apply

2010-06-16 Thread Joshua Wiley
Hello Roslina, Maybe it is just me, but I have difficulty picking apart what you are trying to do because, the data have the same names as the arguments in your functions, and when you create the function term(), you have two sets of arguments (for term() and for gam_sum() ) that have the same nam

[R] sapply or apply

2010-06-16 Thread Roslina Zakaria
Hi r-users, I have this code here : dt   <- winter_pos_sum bt   <- c(24.96874, 19.67861, 23.51001, 19.86868); round(bt,2) alp  <- c(2.724234, 3.914649, 3.229146, 3.120719); round(alp,2) bt_min  <- min(bt) ; bt_min p   <- alp_sum    ; p t  <- 50 t1 <- t+1     #first get the sum ove

Re: [R] sapply code

2010-05-16 Thread Henrique Dallazuanna
Try this: 1 + (1 / log(length(lambda_cor))) * sum((l <- lambda_cor / length(lambda_cor)) * log(l)) On Sun, May 16, 2010 at 10:43 PM, Roslina Zakaria wrote: > Hi r-users, > > I have this code here, but I just wonder how do I use 'sapply' to make it > more efficient > > lamda_cor <- eigen(winter_

[R] sapply code

2010-05-16 Thread Roslina Zakaria
Hi r-users,   I have this code here, but I just wonder how do I use 'sapply' to make it more efficient lamda_cor <- eigen(winter_cor)$values   > lamda_cor [1] 1.3459066 1.0368399 0.8958128 0.7214407   lamda_cxn <- function(dt) { n   <- length(dt)   term    <- vector(length=n, mode="numeric")

Re: [R] sapply, lattice functions

2010-03-21 Thread Santosh
Dear R-gurus.. How do I implement the following: a) Overlay frequency(instead of density) with line of density plot, vertical lines of confidence intervals and reference levels? b) Control the breaks (using "nint?"), order of the panel, and the layout, place units for each conditioning variable?

Re: [R] sapply, lattice functions

2010-03-20 Thread Sundar Dorai-Raj
You're right. It's necessary for xyplot though to prevent grouping. On Mar 20, 2010 10:43 AM, "Dieter Menne" wrote: Sundar Dorai-Raj-2 wrote: > > Or perhaps more clearly, > > histogram(~a1 + b1 + c1, data = aa, o... Why outer=TRUE? Looks same for me without: Dieter library(lattice) aa <- dat

Re: [R] sapply, lattice functions

2010-03-20 Thread Dieter Menne
Sundar Dorai-Raj-2 wrote: > > Or perhaps more clearly, > > histogram(~a1 + b1 + c1, data = aa, outer = TRUE) > > Why outer=TRUE? Looks same for me without: Dieter library(lattice) aa <- data.frame(a1=rnorm(20),b1=rnorm(20,0.8),c1=rnorm(20,0.5)) histogram(~a1 + b1 + c1, data = aa) -- View

Re: [R] sapply, lattice functions

2010-03-19 Thread Sundar Dorai-Raj
Or perhaps more clearly, histogram(~a1 + b1 + c1, data = aa, outer = TRUE) --sundar On Fri, Mar 19, 2010 at 3:50 PM, Gabor Grothendieck wrote: > Try this: > > histogram(~ values | ind, stack(aa)) > > > On Fri, Mar 19, 2010 at 5:44 PM, Santosh wrote: > > Dear R-gurus > > > > aa <- data.frame(a

Re: [R] sapply, lattice functions

2010-03-19 Thread Gabor Grothendieck
Try this: histogram(~ values | ind, stack(aa)) On Fri, Mar 19, 2010 at 5:44 PM, Santosh wrote: > Dear R-gurus > > aa <- data.frame(a1=rnorm(20),b1=rnorm(20,0.8),c1=rnorm(20,0.5)) > sapply(aa,function(x) histogram(x,breaks=NULL)) > > or px <- sapply(aa,function(x) histogram(x,breaks=NULL)) > pri

Re: [R] sapply, lattice functions

2010-03-19 Thread Santosh
Thanks for your response. how do I print them in an ordered manner, akin to using print(px,split=c(2,2,1,1),more=T)) or par(mfrow=c(x,y))? -Santosh On Fri, Mar 19, 2010 at 2:58 PM, Gabor Grothendieck wrote: > Try this: > > junk <- sapply(aa,function(x) print(histogram(x,breaks=NULL))) > > or, s

Re: [R] sapply, lattice functions

2010-03-19 Thread Gabor Grothendieck
Try this: junk <- sapply(aa,function(x) print(histogram(x,breaks=NULL))) or, shorter: for(a in aa) print(histogram(a, breaks = NULL) On Fri, Mar 19, 2010 at 5:44 PM, Santosh wrote: > Dear R-gurus > > aa <- data.frame(a1=rnorm(20),b1=rnorm(20,0.8),c1=rnorm(20,0.5)) > sapply(aa,function(x) hist

[R] sapply, lattice functions

2010-03-19 Thread Santosh
Dear R-gurus aa <- data.frame(a1=rnorm(20),b1=rnorm(20,0.8),c1=rnorm(20,0.5)) sapply(aa,function(x) histogram(x,breaks=NULL)) or px <- sapply(aa,function(x) histogram(x,breaks=NULL)) print(px,split=c(1,1,1,1),more=F) The above code does not seem to work. am I missing something? Thanks, Santosh

Re: [R] Sapply

2009-08-30 Thread hadley wickham
On Sun, Aug 30, 2009 at 5:08 PM, Noah Silverman wrote: > Hi, > > I need a bit of guidance with the sapply function.  I've read the help page, > but am still a bit unsure how to use it. > > I have a large data frame with about 100 columns and 30,000 rows.  One of > the columns is "group" of which th

Re: [R] Sapply

2009-08-30 Thread Rolf Turner
Fortune candidate? cheers, Rolf Turner On 30/08/2009 6:08 PM, Noah Silverman wrote: Normally, I would just write a huge "for each" loop, but have read that is hugely inefficient with R. On 31/08/2009, at 10:47 AM, Duncan Murdoch wrote: Don't believe

Re: [R] Sapply

2009-08-30 Thread Duncan Murdoch
On 30/08/2009 6:08 PM, Noah Silverman wrote: Hi, I need a bit of guidance with the sapply function. I've read the help page, but am still a bit unsure how to use it. I have a large data frame with about 100 columns and 30,000 rows. One of the columns is "group" of which there are about 2,0

Re: [R] Sapply

2009-08-30 Thread Gabor Grothendieck
Try this: data$score <- ave(data$score, data$group, FUN = prop.table) On Sun, Aug 30, 2009 at 6:08 PM, Noah Silverman wrote: > Hi, > > I need a bit of guidance with the sapply function.  I've read the help page, > but am still a bit unsure how to use it. > > I have a large data frame with about 1

[R] Sapply

2009-08-30 Thread Noah Silverman
Hi, I need a bit of guidance with the sapply function. I've read the help page, but am still a bit unsure how to use it. I have a large data frame with about 100 columns and 30,000 rows. One of the columns is "group" of which there are about 2,000 distinct "groups". I want to normalize (s

Re: [R] sapply() related query

2009-06-17 Thread Girish A.R.
Thanks, Marc! This is what I was looking for. best, -Girish PS: Also appreciate your concern about this being a part of a variable selection process. On Jun 17, 9:01 pm, Marc Schwartz wrote: > On Jun 17, 2009, at 10:06 AM, Girish A.R. wrote: > > > > > Hi folks, > > > I'm trying to consolidate

Re: [R] sapply() related query

2009-06-17 Thread Marc Schwartz
On Jun 17, 2009, at 10:06 AM, Girish A.R. wrote: Hi folks, I'm trying to consolidate the outputs (of anova() and lrm()) from multiple runs of single-variable logistic regression. Here's how the output looks: y

[R] sapply() related query

2009-06-17 Thread Girish A.R.
Hi folks, I'm trying to consolidate the outputs (of anova() and lrm()) from multiple runs of single-variable logistic regression. Here's how the output looks: y ~ x1 y ~ x2 y ~ x3 y ~ x4 Chi-Squa

Re: [R] sapply

2009-02-08 Thread glenn
Bullseye ! thanks a lot Sundar -Original Message- From: Sundar Dorai-Raj [mailto:sdorai...@gmail.com] Sent: 09 February 2009 00:31 To: glenn Cc: r-help@r-project.org Subject: Re: [R] sapply I'm not sure what you really want, so perhaps a simple example would help (i.e. what a samp

Re: [R] sapply

2009-02-08 Thread Sundar Dorai-Raj
I'm not sure what you really want, so perhaps a simple example would help (i.e. what a sample of the input looks like and what the output you need looks like). My guess would be sapply(df, diff) but again, I'm not sure. --sundar On Sun, Feb 8, 2009 at 4:24 PM, glenn wrote: > Newbie question so

[R] sapply

2009-02-08 Thread glenn
Newbie question sorry (have tried the help pages I promise) I have a dataframe (date,stockprice) say and looking how I might get the return of: dataframe (difference in days, change in stock price) using sapply - I require a very simple function and don't really want to go down the zoo and quan

Re: [R] sapply and median, possible or not ?

2008-11-07 Thread Keith Jewell
I haven't looked at the detail, but I guess the answer is that mean works on a data frame while median doesn't. ?mean For a data frame, a named vector with the appropriate method being applied column by column. - I guess to use median you'll need nested '[l/s]apply's, the outer

  1   2   >