Re: [R] How to specify a data frame column using command line arg?

2016-06-06 Thread David L Carlson
Looking at your overall goal to plot multiple columns, you can use a simple 
loop with expand.grid():

> set.seed(42)
> adl1 <- matrix(sample.int(9),  3, 3)
> colnames(adl1) <- letters[1:3]
> lbls <- colnames(adl1)
> 
> ncols <- ncol(adl1)
> colnos <- as.matrix(expand.grid(1:ncols, 1:ncols))
> colnos <- colnos[colnos[, 1] < colnos[, 2], ] # Eliminate redundant 
> combinations
> colnos
 Var1 Var2
[1,]12
[2,]13
[3,]23
> 
> for (i in seq_len(nrow(colnos))) {
+  plot(adl1[, colnos[i, ]], xlab=lbls[colnos[i, 1]], 
+   ylab=lbls[colnos[i, 2]])
+ }

Plots all of the unique plots. 

-
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Changrong Ge
Sent: Monday, June 6, 2016 12:07 AM
To: Douglas Johnson
Cc: r-help@r-project.org
Subject: Re: [R] How to specify a data frame column using command line arg?

>
> Hi,
> I have recently made similar scatter plot.
> Suppose you have a data frame "data.csv" and the first column is not
> what you want to plot but rather something informative such as ID, Class,
> Group, etc which might not be your case (but would be very nice if you want
> to color your scatter plots or add cutoff line, etc). The code below would
> work well by printing out figures for all possible column i vs column j.
>
> for(i in names(data[,-1])){
> for(j in names(data[,-1])){
>
> png(paste(i," vs " ,j,".png", sep=""), width=4, height=6, units="in",
> res=300)
>
> plot(data[,i],data[,j],xlab=i,ylab=j,log="xy",pch=16)
>
>
> dev.off()
> print(c(i,j))
> }
> }
>
> Good luck
> Changrong
>
> On Sun, Jun 5, 2016 at 9:45 PM, Douglas Johnson  wrote:
>
>> I'm guessing this is trivial but I've spent two hours searching and
>> reading
>> FAQ, tutorial, introductory and 'idiom' documents and haven't found a
>> solution. All I want to do is select a data frame column at run time using
>> a command line arg. For example, the "test.csv" file contains:
>>
>> a,b,c
>> 1,2,3
>> 4,5,6
>> 7,8,9
>>
>> If I run "test.r a", I get [1,4,7].
>> If I run "test.r b", I get [2,5,8].
>>
>> Here's sample code -- how I map args[1] to column/vector 'a' or 'b' or
>> 'c'?
>>
>> args <- commandArgs(trailingOnly=T)
>>
>> adl1<-read.csv(file="test.csv",head=T,sep=",")
>>
>> adl1$SOME-MAGIC-FUNCTION-OR-SYMBOL(args[1])
>>
>>
>> All I'm really trying to do is generate a bunch of scatter plots of column
>> 'x' vs. 'y' without writing a separate program for each pair.
>>
>>
>> Thanks,
>>
>>
>> Doug
>>
>>
>>
>> --
>> http://www.dojopico.org <http://dojopico.org>
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
>
>
> --
>
> Changrong Ge, PhD
>
> Division of Medical Inflammation Research
> Department of Medical Biochemistry and Biophysics (MBB)
> Karolinska Institutet
> Scheeles väg 2, B2 Plan 4
> SE-171 77 Stockholm
> Sweden
> Tel:  +46-8-524 86337 ,   Mobile: +46-(0)76-2878 029
> Fax: +46-8-524 87750 ,  Email: changrong...@ki.se 
>  or changrong...@gmail.com
>
>


-- 

Changrong Ge, PhD

Division of Medical Inflammation Research
Department of Medical Biochemistry and Biophysics (MBB)
Karolinska Institutet
Scheeles väg 2, B2 Plan 4
SE-171 77 Stockholm
Sweden
Tel:  +46-8-524 86337 ,   Mobile: +46-(0)76-2878 029
Fax: +46-8-524 87750 ,  Email: changrong...@ki.se  or
changrong...@gmail.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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 -- To UNSUBSCRIBE and more, see
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] How to specify a data frame column using command line arg?

