Re: [R] Subset for plot in R
So are the names of the columns in the dataset x, y, and z? or are they area, concentration, and year? you seem to be mixing these together? If you provide a minimal reproducible example (provide some data with dput, or the commands to generate random data, or use a built in dataset) then it makes it easier for us to help you. Is the z/year variable a set of character strings? a factor? or a numeric/integer variable? Assuming the column names are x, y, and z and that z is the numeric year and data is a data frame, then here are some options that should work: plot( data$x[ data$z = 2012 ], data$y[ data$z = 2012 ) with( data[ data$z = 2012, ], plot(x,y) ) plot( y~x, data=data, subset= z = 2012 ) Also see `fortune(dog)` about choosing names for data frames. On Mon, Feb 17, 2014 at 9:18 AM, arun smartpink...@yahoo.com wrote: Hi, Try: set.seed(49) dat1 - data.frame(year= rep(2010:2013,c(10,8,9,13)),x=sample(1e4,40,replace=TRUE),y=sample(40,40,replace=TRUE)) plot(x~y,data=dat1,subset=year 2012) #or with(subset(dat1,year 2012),plot(y,x)) A.K. Hi R people This might take me the whole day to figure out, instead I will ask so I can save some PhD time. I want to plot a subset of my data. x = area y = concentration these data are sorted by z = year (2008 to 2013) So I want to make a plot where I can see x and y =2012 and 2012. I have tied plot(data$y[year='2012'], x[year='2012']) That did not work. also plot(data$y, data$x, year'2012') did not give the right data plot Any suggestions on how to do this would be appreciated. thanks __ 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. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ 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.
Re: [R] Subset for plot in R
Hi, Try: set.seed(49) dat1 - data.frame(year= rep(2010:2013,c(10,8,9,13)),x=sample(1e4,40,replace=TRUE),y=sample(40,40,replace=TRUE)) plot(x~y,data=dat1,subset=year 2012) #or with(subset(dat1,year 2012),plot(y,x)) A.K. Hi R people This might take me the whole day to figure out, instead I will ask so I can save some PhD time. I want to plot a subset of my data. x = area y = concentration these data are sorted by z = year (2008 to 2013) So I want to make a plot where I can see x and y =2012 and 2012. I have tied plot(data$y[year='2012'], x[year='2012']) That did not work. also plot(data$y, data$x, year'2012') did not give the right data plot Any suggestions on how to do this would be appreciated. thanks __ 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.
Re: [R] Subset and plot
Hi r-help-boun...@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 marlink...@gmail.comwrote: 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.la...@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@noaa.gov marlink...@gmail.com U.S. (907) 789-6603 [[alternative HTML version deleted]] __ 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]] __ 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. __ 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.
[R] Subset and plot
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@noaa.gov marlink...@gmail.com U.S. (907) 789-6603 [[alternative HTML version deleted]] __ 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.
Re: [R] Subset and plot
On 2/2/2010 2:51 PM, 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. DF - data.frame(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)) with(subset(DF, Trial==2 Tanks %in% c(a4,'c4','h4')), plot(Day, Wgt, pch=as.numeric(Tanks))) library(lattice) trellis.device(new=TRUE, color=FALSE) xyplot(Wgt ~ Day, groups=Tanks, data=subset(DF, Trial==2 Tanks %in% c(a4,'c4','h4')), par.settings=list(superpose.symbol=list(pch=c(2,19,21 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) -- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ 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.
Re: [R] Subset and plot
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) __ 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.
Re: [R] Subset and plot
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.la...@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@noaa.gov marlink...@gmail.com U.S. (907) 789-6603 [[alternative HTML version deleted]] __ 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.
Re: [R] Subset and plot
What did you ***expect*** or hope to get out of ``c(2,19,21)[Tanks]'' Please note that this expression makes no sense at all as it stands. You apparently want to use plotting symbols 2, 19, and 21 respectively, depending in some way upon the value in ``Tanks''. Since you have specified three plotting symbols and ``Tanks'' has *four* distinct values, this is somewhat mysterious. If you would just tell us what you are actually trying to *do* we would probably be able to tell you how to do it. cheers, Rolf Turner On 3/02/2010, at 2:19 PM, Marlin Keith Cox wrote: 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.la...@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@noaa.gov marlink...@gmail.com U.S. (907) 789-6603 [[alternative HTML version deleted]] __ 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. ## Attention: This e-mail message is privileged and confidential. If you are not the intended recipient please delete the message and notify the sender. Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshal www.marshalsoftware.com ## __ 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.
Re: [R] Subset and plot
Initial email: 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) On Tue, Feb 2, 2010 at 4:38 PM, Rolf Turner r.tur...@auckland.ac.nz wrote: What did you ***expect*** or hope to get out of ``c(2,19,21)[Tanks]'' Please note that this expression makes no sense at all as it stands. You apparently want to use plotting symbols 2, 19, and 21 respectively, depending in some way upon the value in ``Tanks''. Since you have specified three plotting symbols and ``Tanks'' has *four* distinct values, this is somewhat mysterious. If you would just tell us what you are actually trying to *do* we would probably be able to tell you how to do it. cheers, Rolf Turner On 3/02/2010, at 2:19 PM, Marlin Keith Cox wrote: 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.la...@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@noaa.gov marlink...@gmail.com U.S. (907) 789-6603 [[alternative HTML version deleted]] __ 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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ## Attention: This e-mail message is privileged and confidential. If you are not the intended recipient please delete the message and notify the sender. Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshal www.marshalsoftware.com ## -- M. Keith Cox, Ph.D. Alaska NOAA Fisheries, National Marine Fisheries Service Auke Bay Laboratories 17109 Pt. Lena Loop Rd. Juneau, AK 99801 keith@noaa.gov marlink...@gmail.com U.S. (907) 789-6603 [[alternative HTML version deleted]] __ 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.
Re: [R] Subset and plot
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)) 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 marlink...@gmail.comwrote: 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.la...@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@noaa.gov marlink...@gmail.com U.S. (907) 789-6603 [[alternative HTML version deleted]] __ 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]] __ 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.
Re: [R] Subset and plot
Don't need to convert the factors to numeric as that will happen automatically. See below. However as Rolf pointed out they will be numbered from 1-4 even though only 3 are left in the subset as all 4 levels are maintained. So the snippet below will work as long as you specify 4 possible pch values. Below I show how you can change to 3 levels. 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)) daily.sub-subset(daily, subset=Trial==2 Tanks==a4|Trial==2 Tanks==c4|Trial==2 Tanks==h4) with(daily.sub,plot(Wgt ~ Day, pch=c(2,19,21,23)[Tanks])) # with 3 levels daily.sub$Tanks=factor(daily.sub$Tanks) with(daily.sub,plot(Wgt ~ Day, pch=c(19,21,23)[Tanks])) On 2/2/2010 5:54 PM, Dennis Murphy wrote: plot(Wgt ~ Day, data = daily.sub, pch=c(2,19,21)[tanks]) __ 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.
[R] subset and plot
create a subset for brook_dis. When I plot (week, R) I get a nice boxplot, but along the x axis, there are weeks a, b, c along with h and nh. Thank you ahead of time. keith rm(list=ls()) cond.exp1-read.csv(condition/test.csv,header=TRUE) sub-subset(cond.exp1, Species==brook_dis) attach(sub) plot(week, R) detach(sub) Id Species week 7 bow a 10 bow a 12 bow a 19 bow a 23 bow a 24 bow a 25 bow a 26 bow a 27 bow a 28 bow a 32 bow a 39 bow a 7 bow b 10 bow b 12 bow b 19 bow b 23 bow b 24 bow b 25 bow b 26 bow b 27 bow b 28 bow b 32 bow b 39 bow b 7 bow c 10 bow c 12 bow c 19 bow c 23 bow c 24 bow c 25 bow c 26 bow c 27 bow c 28 bow c 32 bow c 39 bow c 84 brook_dis h 85 brook_dis h 86 brook_dis h 87 brook_dis h 88 brook_dis h 89 brook_dis h 90 brook_dis h 91 brook_dis nh 92 brook_dis nh 93 brook_dis nh 94 brook_dis nh 95 brook_dis nh 96 brook_dis nh 97 -- Keith Cox, Ph.D. Sitka Sound Science Center Fisheries Biologist P.O. Box 464 Sitka, Alaska, 99835 907 752-0563 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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.