Having given myself carpal tunnel looking for answer to this ... I have a dataset each column of which has 12 rows in it. I created a variable 'z' as follows: z=1:24 Since I have a large number of these plots to make, and they are a bit complex, I want to want to reference the column I want to plot via a variable containing the name of that column. As follows: similar='1978' s=paste('Y',similar,sep='') variable s now contains 'Y1978' which is the name of one of the columns. However, when I try to plot plot(z,s,type='l') I get a 'x and y lengths differ' error because variable s is being recognized as 'Y1978' length=1, rather than the contents of the column Y1978 length=12. I tried all the usual tricks I know like &s. How do you get R to reference a variable as a column name? Thank you. Gregory A. Graves, Lead Scientist Everglades REstoration COoordination and VERification (RECOVER) Restoration Sciences Department South Florida Water Management District Phones: DESK: 561 / 682 - 2429 CELL: 561 / 719 - 8157 [[alternative HTML version deleted]]
On Nov 30, 2010, at 11:40 AM, Graves, Gregory wrote:> Having given myself carpal tunnel looking for answer to this ... > > > > I have a datasetNamed.... what?> each column of which has 12 rows in it. I created a > variable 'z' as follows: > > > > z=1:24 sWhy did you put twice as many elements in z as there are in a column?> > > > Since I have a large number of these plots to make, and they are a bit > complex, I want to want to reference the column I want to plot via a > variable containing the name of that column. As follows: > > > > similar='1978' > > s=paste('Y',similar,sep='') > > > > variable s now contains 'Y1978' which is the name of one of the > columns. > > > > However, when I try to plot > > > > plot(z,s,type='l')Try this ... assuming that there really are 12 item length columns in a dataframe named, dfm. plot(1:12, dfm[[s]], type="l") dataframes are lists that can be accessed by the names of columns which are interpreted. Don't assume that you can get such interpretation with the $ operator.> > > > I get a 'x and y lengths differ' error because variable s is being > recognized as 'Y1978' length=1, rather than the contents of the column > Y1978 length=12. > > I tried all the usual tricks I know like &s.Huh? "&" is a logical operator.> How do you get R to > reference a variable as a column name? > > > > Thank you. > > > > Gregory A. Graves, Lead Scientist > > Everglades REstoration COoordination and VERification (RECOVER) > > Restoration Sciences Department > > South Florida Water Management District > > Phones: DESK: 561 / 682 - 2429 > > CELL: 561 / 719 - 8157 > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Where is the "column"? Is it in a matrix/dataframe? Why are you trying to plot 12 points against 'z' which has 24 values? You can reference the column by: plot(yourDF[, s]) where 's' is what you created as the column name. Need to refer to indexing in the Intro to R. On Tue, Nov 30, 2010 at 11:40 AM, Graves, Gregory <ggraves at sfwmd.gov> wrote:> Having given myself carpal tunnel looking for answer to this ... > > > > I have a dataset each column of which has 12 rows in it. ?I created a > variable 'z' as follows: > > > > z=1:24 > > > > Since I have a large number of these plots to make, and they are a bit > complex, I want to want to reference the column I want to plot via a > variable containing the name of that column. ?As follows: > > > > similar='1978' > > s=paste('Y',similar,sep='') > > > > variable s now contains 'Y1978' which is the name of one of the columns. > > > > However, when I try to plot > > > > plot(z,s,type='l') > > > > I get a 'x and y lengths differ' error because variable s is being > recognized as 'Y1978' length=1, rather than the contents of the column > Y1978 length=12. > > > > I tried all the usual tricks I know like &s. ?How do you get R to > reference a variable as a column name? > > > > Thank you. > > > > Gregory A. Graves, Lead Scientist > > Everglades REstoration COoordination and VERification (RECOVER) > > Restoration Sciences Department > > South Florida Water Management District > > Phones: ?DESK: 561 / 682 - 2429 > > ? ? ? ? ? ? ? ? ? CELL: ?561 / 719 - 8157 > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?