Saving and Loading your work in R

February 4, 2005 at 1:05 am | In GNU-R | Leave a Comment

save
Use save command to save R objects related to any specific work that you are doing (to distinguish it from other things you or any other user might do in R on the same machine). You can save the environment in a file with extension .Rdata.

load
This can be loaded into R later using “load”.

This would help avoid other people removing your stuff inadvertantly. You can also transport this .Rdata file from one computer to another and load your R environment without having to run all the programs all over again.

For the purpose of the term paper, it will be of use to save your work into an .Rdata file and back it up (e-mail it to yourself?). Then you are not stuck to any particular machine and do not have to worry about somebody tampering with your work.

Vikas

More on “A Little Trick in Reading Data”

February 4, 2005 at 1:03 am | In Data manipulation, GNU-R | Leave a Comment

Here is an example of the beauty of R.

To split up a character string, all you need is to use a function called substr (for sub string)!! So you don’t really need to write the variable into a new file and read it back with read.fwf as I did (see my earlier post titled A Little Trick in Reading Data).

All you need, is to use substr to create a (set of) new variables out of the character strings. And if you want to turn them into numeric variables, you just have to us “as.numeric(substr(…))”.

It is such a joy when you discover that there is something meant to do exactly what you wanted.

Vikas

Subset

February 2, 2005 at 7:53 pm | In Data manipulation, GNU-R | Leave a Comment

The following text from the R help pages clearly explains the use of command subset in extracting data from a data.frame. This command will be of much use in preparation of data set for further analysis.

Vikas

—————

subset {base} R Documentation

Subsetting Vectors and Data Frames

Description

Return subsets of vectors or data frames which meet conditions.
Usage

subset(x, …)

## Default S3 method:
subset(x, subset, …)

## S3 method for class ‘data.frame’:
subset(x, subset, select, drop = FALSE, …)

Arguments
x object to be subsetted.
subset logical expression.
select expression, indicating columns to select from a data frame.
drop passed on to [ indexing operator.
... further arguments to be passed to or from other methods.
Details

For ordinary vectors, the result is simply x[subset & !is.na(subset)].

For data frames, the subset argument works similarly on the rows. Note that subset will be evaluated in the data frame, so columns can be referred to (by name) as variables.

The select argument exists only for the method for data frames. It works by first replacing names in the selection expression with the corresponding column numbers in the data frame and then using the resulting integer vector to index the columns. This allows the use of the standard indexing conventions so that for example ranges of columns can be specified easily.

The drop argument is passed on to the indexing method for data frames.
Value

An object similar to x contain just the selected elements (for a vector), rows and columns (for a data frame), and so on.
Author(s)

Peter Dalgaard
See Also

[, transform
Examples

data(airquality)
subset(airquality, Temp > 80, select = c(Ozone, Temp))
subset(airquality, Day == 1, select = -Temp)
subset(airquality, select = Ozone:Wind)

with(airquality, subset(Ozone, Temp > 80))

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.