2016-06-06 Thread Changrong Ge
>
> Hi,
> I have recently made similar scatter plot.
> Suppose you have a data frame "data.csv" and the first column is not
> what you want to plot but rather something informative such as ID, Class,
> Group, etc which might not be your case (but would be very nice if you want
> to color your scatter plots or add cutoff line, etc). The code below would
> work well by printing out figures for all possible column i vs column j.
>
> for(i in names(data[,-1])){
> for(j in names(data[,-1])){
>
> png(paste(i," vs " ,j,".png", sep=""), width=4, height=6, units="in",
> res=300)
>
> plot(data[,i],data[,j],xlab=i,ylab=j,log="xy",pch=16)
>
>
> dev.off()
> print(c(i,j))
> }
> }
>
> Good luck
> Changrong
>
> On Sun, Jun 5, 2016 at 9:45 PM, Douglas Johnson  wrote:
>
>> I'm guessing this is trivial but I've spent two hours searching and
>> reading
>> FAQ, tutorial, introductory and 'idiom' documents and haven't found a
>> solution. All I want to do is select a data frame column at run time using
>> a command line arg. For example, the "test.csv" file contains:
>>
>> a,b,c
>> 1,2,3
>> 4,5,6
>> 7,8,9
>>
>> If I run "test.r a", I get [1,4,7].
>> If I run "test.r b", I get [2,5,8].
>>
>> Here's sample code -- how I map args[1] to column/vector 'a' or 'b' or
>> 'c'?
>>
>> args <- commandArgs(trailingOnly=T)
>>
>> adl1<-read.csv(file="test.csv",head=T,sep=",")
>>
>> adl1$SOME-MAGIC-FUNCTION-OR-SYMBOL(args[1])
>>
>>
>> All I'm really trying to do is generate a bunch of scatter plots of column
>> 'x' vs. 'y' without writing a separate program for each pair.
>>
>>
>> Thanks,
>>
>>
>> Doug
>>
>>
>>
>> --
>> http://www.dojopico.org 
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
>
>
> --
>
> Changrong Ge, PhD
>
> Division of Medical Inflammation Research
> Department of Medical Biochemistry and Biophysics (MBB)
> Karolinska Institutet
> Scheeles väg 2, B2 Plan 4
> SE-171 77 Stockholm
> Sweden
> Tel:  +46-8-524 86337 ,   Mobile: +46-(0)76-2878 029
> Fax: +46-8-524 87750 ,  Email: changrong...@ki.se 
>  or changrong...@gmail.com
>
>


-- 

Changrong Ge, PhD

Division of Medical Inflammation Research
Department of Medical Biochemistry and Biophysics (MBB)
Karolinska Institutet
Scheeles väg 2, B2 Plan 4
SE-171 77 Stockholm
Sweden
Tel:  +46-8-524 86337 ,   Mobile: +46-(0)76-2878 029
Fax: +46-8-524 87750 ,  Email: changrong...@ki.se  or
changrong...@gmail.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] How to specify a data frame column using command line arg?

2016-06-06 Thread Ista Zahn
Hi Doug,

ggplot lets you map variables to aesthetics in a few different ways,
including passing to variable names as strings to aes_string. See
?aes_string for details.

Best,
Ista
On Jun 6, 2016 1:03 AM, "Jim Lemon"  wrote:

