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

Jim Lemon drjimlemon at gmail.com
Mon Jun 6 04:44:26 CEST 2016


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 <todojo at gmail.com> 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 at 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.



More information about the R-help mailing list