Re: [R] Using lapply in R data table

2016-09-27 Thread Frank S.
Frank S. De: Bert Gunter Enviat el: dilluns, 26 de setembre de 2016 23:18:52 Per a: Ista Zahn A/c: Frank S.; r-help@r-project.org Tema: Re: [R] Using lapply in R data table ... and just for fun, here's an alternative in which mapply() is used to vectorize switch(); again, whet

Re: [R] Using lapply in R data table

2016-09-26 Thread Bert Gunter
... and just for fun, here's an alternative in which mapply() is used to vectorize switch(); again, whether you like it may be just a matter of taste, although I suspect it might be less efficient than ifelse(), which is already vectorized: DT <- within(DT, exposure <- {

Re: [R] Using lapply in R data table

2016-09-26 Thread Bert Gunter
Ista: Aha -- now I see the point. My bad. You are right. I was careless. However, cut() with ifelse() might simplify the code a bit and/or make it more readable. To be clear, this is just a matter of taste; e.g. using your data and a data frame instead of a data table: > DT <- within(DT,

Re: [R] Using lapply in R data table

2016-09-26 Thread Ista Zahn
On Mon, Sep 26, 2016 at 2:48 PM, Bert Gunter wrote: > I thought that that was a typo from the OP, as it disagrees with his > example. But the labels are arbitrary, so in fact cut() will do it > whichever way he meant. I don't see how cut will do it, at least not conveniently. Consider this slight

Re: [R] Using lapply in R data table

2016-09-26 Thread Bert Gunter
I thought that that was a typo from the OP, as it disagrees with his example. But the labels are arbitrary, so in fact cut() will do it whichever way he meant. -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Ber

Re: [R] Using lapply in R data table

2016-09-26 Thread Ista Zahn
On Mon, Sep 26, 2016 at 1:59 PM, Bert Gunter wrote: > This seems like a job for cut() . I thought that at first two, but the middle group shouldn't be .87 but rather exposure" = "2007-01-01" - "fini" so, I think cut alone won't do it. Best, Ista > > (I made DT a data frame to avoid loading the

Re: [R] Using lapply in R data table

2016-09-26 Thread Bert Gunter
This seems like a job for cut() . (I made DT a data frame to avoid loading the data table package. But I assume it would work with a data table too, Check this, though!) > DT <- within(DT, exposure <- > cut(fini,as.Date(c("2000-01-01","2006-01-01","2006-06-30","2006-12-21")), > labels= c(1,.87,

Re: [R] Using lapply in R data table

2016-09-26 Thread Ista Zahn
Hi Frank, lapply(DT) iterates over each column. That doesn't seem to be what you want. There are probably better ways, but here is one approach. DT[, exposure := vector(mode = "numeric", length = .N)] DT[fini < as.Date("2006-01-01"), exposure := 1] DT[fini >= as.Date("2006-01-01") & fini <= as.D

[R] Using lapply in R data table

2016-09-26 Thread Frank S.
Dear all, I have a R data table like this: DT <- data.table( id = rep(c(2, 5, 7), c(3, 2, 2)), fini = rep(as.Date(c('2005-04-20', '2006-02-19', '2006-10-08')), c(3, 2, 2)), group = rep(c("A", "B", "A"), c(3, 2, 2)) ) I want to construct a new variable "exposure" defined as follows: 1) I

[R] Using lapply or mapply

2016-07-29 Thread Mehta, Gaurang
Hi Team, I am new to using apply function in R. I want to find out the empirical quantiles of all items in a list. ## Fitting Kernal Density function Emp_Marginals<-apply(M_Diff_Final,2,kde) ### Simulated variables Sim_Cop<-abs(rmvnorm(1,mean=apply(M_Diff_Final,2,mean),sigma=cov(M_Diff_Final)

Re: [R] Using lapply when there are dependencies

2015-05-28 Thread blue honour via R-help
Winsemius wrote: Subject: Re: [R] Using lapply when there are dependencies Cc: r-help@r-project.org Date: Thursday, May 28, 2015, 7:02 AM On May 27, 2015, at 4:34 PM, blue honour via R-help wrote: > Hi all, > > Let's say I have a vector: > > vv<-c(1,2,3) &

Re: [R] Using lapply when there are dependencies

2015-05-27 Thread Jeff Newmiller
For loops are not slow. Inefficient memory management in for loops is slow. Feel free to preallocate your output vectors and write for loops to your heart's content. If you really want speed you can write this in C++ using Rcpp [1]. If your f() is a standard digital filter algorithm this has al

Re: [R] Using lapply when there are dependencies

2015-05-27 Thread PIKAL Petr
Hi > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of blue > honour via R-help > Sent: Thursday, May 28, 2015 1:34 AM > To: r-help@r-project.org > Subject: [R] Using lapply when there are dependencies > > Hi all, > > Let&#x

Re: [R] Using lapply when there are dependencies

2015-05-27 Thread David Winsemius
On May 27, 2015, at 4:34 PM, blue honour via R-help wrote: > Hi all, > > Let's say I have a vector: > > vv<-c(1,2,3) > > > And suppose I have a function f(a,b), which is a function of 2 scalar inputs. > I would like to evaluate this function separately for each element of the vv > vector wh

[R] Using lapply when there are dependencies

2015-05-27 Thread blue honour via R-help
Hi all, Let's say I have a vector: vv<-c(1,2,3) And suppose I have a function f(a,b), which is a function of 2 scalar inputs. I would like to evaluate this function separately for each element of the vv vector while the second input to f( ) will be the previous output from f( ). So, the valu

[R] Using lapply on term document matrix to calculate word frequency

2015-03-18 Thread Nikhil Goyal
Given three TermDocumentMatrix, text1, text2 and text3, I'd like to calculate word frequency for each of them into a data frame and rbind all the data frames. Three are sample - I have hundreds in reality so I need to functionalize this. It's easy to calculate word freq for one TDM: apply(x,

Re: [R] using lapply to get function values

2014-01-10 Thread David Winsemius
On Jan 10, 2014, at 2:04 AM, Long Vo wrote: Hi R users, I need to apply a function on a list of vectors. This is simple when I use functions that returns only one numerical value such as 'mean' or 'variance'. Things get complex when I use functions returning a list of value, such as 'acf'

[R] using lapply to get function values

2014-01-09 Thread Long Vo
Hi R users, I need to apply a function on a list of vectors. This is simple when I use functions that returns only one numerical value such as 'mean' or 'variance'. Things get complex when I use functions returning a list of value, such as 'acf'. In the following example I first create a list of v

Re: [R] using lapply with recode

2012-11-09 Thread arun
function(x) data.frame(x[colnames(x)!="prov"],prov=recode(x$prov,"'QUE'='QC';'Quebec'='QC'"))) A.K. - Original Message - From: Simon Kiss To: arun Cc: Sent: Friday, November 9, 2012 9:39 AM Subject: Re: [R] using lapp

Re: [R] using lapply with recode

2012-11-09 Thread arun
#5   5 17   QC #6   6  7   AB #7   7  6   AB #8   8 21   AB #9   9  5   AB #10 10  1   AB A.K. ----- Original Message - From: Simon Kiss To: arun Cc: Sent: Friday, November 9, 2012 9:39 AM Subject: Re: [R] using lapply with recode Hi there: None of these suggestions do the work. I tried J

Re: [R] using lapply with recode

2012-11-08 Thread arun
Original Message - From: Simon Kiss To: r-help@r-project.org Cc: Sent: Thursday, November 8, 2012 10:06 PM Subject: [R] using lapply with recode Hello: Forgive me, this is surely a simple question but I can't figure it out, having consulted the help archives and "Data Manipulation Wit

Re: [R] using lapply with recode

2012-11-08 Thread arun
a   prov #1   1 Quebec #2   2 Quebec #3   3 Quebec #4   4 Quebec #5   5 Quebec #6   6 AB #7   7 AB #8   8 AB #9   9 AB #10 10 AB A.K. - Original Message - From: Simon Kiss To: r-help@r-project.org Cc: Sent: Thursday, November 8, 2012 10:06 PM Subject: [R] using lapply

Re: [R] using lapply with recode

2012-11-08 Thread Jim Holtman
you need to return 'x' as the last statement of the lapply. Sent from my iPad On Nov 8, 2012, at 22:06, Simon Kiss wrote: > Hello: > Forgive me, this is surely a simple question but I can't figure it out, > having consulted the help archives and "Data Manipulation With R" (Spector). > I have

[R] using lapply with recode

2012-11-08 Thread Simon Kiss
Hello: Forgive me, this is surely a simple question but I can't figure it out, having consulted the help archives and "Data Manipulation With R" (Spector). I have a list of 11 data frames with one common variable in each (prov). I'd like to use lapply to go through and recode one particular leve

Re: [R] using lapply

2011-03-10 Thread Phil Spector
.org] On Behalf Of rex.dw...@syngenta.com Sent: Thursday, March 10, 2011 8:47 AM To: lig...@statistik.tu-dortmund.de; arun.kumar.s...@gmail.com Cc: r-help@r-project.org Subject: Re: [R] using lapply But no one answered Kushan's question about performance implications of for-loop vs lapply. With apologie

Re: [R] using lapply

2011-03-10 Thread William Dunlap
ware wdunlap tibco.com > > -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Uwe Ligges > Sent: Thursday, March 10, 2011 4:38 AM > To: Arun Kumar Saha > Cc: r-help@r-project.org > Subject: Re: [R] using la

Re: [R] using lapply

2011-03-10 Thread rex.dwyer
Uwe Ligges Sent: Thursday, March 10, 2011 4:38 AM To: Arun Kumar Saha Cc: r-help@r-project.org Subject: Re: [R] using lapply On 10.03.2011 08:30, Arun Kumar Saha wrote: > On reply to the post > http://r.789695.n4.nabble.com/using-lapply-td3345268.html Hmmm, can you please reply to the origi

Re: [R] using lapply

2011-03-10 Thread Uwe Ligges
On 10.03.2011 08:30, Arun Kumar Saha wrote: On reply to the post http://r.789695.n4.nabble.com/using-lapply-td3345268.html Hmmm, can you please reply to the original post and quote it? You mail was not recognized to be in the same thread as the message of the original poster (and hence I was

Re: [R] using lapply

2011-03-10 Thread Uwe Ligges
On 10.03.2011 03:46, Kushan Thakkar wrote: I have a function with the follow signare: apply.strategy(instr, strat) where instr and strat are both objects of classes instrument and strategy respectively. I want to apply this function to a list that holds objects of the class instrument. Curr

[R] using lapply

2011-03-09 Thread Arun Kumar Saha
On reply to the post http://r.789695.n4.nabble.com/using-lapply-td3345268.html Dear Kushan, this may be a good start: ## assuming 'instr.list' is your list object and you are applying my.strat() function on each element of that list, you can use lapply function as lapply(instr.list, function(x)

[R] using lapply

2011-03-09 Thread Kushan Thakkar
I have a function with the follow signare: apply.strategy(instr, strat) where instr and strat are both objects of classes instrument and strategy respectively. I want to apply this function to a list that holds objects of the class instrument. Currently I am doing this by explicit looping: for

Re: [R] using lapply and split to plot up subsets of a vector

2010-12-30 Thread karmakiller
Hi again, I have spent the last couple of days trying to build a function that will allow me to add to the multiple plots that I created with your advice. I have changed to plot(ln.o2con~lnbm,data=df) in my function and this works fine. On an individual plot I can fit quantile regressions u

Re: [R] using lapply and split to plot up subsets of a vector

2010-12-28 Thread Phil Spector
The data= argument to plot only makes sense if the first argument is a formula. So if you change the plot command in your function to plot(ln.o2con~lnbm,data=df) you might get what you want. But I would suggest you take a look at the plot produced by library(lattice) xyplot(ln.o2con~l

[R] using lapply and split to plot up subsets of a vector

2010-12-28 Thread karmakiller
Hi, I would like to be able to plot data from each of the sp.id on individual plots. At the moment I can plot all the data on one graph with the following commands but I cannot figure out how to get individual graph for each sp.id. i<- function(df)plot(lnbm,ln.o2con,data=df) j<- lapply(split(one

Re: [R] Using lapply with two lists

2010-03-26 Thread anna
Yes thank you Jorge :working: - Anna Lippel -- View this message in context: http://n4.nabble.com/Using-lapply-with-two-lists-tp1692883p1692898.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list htt

Re: [R] Using lapply with two lists

2010-03-26 Thread Jorge Ivan Velez
Hi Anna, Here is a suggestion: # creating L1 L1 <- lapply(1:5, function(x) matrix(rnorm(10), ncol = 2)) L1 # creating L2 L2 <- lapply(1:5, function(x) sample(5, 2)) L2 # defining a function foo <- function(x, y) list(x[-y,]) # result mapply(foo, L1, L2) HTH, Jorge On Fri, Mar 26, 2010 at 5:3

Re: [R] Using lapply with two lists

2010-03-26 Thread Riley, Steve
-5672 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of anna Sent: Friday, March 26, 2010 5:35 PM To: r-help@r-project.org Subject: [R] Using lapply with two lists Hello guys, I have a list L1 of matrix. I have another list L2 with

[R] Using lapply with two lists

2010-03-26 Thread anna
Hello guys, I have a list L1 of matrix. I have another list L2 with the same number of elements representing the row of the L matrix that I want to delete (L1[[i]][-L2[[i]],]) but I can't do this with lapply as it iterates through L1 (first argument) and not L2. Any idea? - Anna Lippel -- V

Re: [R] Using lapply and list names not available

2008-02-05 Thread john seers (IFR)
OK, that looks a good suggestion. Though it is a bit of a step towards loops and counting ... Thanks a lot. Regards JS -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: Tue 2/5/2008 4:51 PM To: john seers (IFR) Cc: R Help Subject: Re: [R] Using lapply

Re: [R] Using lapply and list names not available

2008-02-05 Thread Gabor Grothendieck
e lapply to plot the > data (or whatever) and be able to slap a label on it so I can keep track > of what I am doing. > > Regards > > John Seers > > > > > -Original Message- > From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] > Sent: 05 February 2008

Re: [R] Using lapply and list names not available

2008-02-05 Thread john seers (IFR)
plot the data (or whatever) and be able to slap a label on it so I can keep track of what I am doing. Regards John Seers -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: 05 February 2008 16:17 To: john seers (IFR) Cc: R Help Subject: Re: [R] Using lapply and

Re: [R] Using lapply and list names not available

2008-02-05 Thread Dimitris Rizopoulos
9 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: "john seers (IFR)" <[EMAIL PROTECTED]> To: "R Help" <[EMAIL PROTECTED]> Sent: Tuesday, February 05, 2008 5:05

Re: [R] Using lapply and list names not available

2008-02-05 Thread Gabor Grothendieck
The problem is your data is in wide format and you want it in long format. See ?reshape and also see the reshape package. In your example, ?stack is sufficient: library(lattice) xyplot(values ~ seq_along(values) | ind, data = stack(people)) On Feb 5, 2008 11:05 AM, john seers (IFR) <[EMAIL PRO

[R] Using lapply and list names not available

2008-02-05 Thread john seers (IFR)
Hello All Using lapply and ending up with lists of lists I often end up in the position of not having the names of the list passed by lapply. So, if I am doing something like a plot, and I would like the title to reflect which plot it is, I cannot easily do it. So I find myself doing some unstru

Re: [R] using lapply()

2008-01-08 Thread dxc13
lic University of Leuven > > Address: Kapucijnenvoer 35, Leuven, Belgium > Tel: +32/(0)16/336899 > Fax: +32/(0)16/337015 > Web: http://med.kuleuven.be/biostat/ > http://www.student.kuleuven.be/~m0390867/dimitris.htm > > > - Original Message ----- > From: "

Re: [R] using lapply()

2008-01-08 Thread Dimitris Rizopoulos
dimitris.htm - Original Message - From: "dxc13" <[EMAIL PROTECTED]> To: Sent: Tuesday, January 08, 2008 6:11 PM Subject: [R] using lapply() > > useR's, > > I am trying to find a quick way to change some values in a list that > are > subjec

[R] using lapply()

2008-01-08 Thread dxc13
useR's, I am trying to find a quick way to change some values in a list that are subject to a condition to be NA. Consider the 3x1 matrix: delta <- matrix(c(2.5,2.5,1), nrow = 1) And consider the list named v that has 3 elements > v v[[1]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]