> Yes, I see what you want. I can't run this myself as my work computer
> runs Windows and insists on starting Statistica when I run an R file
> in batch mode. What about:
>
> plot(adl1[,args[1]],adl1[,args[2]])
>
> I just noticed that you are plotting in ggplot, so this won't help. Maybe:
>
> aes(x=args[1], y=args[2])
>
> ?
>
> Jim
>
>
> On Mon, Jun 6, 2016 at 1:29 PM, Douglas Johnson  wrote:
> > Hi Jim,
> >
> > Thanks for the quick reply. I was trying to be clear but I think maybe
> the
> > example was too simple. What I really have is a csv file with lots of
> > columns (let's say 26) with a header (say a,b,c,d,e,z). I want to
> > generate lots of scatter plots -- 'a' vs. 'b' and 'd' vs 'j' and so on
> and
> > so on. I don't want to create a separate program for every combination of
> > columns -- that would be hundreds of programs. I just want one program
> that
> > takes three args --
> > 1) the csv file name
> > 2) the name of the 'x-axis' column (e.g. 'd')
> > 3) the name of the 'y-asix' column (e.g. 'j')
> >
> > so can run:
> >
> > scatter_plot.r sample01.csv a b
> > scatter_plot.r sample01.csv d j
> > .
> >
> > I can't figure out how to get  the column names from the args[] to the
> > aes(x=?, y=?).
> > There must be some kind of indirect  reference or eval() or substitute()
> > operator in R but I can't find it.
> >
> > Anyway, thanks for taking a shot at this.
> >
> > Best,
> >
> > doug
> >
> >
> >
> >
> > On Sun, Jun 5, 2016 at 10:44 PM, Jim Lemon  wrote:
> >>
> >> Hi Doug,
> >> I think this will work for you:
> >>
> >> adl1<-read.csv("test.csv")
> >> adl1[,"a"]
> >> [1] 1 4 7
> >>
> >> so, adl1[,args[1]] should get you the column that you pass as the
> >> first argument.
> >>
> >> Jim
> >>
> >>
> >> On Mon, Jun 6, 2016 at 5:45 AM, Douglas Johnson 
> wrote:
> >> > I'm guessing this is trivial but I've spent two hours searching and
> >> > reading
> >> > FAQ, tutorial, introductory and 'idiom' documents and haven't found a
> >> > solution. All I want to do is select a data frame column at run time
> >> > using
> >> > a command line arg. For example, the "test.csv" file contains:
> >> >
> >> > a,b,c
> >> > 1,2,3
> >> > 4,5,6
> >> > 7,8,9
> >> >
> >> > If I run "test.r a", I get [1,4,7].
> >> > If I run "test.r b", I get [2,5,8].
> >> >
> >> > Here's sample code -- how I map args[1] to column/vector 'a' or 'b' or
> >> > 'c'?
> >> >
> >> > args <- commandArgs(trailingOnly=T)
> >> >
> >> > adl1<-read.csv(file="test.csv",head=T,sep=",")
> >> >
> >> > adl1$SOME-MAGIC-FUNCTION-OR-SYMBOL(args[1])
> >> >
> >> >
> >> > All I'm really trying to do is generate a bunch of scatter plots of
> >> > column
> >> > 'x' vs. 'y' without writing a separate program for each pair.
> >> >
> >> >
> >> > Thanks,
> >> >
> >> >
> >> > Doug
> >> >
> >> >
> >> >
> >> > --
> >> > http://www.dojopico.org 
> >> >
> >> > [[alternative HTML version deleted]]
> >> >
> >> > __
> >> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> > 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.
> >
> >
> >
> >
> > --
> > http://www.dojopico.org
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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] How to specify a data frame column using command line arg?

2016-06-05 Thread Jim Lemon
Yes, I see what you want. I can't run this myself as my work computer
runs Windows and insists on starting Statistica when I run an R file
in batch mode. What about:

plot(adl1[,args[1]],adl1[,args[2]])

I just noticed that you are plotting in ggplot, so this won't help. Maybe:

aes(x=args[1], y=args[2])

?

Jim


