# [R] Subset and plot

Petr PIKAL petr.pikal at precheza.cz
Wed Feb 3 10:23:46 CET 2010

```Hi

r-help-bounces at r-project.org napsal dne 03.02.2010 02:54:46:

> Let's look at your data frame:
>
> > str(daily.sub1)
> 'data.frame':   9 obs. of  4 variables:
>  \$ Trial: Factor w/ 1 level "2": 1 1 1 1 1 1 1 1 1
>  \$ Tanks: Factor w/ 3 levels "a4","c4","h4": 1 1 1 2 2 2 3 3 3
>  \$ Day  : Factor w/ 9 levels "10","11","12",..: 4 5 6 7 8 9 1 2 3
>  \$ Wgt  : Factor w/ 9 levels "16","17","18",..: 1 2 3 4 5 6 7 8 9
>
> When you did the cbind to get daily, it converted the matrix to
character;
> therefore, when you coerced it to a data frame, everything was read as a
> factor. What you needed to do was
>
> daily <- data.frame(Trial = rep(c(1,2),each=12),
> Tanks=rep(rep(c("a3","a4","c4","h4"),each=3),2),
>                             Day=rep(c(1:12),2),
>                             Wgt=c(1:24))
> > str(daily)
> 'data.frame':   24 obs. of  4 variables:
>  \$ Trial: num  1 1 1 1 1 1 1 1 1 1 ...
>  \$ Tanks: Factor w/ 4 levels "a3","a4","c4",..: 1 1 1 2 2 2 3 3 3 4 ...
>  \$ Day  : int  1 2 3 4 5 6 7 8 9 10 ...
>  \$ Wgt  : int  1 2 3 4 5 6 7 8 9 10 ...
>
> Then...
> daily.sub<-subset(daily, subset=Trial==2 & Tanks=="a4"|Trial==2 &
> Tanks=="c4"|Trial==2 & Tanks=="h4")
> > str(daily.sub)
> 'data.frame':   9 obs. of  4 variables:
>  \$ Trial: num  2 2 2 2 2 2 2 2 2
>  \$ Tanks: Factor w/ 4 levels "a3","a4","c4",..: 2 2 2 3 3 3 4 4 4
>  \$ Day  : int  4 5 6 7 8 9 10 11 12
>  \$ Wgt  : int  16 17 18 19 20 21 22 23 24
>
> But you're still not done because Tanks is a factor and subscripts have
> to be numeric, so
>
> daily.sub\$tanks <- as.numeric(as.character(daily.sub\$Tanks))

Shouldn't it be

daily.sub\$tanks <- as.numeric(daily.sub\$Tanks)

Regards
Petr

>
> and *now* your plot will work...
> plot(Wgt ~ Day, data = daily.sub,  pch=c(2,19,21)[tanks])
>
> the choice of plotting character being determined by the value of tanks.
>
> HTH,
> Dennis
>
>
>
> On Tue, Feb 2, 2010 at 5:19 PM, Marlin Keith Cox
>
> > I tried the following and still could not get it to work.  I
understand
> > your
> > logic, but cannot get this to work.
> >
> > rm(list=ls())
> > Trial<-rep(c(1,2),each=12)
> > Tanks=rep(c("a3","a4","c4","h4"),each=3,2)
> > Day=rep(c(1:12),2)
> > Wgt=c(1:24)
> > daily<-cbind(Trial, Tanks, Day, Wgt)
> > daily
> > daily.sub<-subset(daily, subset=Trial==2 & Tanks=="a4"|Trial==2 &
> > Tanks=="c4"|Trial==2 & Tanks=="h4")
> > daily.sub1<-as.data.frame(daily.sub)
> >
> > x11()
> > plot(Day, Wgt, pch=c(2,19,21)[Tanks])
> > with(daily.sub1,c(2,19,21)[Tanks])
> >
> > On Tue, Feb 2, 2010 at 1:05 PM, Jeff Laake <Jeff.Laake at noaa.gov>
wrote:
> >
> > > The problem is with attach.  You should have seen an error that the
> > objects
> > > are aliased.  You have Tanks in your workspace and in the attached
> > > dataframe. It is using the one in your workspace which is not a
factor
> > > variable.  Try:
> > >
> > >
> > >  c(2,19,21)[Tanks]
> > > with(daily.sub1,c(2,19,21)[Tanks])
> > >
> > > Avoid attach and use with which is a temporary attach that won't be
> > subject
> > > to that problem.
> > >
> > > --jeff
> > >
> > > On 2/2/2010 11:51 AM, Marlin Keith Cox wrote:
> > >
> > >> Here is a runable program.  When I plot Day and Wgt, it graphs all
the
> > >> data
> > >> points.  All I need is daily.sub1 plotted.  I also need each
"Tanks" to
> > >> have
> > >> its own col or pch.  When I run it with the line with pch, it gives
me
> > >> nothing.
> > >>
> > >> rm(list=ls())
> > >> Trial<-rep(c(1,2),each=12)
> > >> Tanks=rep(c("a3","a4","c4","h4"),each=3,2)
> > >> Day=rep(c(1:12),2)
> > >> Wgt=c(1:24)
> > >> daily<-cbind(Trial, Tanks, Day, Wgt)
> > >> daily
> > >> daily.sub<-subset(daily, subset=Trial==2&  Tanks=="a4"|Trial==2&
> > >> Tanks=="c4"|Trial==2&  Tanks=="h4")
> > >> daily.sub1<-as.data.frame(daily.sub)
> > >> attach(daily.sub1)
> > >> daily.sub1
> > >> x11()
> > >> plot(Day, Wgt)
> > >> #plot(Day, Wgt, pch=c(2,19,21)[Tanks])
> > >> detach(daily.sub1)
> > >>
> > >>
> > >>
> > >
> >
> >
> > --
> > M. Keith Cox, Ph.D.
> > Alaska NOAA Fisheries, National Marine Fisheries Service
> > Auke Bay Laboratories
> > 17109 Pt. Lena Loop Rd.
> > Juneau, AK 99801
> > Keith.Cox at noaa.gov
> > U.S. (907) 789-6603
> >
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help