Miscellaneous

Date and time functions in R

In library(survival), we can find the following functions:

  • as.date

    Converts any of the following character forms to a Julian date: 8/31/56, 8-31-1956, 31 8 56, 083156, 31Aug56, or August 31 1956.

    Example:

    > as.date(c("1jan1960", "2jan1960", "31mar1960", "30jul1960"))
    [1] 1Jan60  2Jan60  31Mar60 30Jul60
    
  • mdy.date

    Given a month, day, and year, returns the number of days since January 1, 1960.

    Example:

     

    > mdy.date(3, 10, 53)
    [1] 10Mar53
    
  • date.mdy

    Convert a vector of Julian dates to a list of vectors with the corresponding values of month, day and year, and optionally weekday.

    Example:

     

    > a1<-mdy.date(month = 8, day = 7, year = 1960)
    > a1
    [1] 7Aug60
    > date.mdy(a1)
    $month
    [1] 8
    
    $day
    [1] 7
    
    $year
    [1] 1960
    
    > 
    
  • date.mmddyy

    Given a vector of Julian dates, this returns them in the form ``10/11/89'', ``28/7/54'', etc.

    Example:

     

    > date.mmddyy(mdy.date(3, 10, 53))
    [1] "3/10/53"
    
  • date.ddmmmyy

    Given a vector of Julian dates, this returns them in the form ``10Nov89'', ``28Jul54'', etc.

    Example:

     

    > date.ddmmmyy(mdy.date(3, 10, 53))
    [1] "10Mar53"
    
  • date.mmddyyyy

    Given a vector of Julian dates, this returns them in the form ``10/11/1989'', ``28/7/1854'', etc.

    Example:

     

    > date.mmddyyyy(mdy.date(3, 10, 53))
    [1] "3/10/1953"
FAQ Category

How to call C functions in R?

In R, we can call C functions. For example, we have the following toy C function in the file test.c.

 

    /***** 
      The function is to calculate Hadama product of two matrices.
      out[i][j]=x[i][j]*y[i][j].
      The inputs x, y and the output out are vectors, not matrices.
      So in R, you need to transform input matrices into
      vectors and transform output vector back to matrix.
    *****/
    void myHadamaProduct(double *x, double *y, int *nrow, int *ncol, double *out)
    {
      int i, j, r, c;

      r=*nrow;
      c=*ncol;
      for(i = 0; i < r; i ++)
      { for(j = 0; j < c; j ++)
        { out[i*c+j]=x[i*c+j]*y[i*c+j]; }
      }
      return;
    }

 

 

 

 

 

  1. First, we need to compile the file test.c to create a shared library, test.so say, by using the GNU C compiler:

     

        gcc -fpic -shared -fno-gnu-linker -o test.so test.c
    
  2. Next, we need to use the R function dyn.load to load the shared library test.so.

        if(!is.loaded("myHadamaProduct")){ dyn.load("./test.so") }
    

    The R function is.loaded is to check if the C function myHadamaProduct is already be loaded to R. If yes, then we do not need to loaded it again.

  3. Next, we use the R function .C to call the C function myHadamaProduct. For example,

        x<-matrix(1:10,nrow=5, ncol=2) # get a 5x2 matrix
        y<-matrix(1:10,nrow=5, ncol=2) # get a 5x2 matrix
    
        # In R, a matrix is stored by column in the memory.
        # However, in C, a matrix is stored by row in the memory.
        # So we need to transpose the matrix x, namely t(x), before 
        # transforming it to a vector.
        xx<-as.double(as.vector(t(x)))
        yy<-as.double(as.vector(t(y)))
        nr<-as.integer(nrow(x))
        nc<-as.integer(ncol(x))
        res<-.C("myHadamaProduct", xx, yy, nr, nc, out=as.double(rep(0.0, n)))
    
        # In C, matrix is stored by row. So when transforming back, we need to
        # specify byrow=T.
        mat<-matrix(res$out, ncol=nc, byrow=T)
    
        cat("Hadama product >>n")
        print(mat)
    
  4. If you do not need to use the shared library test.so any more, you can use the R function dyn.unload to unload it.

        if(is.loaded("myHadamaProduct")){ dyn.unload("./test.so") }
    