On Mon, Jun 6, 2016 at 1:29 PM, Douglas Johnson  wrote:
> Hi Jim,
>
> Thanks for the quick reply. I was trying to be clear but I think maybe the
> example was too simple. What I really have is a csv file with lots of
> columns (let's say 26) with a header (say a,b,c,d,e,z). I want to
> generate lots of scatter plots -- 'a' vs. 'b' and 'd' vs 'j' and so on and
> so on. I don't want to create a separate program for every combination of
> columns -- that would be hundreds of programs. I just want one program that
> takes three args --
> 1) the csv file name
> 2) the name of the 'x-axis' column (e.g. 'd')
> 3) the name of the 'y-asix' column (e.g. 'j')
>
> so can run:
>
> scatter_plot.r sample01.csv a b
> scatter_plot.r sample01.csv d j
> .
>
> I can't figure out how to get  the column names from the args[] to the
> aes(x=?, y=?).
> There must be some kind of indirect  reference or eval() or substitute()
> operator in R but I can't find it.
>
> Anyway, thanks for taking a shot at this.
>
> Best,
>
> doug
>
>
>
>
> On Sun, Jun 5, 2016 at 10:44 PM, Jim Lemon  wrote:
>>
>> Hi Doug,
>> I think this will work for you:
>>
>> adl1<-read.csv("test.csv")
>> adl1[,"a"]
>> [1] 1 4 7
>>
>> so, adl1[,args[1]] should get you the column that you pass as the
>> first argument.
>>
>> Jim
>>
>>
>> On Mon, Jun 6, 2016 at 5:45 AM, Douglas Johnson  wrote:
>> > I'm guessing this is trivial but I've spent two hours searching and
>> > reading
>> > FAQ, tutorial, introductory and 'idiom' documents and haven't found a
>> > solution. All I want to do is select a data frame column at run time
>> > using
>> > a command line arg. For example, the "test.csv" file contains:
>> >
>> > a,b,c
>> > 1,2,3
>> > 4,5,6
>> > 7,8,9
>> >
>> > If I run "test.r a", I get [1,4,7].
>> > If I run "test.r b", I get [2,5,8].
>> >
>> > Here's sample code -- how I map args[1] to column/vector 'a' or 'b' or
>> > 'c'?
>> >
>> > args <- commandArgs(trailingOnly=T)
>> >
>> > adl1<-read.csv(file="test.csv",head=T,sep=",")
>> >
>> > adl1$SOME-MAGIC-FUNCTION-OR-SYMBOL(args[1])
>> >
>> >
>> > All I'm really trying to do is generate a bunch of scatter plots of
>> > column
>> > 'x' vs. 'y' without writing a separate program for each pair.
>> >
>> >
>> > Thanks,
>> >
>> >
>> > Doug
>> >
>> >
>> >
>> > --
>> > http://www.dojopico.org 
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > __
>> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > 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.
>
>
>
>
> --
> http://www.dojopico.org

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] How to specify a data frame column using command line arg?

2016-06-05 Thread Jim Lemon
Hi Doug,
I think this will work for you:

adl1<-read.csv("test.csv")
adl1[,"a"]
[1] 1 4 7

so, adl1[,args[1]] should get you the column that you pass as the
first argument.

Jim


On Mon, Jun 6, 2016 at 5:45 AM, Douglas Johnson  wrote:
> I'm guessing this is trivial but I've spent two hours searching and reading
> FAQ, tutorial, introductory and 'idiom' documents and haven't found a
> solution. All I want to do is select a data frame column at run time using
> a command line arg. For example, the "test.csv" file contains:
>
> a,b,c
> 1,2,3
> 4,5,6
> 7,8,9
>
> If I run "test.r a", I get [1,4,7].
> If I run "test.r b", I get [2,5,8].
>
> Here's sample code -- how I map args[1] to column/vector 'a' or 'b' or 'c'?
>
> args <- commandArgs(trailingOnly=T)
>
> adl1<-read.csv(file="test.csv",head=T,sep=",")
>
> adl1$SOME-MAGIC-FUNCTION-OR-SYMBOL(args[1])
>
>
> All I'm really trying to do is generate a bunch of scatter plots of column
> 'x' vs. 'y' without writing a separate program for each pair.
>
>
> Thanks,
>
>
> Doug
>
>
>
> --
> http://www.dojopico.org 
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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] How to specify a data frame column using command line arg?

2016-06-05 Thread Douglas Johnson
I'm guessing this is trivial but I've spent two hours searching and reading
FAQ, tutorial, introductory and 'idiom' documents and haven't found a
solution. All I want to do is select a data frame column at run time using
a command line arg. For example, the "test.csv" file contains:

a,b,c
1,2,3
4,5,6
7,8,9

If I run "test.r a", I get [1,4,7].
If I run "test.r b", I get [2,5,8].

Here's sample code -- how I map args[1] to column/vector 'a' or 'b' or 'c'?

args <- commandArgs(trailingOnly=T)

adl1<-read.csv(file="test.csv",head=T,sep=",")

adl1$SOME-MAGIC-FUNCTION-OR-SYMBOL(args[1])


All I'm really trying to do is generate a bunch of scatter plots of column
'x' vs. 'y' without writing a separate program for each pair.


Thanks,


Doug



-- 
http://www.dojopico.org 

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.