Dear all, I would like to use the values in vegaggr.BLMCMR02$colony str(vegaggr.BLMCMR02) `data.frame': 1678 obs. of 3 variables: $ vegtype : Factor w/ 27 levels "2010","2020",..: 3 4 5 19 4 5 19 5 $ colony : Factor w/ 406 levels "0","1","10","100",..: 1 1 1 1 2 2 2 $ Totvegproparea: num 0.00055 0.03956 0.95705 0.00284 0.05215 ... as the seq in a for loop (for(var in seq)), and therefore first picked them out as cols = aggregate(list(A = vegaggr.BLMCMR02$Totvegproparea), list(colony = vegaggr.BLMCMR02$colony), sum)[,1]> str(cols)Factor w/ 406 levels "0","1","10","100",..: 1 2 3 4 5 6 7 8 9 10 ... I next planned to transform cols using as.integer(cols). However, this transformation gives a vector corresponding to seq(from=1,to=length(cols)). Could someone please give advice on how to make use of a factor as the seq in a for loop (which is strictly not allowed' according to ?Control). Thanks! Tord
Tord Snall <Tord.Snall at helsinki.fi> writes:> Dear all, > I would like to use the values in vegaggr.BLMCMR02$colony > > str(vegaggr.BLMCMR02) > `data.frame': 1678 obs. of 3 variables: > $ vegtype : Factor w/ 27 levels "2010","2020",..: 3 4 5 19 4 5 19 5 > $ colony : Factor w/ 406 levels "0","1","10","100",..: 1 1 1 1 2 2 2 > $ Totvegproparea: num 0.00055 0.03956 0.95705 0.00284 0.05215 ... > > as the seq in a for loop (for(var in seq)), and therefore first picked them > out as > cols = aggregate(list(A = vegaggr.BLMCMR02$Totvegproparea), > list(colony = vegaggr.BLMCMR02$colony), sum)[,1] > > str(cols) > Factor w/ 406 levels "0","1","10","100",..: 1 2 3 4 5 6 7 8 9 10 ... > > I next planned to transform cols using as.integer(cols). However, this > transformation gives a vector corresponding to seq(from=1,to=length(cols)).Yes, it would do that... The construction looks a bit complicated, though. If you want the levels of the factor, wouldn't levels(vegaggr.BLMCMR02$colony) do it?> Could someone please give advice on how to make use of a factor as the seq > in a for loop (which is strictly not allowed' according to ?Control).You're not exactly being clear about what the for loop should achieve! -- O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On 9/19/05, Tord Snall <Tord.Snall at helsinki.fi> wrote:> Dear all, > I would like to use the values in vegaggr.BLMCMR02$colony > > str(vegaggr.BLMCMR02) > `data.frame': 1678 obs. of 3 variables: > $ vegtype : Factor w/ 27 levels "2010","2020",..: 3 4 5 19 4 5 19 5 > $ colony : Factor w/ 406 levels "0","1","10","100",..: 1 1 1 1 2 2 2 > $ Totvegproparea: num 0.00055 0.03956 0.95705 0.00284 0.05215 ... > > as the seq in a for loop (for(var in seq)), and therefore first picked them > out as > cols = aggregate(list(A = vegaggr.BLMCMR02$Totvegproparea), > list(colony = vegaggr.BLMCMR02$colony), sum)[,1] > > str(cols) > Factor w/ 406 levels "0","1","10","100",..: 1 2 3 4 5 6 7 8 9 10 ... > > I next planned to transform cols using as.integer(cols). However, this > transformation gives a vector corresponding to seq(from=1,to=length(cols)). > > Could someone please give advice on how to make use of a factor as the seq > in a for loop (which is strictly not allowed' according to ?Control). >What are you trying to loop over? Species <- head(iris$Species) for(i in seq(Species)) print(Species[i]) will loop over the elements of the factor Species and you can use for(lev in levels(Species)) print(lev) to loop over its levels.
Does the follow help? > aFactor <- factor(rep(letters[1:3], 1:3)) > aFactor [1] a b b c c c Levels: a b c > for(af in levels(aFactor)){ + print(sum(aFactor==af)) + } [1] 1 [1] 2 [1] 3 spencer graves Tord Snall wrote:> Dear all, > I would like to use the values in vegaggr.BLMCMR02$colony > > str(vegaggr.BLMCMR02) > `data.frame': 1678 obs. of 3 variables: > $ vegtype : Factor w/ 27 levels "2010","2020",..: 3 4 5 19 4 5 19 5 > $ colony : Factor w/ 406 levels "0","1","10","100",..: 1 1 1 1 2 2 2 > $ Totvegproparea: num 0.00055 0.03956 0.95705 0.00284 0.05215 ... > > as the seq in a for loop (for(var in seq)), and therefore first picked them > out as > cols = aggregate(list(A = vegaggr.BLMCMR02$Totvegproparea), > list(colony = vegaggr.BLMCMR02$colony), sum)[,1] > >>str(cols) > > Factor w/ 406 levels "0","1","10","100",..: 1 2 3 4 5 6 7 8 9 10 ... > > I next planned to transform cols using as.integer(cols). However, this > transformation gives a vector corresponding to seq(from=1,to=length(cols)). > > Could someone please give advice on how to make use of a factor as the seq > in a for loop (which is strictly not allowed' according to ?Control). > > Thanks! > > Tord > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- Spencer Graves, PhD Senior Development Engineer PDF Solutions, Inc. 333 West San Carlos Street Suite 700 San Jose, CA 95110, USA spencer.graves at pdf.com www.pdf.com <http://www.pdf.com> Tel: 408-938-4420 Fax: 408-280-7915
Dear Spencer,> Does the follow help?Yes, as did the earlier replies. Thanks! Tord> > > aFactor <- factor(rep(letters[1:3], 1:3)) > > aFactor > [1] a b b c c c > Levels: a b c > > for(af in levels(aFactor)){ > + print(sum(aFactor==af)) > + } > [1] 1 > [1] 2 > [1] 3 > > spencer graves > > Tord Snall wrote: > > > Dear all, > > I would like to use the values in vegaggr.BLMCMR02$colony > > > > str(vegaggr.BLMCMR02) > > `data.frame': 1678 obs. of 3 variables: > > $ vegtype : Factor w/ 27 levels "2010","2020",..: 3 4 5 19 4 5 19 > 5 > > $ colony : Factor w/ 406 levels "0","1","10","100",..: 1 1 1 1 2 > 2 2 > > $ Totvegproparea: num 0.00055 0.03956 0.95705 0.00284 0.05215 ... > > > > as the seq in a for loop (for(var in seq)), and therefore first picked > them > > out as > > cols = aggregate(list(A = vegaggr.BLMCMR02$Totvegproparea), > > list(colony = vegaggr.BLMCMR02$colony), sum)[,1] > > > >>str(cols) > > > > Factor w/ 406 levels "0","1","10","100",..: 1 2 3 4 5 6 7 8 9 10 ... > > > > I next planned to transform cols using as.integer(cols). However, this > > > transformation gives a vector corresponding to > seq(from=1,to=length(cols)). > > > > Could someone please give advice on how to make use of a factor as the > seq > > in a for loop (which is strictly not allowed' according to ?Control). > > > > Thanks! > > > > Tord > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > -- > Spencer Graves, PhD > Senior Development Engineer > PDF Solutions, Inc. > 333 West San Carlos Street Suite 700 > San Jose, CA 95110, USA > > spencer.graves at pdf.com > www.pdf.com <http://www.pdf.com> > Tel: 408-938-4420 > Fax: 408-280-7915 >