Dear all, let say I have following list: Dat <- vector("list", length = 26) names(Dat) <- LETTERS My_Function <- function(x) return(rnorm(5)) Dat1 <- lapply(Dat, My_Function) However I want to apply my function 'My_Function' for all elements of 'Dat' except the elements having 'names(Dat) == "P"'. Here I have specified the name "P" just for illustration however this will be some name specified by user. Is there any direct way to achieve this, using 'lapply'? Thanks for your help.
What about: lapply(Dat[names(Dat) != "P"], My_Function) You could use %in% if you actually want to match a longer set of names. Sarah On Fri, Dec 14, 2012 at 1:58 PM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:> Dear all, let say I have following list: > > Dat <- vector("list", length = 26) > names(Dat) <- LETTERS > My_Function <- function(x) return(rnorm(5)) > Dat1 <- lapply(Dat, My_Function) > > > However I want to apply my function 'My_Function' for all elements of 'Dat' > except the elements having 'names(Dat) == "P"'. Here I have specified the > name "P" just for illustration however this will be some name specified by > user. > > Is there any direct way to achieve this, using 'lapply'? > > Thanks for your help. >-- Sarah Goslee http://www.functionaldiversity.org
How about Dat1 <- lapply(subset(Dat, Dat!="P"), My_Function) --Mark ________________________________ From: Christofer Bogaso <bogaso.christofer@gmail.com> To: r-help@r-project.org Sent: Friday, December 14, 2012 1:58 PM Subject: [R] A question on list and lapply Dear all, let say I have following list: Dat <- vector("list", length = 26) names(Dat) <- LETTERS My_Function <- function(x) return(rnorm(5)) Dat1 <- lapply(Dat, My_Function) However I want to apply my function 'My_Function' for all elements of 'Dat' except the elements having 'names(Dat) == "P"'. Here I have specified the name "P" just for illustration however this will be some name specified by user. Is there any direct way to achieve this, using 'lapply'? Thanks for your help. ______________________________________________ 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]]
Hi, If you want the list element "P" to be present as NULL in the result you could use this: set.seed(51) lapply(lapply(Dat,My_Function),function(x) {if(names(Dat)[match.call()[[2]][[3]]]%in% "P") NULL else x}) A.K. ----- Original Message ----- From: Christofer Bogaso <bogaso.christofer at gmail.com> To: r-help at r-project.org Cc: Sent: Friday, December 14, 2012 1:58 PM Subject: [R] A question on list and lapply Dear all, let say I have following list: Dat <- vector("list", length = 26) names(Dat) <- LETTERS My_Function <- function(x) return(rnorm(5)) Dat1 <- lapply(Dat, My_Function) However I want to apply my function 'My_Function' for all elements of 'Dat' except the elements having 'names(Dat) == "P"'. Here I have specified the name "P" just for illustration however this will be some name specified by user. Is there any direct way to achieve this, using 'lapply'? Thanks for your help. ______________________________________________ R-help at 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.
Yes, my example should have either applied lapply to structure(seq_along(bases), names=names(bases)) instead of just seq_along(bases) or added the names(bases) to the ouput of lapply (assuming one wanted the names of 'bases' on the output). Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: arun [mailto:smartpink111 at yahoo.com] > Sent: Friday, December 14, 2012 1:37 PM > To: William Dunlap > Cc: R help > Subject: Re: [R] A question on list and lapply > > HI, > > By applying: > bases > #$O > #[1] "Oak Harbor" > > #$P > #[1] "Pensicola" > # > #$Q > #[1] "Quonset Point" > > res2<- lapply(bases,function(x) {if(names(bases)[match.call()[[2]][[3]]]%in% "P") > tolower(x) else paste0("(",x,")")}) > res2 > #$O > #[1] "(Oak Harbor)" > # > #$P > #[1] "pensicola" > # > #$Q > #[1] "(Quonset Point)" > #the names of the list elements are not lost unless, it is named again. > > > res1<-lapply(seq_along(bases), function(i){ base <- bases[i] ; if (names(base) != "P") > paste0("(",base,")") else tolower(base) } ) > names(res1) > #NULL > ?names(res2) > #[1] "O" "P" "Q" > A.K. > > > > ----- Original Message ----- > From: William Dunlap <wdunlap at tibco.com> > To: arun <smartpink111 at yahoo.com>; Christofer Bogaso > <bogaso.christofer at gmail.com> > Cc: R help <r-help at r-project.org> > Sent: Friday, December 14, 2012 3:59 PM > Subject: RE: [R] A question on list and lapply > > > lapply(lapply(Dat,My_Function),function(x) {if(names(Dat)[match.call()[[2]][[3]]]%in% > > "P") NULL else x}) > > match.call()[[2]][[3]], gack! > > In lapply(X, FUN), FUN is applied to X[[i]], which has lost the names attribute that X > may have had.? X[i] retains a part of the names attribute (since it is a sublist of X, not an > element > of X).? Hence FUN can look at the name associated with X[i] with code like the following: > ? ? lapply(seq_along(X), FUN=function(i) { Xi <- X[i] ; names(Xi) }) > > E.g., to apply one sort of processing to elements named "P" and another sort to those > not named "P" you can do: > ? > bases <- list(O="Oak Harbor",P="Pensicola",Q="Quonset Point") > ? > lapply(seq_along(bases), function(i){ base <- bases[i] ; if (names(base) != "P") > paste0("(",base,")") else tolower(base) } ) > ? [[1]] > ? [1] "(Oak Harbor)" > > ? [[2]] > ? [1] "pensicola" > > ? [[3]] > ? [1] "(Quonset Point)" > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > > -----Original Message----- > > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > > Of arun > > Sent: Friday, December 14, 2012 12:11 PM > > To: Christofer Bogaso > > Cc: R help > > Subject: Re: [R] A question on list and lapply > > > > Hi, > > > > If you want the list element "P" to be present as NULL in the result > > you could use this: > > set.seed(51) > > lapply(lapply(Dat,My_Function),function(x) {if(names(Dat)[match.call()[[2]][[3]]]%in% > > "P") NULL else x}) > > A.K. > > > > > > > > > > ----- Original Message ----- > > From: Christofer Bogaso <bogaso.christofer at gmail.com> > > To: r-help at r-project.org > > Cc: > > Sent: Friday, December 14, 2012 1:58 PM > > Subject: [R] A question on list and lapply > > > > Dear all, let say I have following list: > > > > Dat <- vector("list", length = 26) > > names(Dat) <- LETTERS > > My_Function <- function(x) return(rnorm(5)) > > Dat1 <- lapply(Dat, My_Function) > > > > > > However I want to apply my function 'My_Function' for all elements of 'Dat' except the > > elements having 'names(Dat) == "P"'. Here I have specified the name "P" just for > > illustration however this will be some name specified by user. > > > > Is there any direct way to achieve this, using 'lapply'? > > > > Thanks for your help. > > > > ______________________________________________ > > R-help at 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 at 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.
If you don't need the "P" element in the output, then I think the answers you've already received are good. But if you do want to retain the "P" element, then I think it's better to simply add the missing elements back in after using lapply. The code will be easier to understand a year from now. Here's what I would do: Dat <- vector("list", length = 26) names(Dat) <- LETTERS exclude <- c('P','Z') tmp1 <- Dat[ setdiff(names(Dat), exclude) ] tmp2 <- Dat[ intersect(names(Dat), exclude) ] myfun <- function(x) rnorm(5) ## return() not needed Dat1 <- c( lapply(tmp1, myfun) , tmp2) ## put back in original order if desired Dat1 <- Dat1[ names(Dat) ] Is there a "direct way" to achieve this *and* include the 'P' element in Dat1? Not that I know of. -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 12/14/12 10:58 AM, "Christofer Bogaso" <bogaso.christofer at gmail.com> wrote:>Dear all, let say I have following list: > >Dat <- vector("list", length = 26) >names(Dat) <- LETTERS >My_Function <- function(x) return(rnorm(5)) >Dat1 <- lapply(Dat, My_Function) > > >However I want to apply my function 'My_Function' for all elements of >'Dat' except the elements having 'names(Dat) == "P"'. Here I have >specified the name "P" just for illustration however this will be some >name specified by user. > >Is there any direct way to achieve this, using 'lapply'? > >Thanks for your help. > >______________________________________________ >R-help at 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.