[R] How can I write routines and scripts in the R environmen

(Ted Harding) Ted.Harding at manchester.ac.uk
Mon Sep 17 21:09:02 CEST 2007


On 17-Sep-07 18:11:26, Maura E Monville wrote:
> I would like to plot some semi-periodic signals as a function
> of the phase expressed as a value in [0,2PI]
> The problem is that the phase data is reported as the residual
> of the division by 2PI.
> For instance if the phase is 10.359 rd  then:
> 15.359 / 6.283185  = 2*6.283185 + 2.79263
> then only the residual 2.79263 is stored. 
> In order to plot a number of consecutive cycles I have to add
> the correct multiple of 2PI  to each phase valu.
> The end/beginning of  anew cycle is marked in another field,
> say phase-mark, containing the letter "Z".
> My goal is to write a small routine that does that for each
> signal file.
> If I had to do that in C language I'd write something like:
> 
> num-cycles = 0;
> for (i=0,i<num-records; i++) {
>    if ( my.data.frame$phase_mark[i] =="Z" )  {
>         my.data.frame$phase[i]  += num-cycles*2PI;
>         num-cycles++;
>    }
> }
> 
> Questions:
> 
> 1. How can I write a small routine like the one above to operate
> on certain columns of an entire data.frame ?
> 
One technique for computing what you want is on the following
lines. You don't say what is in your variable "phase_mark"
(which I'll call "phasemark" to get rid of the "_"), but
I'll assume that it is NA unless there is a "Z" there.
In that case, consider the following as a hint:

phasemark<-c("Z",NA,NA,"Z",NA,NA,NA,NA,"Z",NA,NA,"Z")

1*((!is.na(phasemark)&(phasemark=="Z")))
## [1] 1 0 0 1 0 0 0 0 1 0 0 1

cumsum((!is.na(phasemark)&(phasemark=="Z"))) - 1
## [1] 0 0 0 1 1 1 1 1 2 2 2 3

(i.e. the first "Z" marks the first cycle, so you add 0 multiples
of 2*pi).

So now if you create

phaseno <- cumsum((!is.na(phasemark)&(phasemark=="Z"))) - 1

you can simply add 2*pi*phaseno to your variable phase.

A for "operating on certain columns", you have basically
done this already by using the selector "$", which extracts the
named column. You can equally well assign to it, e.g.

> 2. How can I write a parametric R script (like a shell script
> in Linux) that reads a CSV file into a new R data.frame?
> The commands I now issue manually are as follows:
> 
> read.csv("file-name",header=T,skip=9)
> file-name$PatientID = paste(rep("file-name",length(file-name[,1])))
> names(file-names)= c("Amplitude","Phase", ....)

DON'T use a name like "file-name" for an object in R!! R will read
it as though you are trying to subtract something from a variable
called "file".

Your first line should be like

Data <- read.csv("file-name",header=T,skip=9)

Your remaining commands are difficult to interpret without
more explicit detail of how the data are stored in the file
"file-name", so I can't relate my suggested computation above
to what you've written.

However, suppose that the dataframe Data as read in above
has columns Amplitude, Phase, PhaseMark (NOT Phase-Mark!!)
then you could create

Data$Phase <- Data$Phase +
2*pi*(cumsum((!is.na(Data$PhaseMark)&(Data$PhaseMark=="Z"))) - 1)

which would over-write the original Data$Phase, or you can
augment Data with a new column Data$PhasePlus by using

Data$PhasePlus <- Data$Phase +
2*pi*(cumsum((!is.na(Data$PhaseMark)&(Data$PhaseMark=="Z"))) - 1)

which will leave you original Data$Phase untouched, and add
an extra column Data$PhasePlus.

As for writing a "parametric R script", you could define
a function like

read.my.file <- function(filename){
  Data <- read.csv(file=filename,header=T,skip=9)
  Data$PhasePlus <- Data$Phase +
  2*pi*
  (cumsum((!is.na(Data$PhaseMark)&(Data$PhaseMark=="Z")))
   - 1)
  [anything else you want to do at read-in time]
  Data
}

(or you could include header and skip as parameters as well)
and then call it with

MyNewData <- read.my.file("my-new-file")


Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 17-Sep-07                                       Time: 20:08:47
------------------------------ XFMail ------------------------------



More information about the R-help mailing list