?Hi: I always use subset the same way but now is returning 0 rows. What's wrong with the way I am subsetting? library(ggplot2) structure(list(first = c(38.2086, 43.1768, 43.146, 41.8044, 42.4232, 46.3646, 38.0813, 40.0745, 40.4889, 38.6246, 40.2826, 41.6056, 34.5353, 40.0768), second = c(43.3295, 42.4326, 38.8994, 37.0894, 42.3218, 46.1726, 39.1206, 41.2072, 42.4874, 40.2657, 38.7766, 40.8822, 42.0165, 49.2055), third = c(42.24, 42.992, 37.7419, 42.3448, 41.9131, 44.385, 42.7811, 44.1963, 40.8088, 43.9634, 38.7079, 38.0791, 44.3136, 39.5333)), .Names = c("first", "second", "third"), class = "data.frame", row.names = c(NA, -14L)) ?head(x);str(x) xmelt <- melt(x) ?names(xmelt) <- c("year","fatPerc") ??str(xmelt);xmelt ?# Subset to plot only the 'first' and third year firstyear <- subset(xmelt,year ==' first');str(firstyear) # works # two variables,,doesn't work firstyear <- subset(xmelt,year ==' first' & year == 'third');str(firstyear) ? Felipe D. Carrillo Supervisory Fishery Biologist Department of the Interior US Fish & Wildlife Service California, USA
Hi Felipe, On Mon, Nov 29, 2010 at 1:02 PM, Felipe Carrillo <mazatlanmexico at yahoo.com> wrote:> ?Hi: > I always use subset the same way but now is returning 0 rows. > What's wrong with the way I am subsetting? > > library(ggplot2) > structure(list(first = c(38.2086, 43.1768, 43.146, 41.8044, 42.4232, > 46.3646, 38.0813, 40.0745, 40.4889, 38.6246, 40.2826, 41.6056, > 34.5353, 40.0768), second = c(43.3295, 42.4326, 38.8994, 37.0894, > 42.3218, 46.1726, 39.1206, 41.2072, 42.4874, 40.2657, 38.7766, > 40.8822, 42.0165, 49.2055), third = c(42.24, 42.992, 37.7419, > 42.3448, 41.9131, 44.385, 42.7811, 44.1963, 40.8088, 43.9634, > 38.7079, 38.0791, 44.3136, 39.5333)), .Names = c("first", "second", > "third"), class = "data.frame", row.names = c(NA, -14L)) > ?head(x);str(x) > xmelt <- melt(x) > ?names(xmelt) <- c("year","fatPerc") > ??str(xmelt);xmelt > ?# Subset to plot only the 'first' and third year > firstyear <- subset(xmelt,year ==' first');str(firstyear) # worksReally? It does not for me. I think you need to remove the space: ' first' to 'first'> # two variables,,doesn't work > firstyear <- subset(xmelt,year ==' first' & year == 'third');str(firstyear)Same spacing problem, also I wonder if you want | instead of &. & will mean you are testing for years that are simultaneously the first AND ( & ) the third, rather than the first OR ( | )the third. See ?Logic for the distinction (in base, if it asks). HTH, Josh> > Felipe D. Carrillo > Supervisory Fishery Biologist > Department of the Interior > US Fish & Wildlife Service > California, USA > > > > > ______________________________________________ > 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.-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Silvano - If you always have exactly one med1, one med2, and one med3 for each combination of Grupos, Dias, and Rato, you can use> aggregate(Esp.Inter.Trac~Grupos+Dias+ Rato,mean,data=x)Grupos Dias Rato Esp.Inter.Trac 1 C 3 1 100.55667 2 GFC 3 1 87.62333 3 C 3 2 80.37000 4 GFC 3 2 101.33000 5 C 3 3 66.97333 6 GFC 3 3 101.52667 7 C 3 4 84.84000 8 GFC 3 4 103.97333 9 C 3 5 76.99333 10 GFC 3 5 121.67000 11 C 3 6 70.78333 12 GFC 3 6 91.41000 If there are multiples of any of med1, med2, or med3 within any of the combinations, it would be a little trickier:> one = aggregate(Esp.Inter.Trac~Grupos+Dias+Rato+blocos,mean,data=x) > two = aggregate(Esp.Inter.Trac~Grupos+Dias+Rato,mean,data=one)That would still assume there was at least one value for each of med1, med2, and med3 for each combination. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Thu, 16 Dec 2010, Silvano wrote:> Hi, > > I have a file, like below, and I want create a new data.frame with variables: > Grupos Dias Rato Esp.Inter.Trac, but Esp.Inter.Trac will be the mean of > med1 med2 and med3 for each Rato. > > How can I do this? > > > Grupos Dias Rato blocos Esp.Inter.Trac > GFC 3 1 med1 85.99 > GFC 3 2 med1 112.78 > GFC 3 3 med1 105.43 > GFC 3 4 med1 86.18 > GFC 3 5 med1 135.66 > GFC 3 6 med1 76.25 > GFC 3 1 med2 91.08 > GFC 3 2 med2 100.57 > GFC 3 3 med2 131.79 > GFC 3 4 med2 138.46 > GFC 3 5 med2 129.78 > GFC 3 6 med2 107.92 > GFC 3 1 med3 85.80 > GFC 3 2 med3 90.64 > GFC 3 3 med3 67.36 > GFC 3 4 med3 87.28 > GFC 3 5 med3 99.57 > GFC 3 6 med3 90.06 > C 3 1 med1 81.19 > C 3 2 med1 94.74 > C 3 3 med1 49.18 > C 3 4 med1 105.76 > C 3 5 med1 82.71 > C 3 6 med1 80.10 > C 3 1 med2 121.30 > C 3 2 med2 82.77 > C 3 3 med2 99.57 > C 3 4 med2 73.66 > C 3 5 med2 72.89 > C 3 6 med2 76.47 > C 3 1 med3 99.18 > C 3 2 med3 63.60 > C 3 3 med3 52.17 > C 3 4 med3 75.10 > C 3 5 med3 75.38 > C 3 6 med3 55.78 > > -------------------------------------- > Silvano Cesar da Costa > Departamento de Estat?stica > Universidade Estadual de Londrina > Fone: 3371-4346 > > ______________________________________________ > 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. >
Hi, I have a file, like below, and I want create a new data.frame with variables: Grupos Dias Rato Esp.Inter.Trac, but Esp.Inter.Trac will be the mean of med1 med2 and med3 for each Rato. How can I do this? Grupos Dias Rato blocos Esp.Inter.Trac GFC 3 1 med1 85.99 GFC 3 2 med1 112.78 GFC 3 3 med1 105.43 GFC 3 4 med1 86.18 GFC 3 5 med1 135.66 GFC 3 6 med1 76.25 GFC 3 1 med2 91.08 GFC 3 2 med2 100.57 GFC 3 3 med2 131.79 GFC 3 4 med2 138.46 GFC 3 5 med2 129.78 GFC 3 6 med2 107.92 GFC 3 1 med3 85.80 GFC 3 2 med3 90.64 GFC 3 3 med3 67.36 GFC 3 4 med3 87.28 GFC 3 5 med3 99.57 GFC 3 6 med3 90.06 C 3 1 med1 81.19 C 3 2 med1 94.74 C 3 3 med1 49.18 C 3 4 med1 105.76 C 3 5 med1 82.71 C 3 6 med1 80.10 C 3 1 med2 121.30 C 3 2 med2 82.77 C 3 3 med2 99.57 C 3 4 med2 73.66 C 3 5 med2 72.89 C 3 6 med2 76.47 C 3 1 med3 99.18 C 3 2 med3 63.60 C 3 3 med3 52.17 C 3 4 med3 75.10 C 3 5 med3 75.38 C 3 6 med3 55.78 -------------------------------------- Silvano Cesar da Costa Departamento de Estat?stica Universidade Estadual de Londrina Fone: 3371-4346
Phil, this is exactly what do I want. Thanks a lot. -------------------------------------- Silvano Cesar da Costa Departamento de Estat?stica Universidade Estadual de Londrina Fone: 3371-4346 -------------------------------------- ----- Original Message ----- From: "Phil Spector" <spector at stat.berkeley.edu> To: "Silvano" <silvano at uel.br> Cc: <r-help at r-project.org> Sent: Wednesday, December 15, 2010 5:27 PM Subject: Re: [R] subset> Silvano - > If you always have exactly one med1, one med2, and one > med3 > for each combination of Grupos, Dias, and Rato, you can > use > >> aggregate(Esp.Inter.Trac~Grupos+Dias+ Rato,mean,data=x) > Grupos Dias Rato Esp.Inter.Trac > 1 C 3 1 100.55667 > 2 GFC 3 1 87.62333 > 3 C 3 2 80.37000 > 4 GFC 3 2 101.33000 > 5 C 3 3 66.97333 > 6 GFC 3 3 101.52667 > 7 C 3 4 84.84000 > 8 GFC 3 4 103.97333 > 9 C 3 5 76.99333 > 10 GFC 3 5 121.67000 > 11 C 3 6 70.78333 > 12 GFC 3 6 91.41000 > > If there are multiples of any of med1, med2, or med3 > within > any of the combinations, it would be a little trickier: > >> one = >> aggregate(Esp.Inter.Trac~Grupos+Dias+Rato+blocos,mean,data=x) >> two = >> aggregate(Esp.Inter.Trac~Grupos+Dias+Rato,mean,data=one) > > That would still assume there was at least one value for > each of > med1, med2, and med3 for each combination. > > - Phil Spector > Statistical Computing Facility > Department of Statistics > UC Berkeley > spector at stat.berkeley.edu > > > On Thu, 16 Dec 2010, Silvano wrote: > >> Hi, >> >> I have a file, like below, and I want create a new >> data.frame with variables: >> Grupos Dias Rato Esp.Inter.Trac, but Esp.Inter.Trac >> will be the mean of >> med1 med2 and med3 for each Rato. >> >> How can I do this? >> >> >> Grupos Dias Rato blocos Esp.Inter.Trac >> GFC 3 1 med1 85.99 >> GFC 3 2 med1 112.78 >> GFC 3 3 med1 105.43 >> GFC 3 4 med1 86.18 >> GFC 3 5 med1 135.66 >> GFC 3 6 med1 76.25 >> GFC 3 1 med2 91.08 >> GFC 3 2 med2 100.57 >> GFC 3 3 med2 131.79 >> GFC 3 4 med2 138.46 >> GFC 3 5 med2 129.78 >> GFC 3 6 med2 107.92 >> GFC 3 1 med3 85.80 >> GFC 3 2 med3 90.64 >> GFC 3 3 med3 67.36 >> GFC 3 4 med3 87.28 >> GFC 3 5 med3 99.57 >> GFC 3 6 med3 90.06 >> C 3 1 med1 81.19 >> C 3 2 med1 94.74 >> C 3 3 med1 49.18 >> C 3 4 med1 105.76 >> C 3 5 med1 82.71 >> C 3 6 med1 80.10 >> C 3 1 med2 121.30 >> C 3 2 med2 82.77 >> C 3 3 med2 99.57 >> C 3 4 med2 73.66 >> C 3 5 med2 72.89 >> C 3 6 med2 76.47 >> C 3 1 med3 99.18 >> C 3 2 med3 63.60 >> C 3 3 med3 52.17 >> C 3 4 med3 75.10 >> C 3 5 med3 75.38 >> C 3 6 med3 55.78 >> >> -------------------------------------- >> Silvano Cesar da Costa >> Departamento de Estat?stica >> Universidade Estadual de Londrina >> Fone: 3371-4346 >> >> ______________________________________________ >> 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. >>