Note:

 

 

  • The C function called by R must be void type. For the example above, the function myHadamaProduct has to have the form:

        void myHadamaProduct(double *x, double *y, int *nrow, int *ncol, double *out)
    

    rather than

        double *myHadamaProduct(double *x, double *y, int *nrow, int *ncol)
    

    You have to let the function return values through arguments, e.g "double *out" in the above example. In fact if the arguments are pointers (e.g. *out) and you change their values they refer to within the function, then the values where the pointers refer to will be changed after calling this function.

     

  • All arguments in the C function have to be passed by addresses instead of values. That is, all arguments have to be pointers. For the example above, you cannot change "int *nrow" to "int nrow".

    The values where the pointers refer to will be changed after calling the function, if the values are changed within the function. So be careful when using pointers as function arguments.

     

  • Any values returned by C functions which are called in R must be initialized and must have the format:

        # if the variable is defined as double in the C function
        variablename=as.double(initialized values)
        # if the variable is defined as integer in the C function
        variablename=as.integer(initialized values)
    

    in the ".C" function (e.g. "out=as.double(rep(0.0, n))" in the above example).

    The input values must also be initialized and must have the above format. However, they can be formated before the ".C" function (e.g. "nr<-as.integer(nrow(x))" in the above example).

     

  • If the output is not written as variablename=variablename format (e.g. out=as.double(rep(0.0, n)) in the above example), You still can get results. However, you have to use res[[5]] to refer out in the above example. In fact, the .C function return a list containing all arguments of the C function myHadamaProduct. Since out is the 5-th argument, you can use res[[5]] to refer to the 5-th elements of the list.
  • It is okay that the file test.c contains the main function.
  • Sometimes, the command "dyn.load("test.so")" gets error message. This is probably caused by the environment variable "$PATH" was not set correctly. You can either add the following line to the file ".bashrc" in your home directory:

        export PATH=$PATH:.:
    

    or use the command

        dyn.load("./test.so")
FAQ Category

How do I install a package in R/Splus in UNIX?

