[R] data.frame and commandArgs problem

scottyjock scott.smith2 at nhs.net
Tue Oct 16 13:01:33 CEST 2012


Hi,

I am writing a .R script to plot some data from a file, all info given from
command line.

For example: 

Rscript Rtest.R sas_summary.csv sas_score_pre sas_score_post

...will try to plot columns of data "sas_score_pre" and "sas_score_post"
from the file "sas_summary.csv".

I can extract these using :

args <- commandArgs(TRUE)

data<-data.frame(read.csv(args[1]))
attach(data)
pre <- args[2]
post <- args[3]

I then want to do some arithmetic on the columns for plotting:

dat=data.frame(pre,post)
dat$diff <- dat$pre - dat$post
dat$mean <-  ( dat$pre + dat$post)/2

the problem is they are of class "character":

pre
post
class(pre)
class(post)

...gives

[1] "sas_score_pre"
[1] "character"
[1] "myo_score_post"
[1] "character"

So then the following code:

dat=data.frame(pre,post)
dat
dat$diff <- dat$pre - dat$post

...gives the error:

               pre           post
1 sas_stress_score myo_isch_score
Warning message:
In Ops.factor(dat$pre, dat$post) : - not meaningful for factors
Warning message:
In Ops.factor(dat$pre, dat$post) : + not meaningful for factors
Error: Discrete value supplied to continuous scale
Execution halted

How can I make the data  frame read the column headers from the input and
associate it with the columns in "data"?

Hope this makes some sense?!?
thanks
Scott



--
View this message in context: http://r.789695.n4.nabble.com/data-frame-and-commandArgs-problem-tp4646326.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list