Hi R, I wanted to know how do we access the elements of a list. In particular, v=list(c(1,2,3,4,5),c(1,2,33,4,5),c(1,2,333,4,5),c(1,2,3333,4,5)) I want to access all the thirds items of the elements of the list. i.e., I want to access the elements, 3,33,333,3333. This can be done through sapply as: sapply(v,function(x) x[3]) But I need to access this without using 'sapply'. Like in a data frame, we do df[2,3] etc.... Any ideas? BR, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}}
Hi All I have looked but cannot find an answer to this. I want to build a formula dynamically (in a function), using for example paste, and using it in a model: fr<-"F1" f1<-formula(paste(fr, "~ SensoryTerm")) m1<-aov(f1, data=vdata) So this is the equivalent of m1<-aov(F1 ~ SensoryTerm, data=vdata) This works fine but the problem is the formula appears as "f1" in the summary(m1) of the model and not "F1 ~ SensoryTerm". With many models this can be a bit confusing .... Is there a way to code this so the formula appears in the model summary? Regards John Seers> sessionInfo()R version 2.6.1 (2007-11-26) i386-pc-mingw32 locale: LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices datasets utils methods base other attached packages: [1] pixmap_0.4-7 gplots_2.3.2 gdata_2.3.1 gtools_2.4.0 rcom_1.5-2.2 [6] nnet_7.2-38 e1071_1.5-17 class_7.2-38 tree_1.0-26 mgcv_1.3-29 [11] MASS_7.2-38 car_1.2-7 RODBC_1.2-2 RWinEdt_1.7-9>---
use as.formula() rather than just formula(). Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of john seers (IFR) Sent: Friday, 1 February 2008 7:31 PM To: r-help at stat.math.ethz.ch Subject: [R] Building a formula using paste Hi All I have looked but cannot find an answer to this. I want to build a formula dynamically (in a function), using for example paste, and using it in a model: fr<-"F1" f1<-formula(paste(fr, "~ SensoryTerm")) m1<-aov(f1, data=vdata) So this is the equivalent of m1<-aov(F1 ~ SensoryTerm, data=vdata) This works fine but the problem is the formula appears as "f1" in the summary(m1) of the model and not "F1 ~ SensoryTerm". With many models this can be a bit confusing .... Is there a way to code this so the formula appears in the model summary? Regards John Seers> sessionInfo()R version 2.6.1 (2007-11-26) i386-pc-mingw32 locale: LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices datasets utils methods base other attached packages: [1] pixmap_0.4-7 gplots_2.3.2 gdata_2.3.1 gtools_2.4.0 rcom_1.5-2.2 [6] nnet_7.2-38 e1071_1.5-17 class_7.2-38 tree_1.0-26 mgcv_1.3-29 [11] MASS_7.2-38 car_1.2-7 RODBC_1.2-2 RWinEdt_1.7-9>--- ______________________________________________ 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.
I find it hard to imagine you need to do it without using sapply. If you get over your phobia, you might like to try sapply(v, "[", 3) If the elements of the list are all the same length, as in your example, you could do data.frame(v)[3, ] I think we need to know more about why you think you can't use sapply before we can really help you, though. Bill Venables. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha Vishwanath Karanth Sent: Friday, 1 February 2008 7:05 PM To: r-help at stat.math.ethz.ch Subject: [R] Accessing the elements of a list Hi R, I wanted to know how do we access the elements of a list. In particular, v=list(c(1,2,3,4,5),c(1,2,33,4,5),c(1,2,333,4,5),c(1,2,3333,4,5)) I want to access all the thirds items of the elements of the list. i.e., I want to access the elements, 3,33,333,3333. This can be done through sapply as: sapply(v,function(x) x[3]) But I need to access this without using 'sapply'. Like in a data frame, we do df[2,3] etc.... Any ideas? BR, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} ______________________________________________ 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.
Please disregard my previous reply. Now that I have read your question, all becomes clear. To do what you want to do is a bit tricky. Here is one way f1 <- as.name("F1") fm <- eval(bquote(aov(.(f1) ~ sensoryTerm, data = vdata))) Them fm is the fitted model object, suitably formed. E&OE, of course! Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Venables, Bill (CMIS, Cleveland) Sent: Friday, 1 February 2008 7:45 PM To: john.seers at bbsrc.ac.uk; r-help at stat.math.ethz.ch Subject: Re: [R] Building a formula using paste use as.formula() rather than just formula(). Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of john seers (IFR) Sent: Friday, 1 February 2008 7:31 PM To: r-help at stat.math.ethz.ch Subject: [R] Building a formula using paste Hi All I have looked but cannot find an answer to this. I want to build a formula dynamically (in a function), using for example paste, and using it in a model: fr<-"F1" f1<-formula(paste(fr, "~ SensoryTerm")) m1<-aov(f1, data=vdata) So this is the equivalent of m1<-aov(F1 ~ SensoryTerm, data=vdata) This works fine but the problem is the formula appears as "f1" in the summary(m1) of the model and not "F1 ~ SensoryTerm". With many models this can be a bit confusing .... Is there a way to code this so the formula appears in the model summary? Regards John Seers> sessionInfo()R version 2.6.1 (2007-11-26) i386-pc-mingw32 locale: LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices datasets utils methods base other attached packages: [1] pixmap_0.4-7 gplots_2.3.2 gdata_2.3.1 gtools_2.4.0 rcom_1.5-2.2 [6] nnet_7.2-38 e1071_1.5-17 class_7.2-38 tree_1.0-26 mgcv_1.3-29 [11] MASS_7.2-38 car_1.2-7 RODBC_1.2-2 RWinEdt_1.7-9>--- ______________________________________________ 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.
Shubha Vishwanath Karanth wrote:> Hi R, > > > > I wanted to know how do we access the elements of a list. In particular, > > > > v=list(c(1,2,3,4,5),c(1,2,33,4,5),c(1,2,333,4,5),c(1,2,3333,4,5)) > > > > I want to access all the thirds items of the elements of the list. i.e., > I want to access the elements, 3,33,333,3333. This can be done through > sapply as: > > > > sapply(v,function(x) x[3]) > > > > But I need to access this without using 'sapply'. Like in a data frame, > we do df[2,3] etc.... Any ideas? > >Hi, not the most elegant, but this works: cmd > unlist(as.data.frame(v)[3,]) c.1..2..3..4..5. c.1..2..33..4..5. c.1..2..333..4..5. c.1..2..3333..4..5. 3 33 333 3333 Cheers, Thibaut. -- ###################################### Thibaut JOMBART CNRS UMR 5558 - Laboratoire de Biom?trie et Biologie Evolutive Universite Lyon 1 43 bd du 11 novembre 1918 69622 Villeurbanne Cedex T?l. : 04.72.43.29.35 Fax : 04.72.43.13.88 jombart at biomserv.univ-lyon1.fr http://lbbe.univ-lyon1.fr/-Jombart-Thibaut-.html?lang=en http://pbil.univ-lyon1.fr/software/adegenet/
Thank you very much. That problem has been niggling me for some time. I slotted in your code and it worked. I just need to spend a bit of time understanding it ... Thanks again. John Seers --- -----Original Message----- From: Bill.Venables at csiro.au [mailto:Bill.Venables at csiro.au] Sent: 01 February 2008 09:57 To: Bill.Venables at csiro.au; john seers (IFR); r-help at stat.math.ethz.ch Subject: RE: [R] Building a formula using paste Please disregard my previous reply. Now that I have read your question, all becomes clear. To do what you want to do is a bit tricky. Here is one way f1 <- as.name("F1") fm <- eval(bquote(aov(.(f1) ~ sensoryTerm, data = vdata))) Them fm is the fitted model object, suitably formed. E&OE, of course! Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Venables, Bill (CMIS, Cleveland) Sent: Friday, 1 February 2008 7:45 PM To: john.seers at bbsrc.ac.uk; r-help at stat.math.ethz.ch Subject: Re: [R] Building a formula using paste use as.formula() rather than just formula(). Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of john seers (IFR) Sent: Friday, 1 February 2008 7:31 PM To: r-help at stat.math.ethz.ch Subject: [R] Building a formula using paste Hi All I have looked but cannot find an answer to this. I want to build a formula dynamically (in a function), using for example paste, and using it in a model: fr<-"F1" f1<-formula(paste(fr, "~ SensoryTerm")) m1<-aov(f1, data=vdata) So this is the equivalent of m1<-aov(F1 ~ SensoryTerm, data=vdata) This works fine but the problem is the formula appears as "f1" in the summary(m1) of the model and not "F1 ~ SensoryTerm". With many models this can be a bit confusing .... Is there a way to code this so the formula appears in the model summary? Regards John Seers> sessionInfo()R version 2.6.1 (2007-11-26) i386-pc-mingw32 locale: LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices datasets utils methods base other attached packages: [1] pixmap_0.4-7 gplots_2.3.2 gdata_2.3.1 gtools_2.4.0 rcom_1.5-2.2 [6] nnet_7.2-38 e1071_1.5-17 class_7.2-38 tree_1.0-26 mgcv_1.3-29 [11] MASS_7.2-38 car_1.2-7 RODBC_1.2-2 RWinEdt_1.7-9>--- ______________________________________________ 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.