Below are some instructions for installing an R package into your own disk space (so that you don't need to ask the busy systems manager).

To install a package (in Unix) which is not part of the main distribution of R, follow the steps below.

  • go to http://www.r-project.org or a local mirror site http://cran.stat.sfu.ca and get the source as a gzipped tar file, an example is the package sspir_0.2.3.tar.gz for State Space Models in R
  • extract from the tar file with (say under your ~/tmp directory)

        mkdir ~/tmp  # if directory doesn't already exist
        mv ~/sspir_0.2.3.tar.gz ~/tmp
        cd ~/tmp
        tar xzvf sspir_0.2.3.tar.gz
    
  • compile the source (typically C and/or fortran routines) and install into your local directory (say ~/Rlib) with the following Unix command line (where you replace $HOME with your home directory which is the output of 'echo $HOME').

        R CMD INSTALL --library=$HOME/Rlib sspir
    

    To use the package, you need the lib.loc option for library()

        > library(sspir,lib.loc="$HOME/Rlib")
        > library(help=sspir,lib.loc="$HOME/Rlib")
    
  • With newer versions of R, the above can be combined into one step

         R CMD INSTALL --library=$HOME/Rlib sspir_0.2.3.tar.gz
    
  • If you decide later you don't want the package, then from the Unix command line

        R CMD REMOVE --library=$HOME/Rlib sspir
  • If you want to let others use your locally added packages, just set permissions appropriately to $HOME/Rlib, e.g.

       chmod -R og+rX $HOME/Rlib
    
  • Note that the different flavors of Unix on our network. A package compiled in one computer (e.g. 32-bit Linux) should work on another computer with the same architecture. If you work on different servers on our network, then you would have to compile separate versions. In this case, one possibility is something like subdirectory Rlib_linux32, Rlib_linux64, Rlib_solaris64 under your $HOME
  • Alternatively, after testing the package, you can ask the systems manager to install it (be clear whether you want the Solaris or Linux version) by providing the following instructions (where you replace $MYRPKGTARDIR with the directory where the tar file is unpacked, and replace $PKG with the name of the package)

      cd $MYRPKGTARDIR
      R CMD INSTALL $PKG
    

As a final note, if you want to install a package in Windows, the simplest thing to do is:

  • go to http://www.r-project.org/ and get the zip file with the Windows compiled version
  • unzip the file under $RHOME/library, where $RHOME is the folder where you installed R
  • if you don't have write access to $RHOME, just unzip the package anywhere and use library() with lib.loc argument to load the package later
  • Alternatively, use the installer in the R console menu.
FAQ Category

How to read data files containing time data by R?

For example, the data file test.dat is:

4188418000628;       1  ;    05-19-2002  ;    06-23-2002  ;     26.6;   3.71;   3.03;  0
4188418000628;       1  ;    05-19-2002  ;    07-15-2002  ;     28.1;   3.41;   2.79;  0
4188418000628;       1  ;    05-19-2002  ;    08-15-2002  ;     32.0;   3.43;   3.30;  0

The field is separated by ";" and the third and fourth columns are time data.

 

  1. Read time data as characters.

    y<-read.table("test.dat", sep=";", as.is=T, strip.white=T)
    

    and we can get

    > y$V3
    [1] "05-19-2002" "05-19-2002" "05-19-2002"
    

    The argument strip.white is used only when sep has been specified, and allows the stripping of leading and trailing white space from `character' fields (`numeric' fields are always stripped). If strip.white=F, then we will get

    > y1<-read.table("test.dat", sep=";", as.is=T)
    > y1$V3
    [1] "    05-19-2002  " "    05-19-2002  " "    05-19-2002  "
    

    The default behavior of read.table is to convert character variables (which are not converted to logical, numeric or complex) to factors. When as.is=T, read.table will not convert character variables to factors.

     

  2. Convert characters to a Julian date by using the function as.date in library(survival)

    library(survival)
    > a<-as.date(y$V3)
    > a
    [1] 19May2002 19May2002 19May2002
    > b
    [1] 23Jun2002 15Jul2002 15Aug2002
    > a[1]-b[1]
    [1] -35
FAQ Category

How to format output of R?

You can use R command format. For example

    round(0.10000, 3)

produces

0.1

instead of

0.100

. To get correct format, we can use the following command

    format(round(0.10000,3), nsmall=3, digits=3)

Note that the return value of the above command is a character string "0.100", not a numerical value.

FAQ Category

How do I get Firefox to run an external application on a particular file type?

A helper application is a program external to the browser that will open its own window or terminal to display results. A helper application is not the same ias a plugin. A plugin is loaded by the browser process data internally and render the results within the browser window (e.g. Acrobaty Reader plugin for PDF files).

This FAQ does not cover the installation of plugins; you have to go elsewhere for that.

If you want Firefox to autmatically use an external helper program to process files of a particular type (characterized by a filename extension or MIME type) whenever you click on a link to that file, these are the steps you can perform.

Manual Method

  • Step 1 (optional): backup your existing Firefox MIME database in case you screw it up. You can also delete the file and let Firefox rebuild it if things go wrong.

    	cd ~/.mozilla/firefox/user-profile
    	cp mimeTypes.rdf mimeTypes.rdf~
    	# Now you can edit this file ...
    	vi mimeTypes.rdf 
  • Step 2: add the MIME type to the list of helpers Firefox should handle. If the <RDF:Seq ... </RDF:Seq> tag already exists, only add the middle line.

    	<RDF:Seq RDF:about="urn:mimetypes:root">
    	  ...
    	  <RDF:li RDF:resource="urn:mimetype:application/testapp"/>
    	</RDF:Seq> 
  • Step 3: associate file extensions with this MIME type:

    	<RDF:Description RDF:about="urn:mimetype:application/testapp"
    	  NC:editable="true"
    	  NC:value="application/testapp"
    	  NC:description="Description of MIME type">
    	  <NC:fileExtensions>ext1</NC:fileExtensions>
    	  <NC:fileExtensions>ext2</NC:fileExtensions>
    	  ...
    	  <NC:handlerProp RDF:resource=
    	    "urn:mimetype:handler:application/testapp"/>
    	</RDF:Description> 
  • Step 4: inform Firefox this MIME type is automatically handled by an external application.

    	<RDF:Description RDF:about="urn:mimetype:handler:application/testapp"
    	  NC:alwaysAsk="false"
    	  NC:useSystemDefault="false"
    	  NC:saveToDisk="false">
    	  <NC:externalApplication RDF:resource=
    	    "urn:mimetype:externalApplication:application/testapp"/>
    	</RDF:Description> 
  • Step 5: inform Firefox which external application to use:

    	<RDF:Description
    	  RDF:about="urn:mimetype:externalApplication:application/testapp"
    	  NC:path="executable" /> 

For steps 2 and onward, snippets should be placed before the final line

	</RDF:RDF> 

You should replace the example values above with your particular values. For example, to view postscript files using ghostview (gv), you would replace

  • application/testapp with application/postscript.
  • ext1 with ps. Optionally, you could also replace ext1 with eps for excapsulated postscript, or you can leave that out.
  • executable with gv or /usr/local/bin/gv.
  • Description of MIME type with PostScript file.

Plugin Method

A much easier way, if you have the ability to install Firefox plugins, may be to install the MimeEdit plugin, which gives the user the ability to associate MIME types with application that can operate on them using a GUI. It can be found here

https://addons.mozilla.org/en-US/firefox/addon/4498

FAQ Category

How do I handle complaints from Firefox/Netscape that another browser is running?

A browser will usually check to see if another instance of itself is running, and will refuse to start (or use your regular profile) if it believes that this is the case. It does this to ensure consistency of its database (bookmarks, browsing history, etc.) which it can't guarantee if another browser is modifying the same data it is reading from or writing to.

A browser will check that it is not stepping on its own toes by checking the existence of a lock file. A lock file is created whenever a browser starts to run to warn other potential instances of itself not to start running.

However, problems may arise if the original instance of the browser crashes or ends abnormally, and does not get the chance to remove the lock file. The lock file is located at

     Firefox: ~/.mozilla/firefox/<profile-directory>/lock      Netscape: ~/.netscape/lock 

These file are symbolic links (not regular files) that will point to the host and process-id that created the lock file. You can glean this information by doing

     ls -l <lockfile>          Example output:         lrwxrwxrwx 1 smith wesson  18 Jan 12 15:54 lock -> 12.34.56.78:+21321 

If you cannot run a browser because of this exclusivity constraint, first make sure that there really isn't another instance running on another terminal or hiding on your desktop. You can also check to see that it really doesn't exist by logging on to the host the lock file claims the browser it is running on and doing

        ssh 12.34.56.78 -l smith        ps -p 21321 

If this process exists, you can either find where it is displaying to and stop it, or you can kill it by doing

       kill 21321 

If the process does nto exist, or the lock file is still there, you can now remove it:

        rm <lockfile> 

Try starting your browser now. If you still can't start it up, Email the IT staff.

FAQ Category

How to change the frame style of the slides in the "seminar" class?

A1) Method 1:
      a) first include "usepackage{fancybox}"
      b) then use
           slideframe{shadow} or
           slideframe{double} or
           slideframe{oval} or
           slideframe{Oval}
         to change frame style or use
           slideframe{none}
         to create slides without frame
     note: put these commands in the preamble will cause all slides have 
the same frame unless you use another slideframe command to change it.
The above commands can be used within any begin{slide}...end{slide}.

  Method 2:
    a) first include "usepackage{semcolor}"
    b) then use
      slideframe{scplain} or
      slideframe{scshadow} or
      slideframe{scdouble} or
      slideframe{none}
FAQ Category