Recommended R functions: Difference between revisions

From Opasnet
Jump to navigation Jump to search
mNo edit summary
(table created from subtitles)
Line 11: Line 11:
==Answer==
==Answer==


===data.frame===
{| class="wikitable sortable" {{prettytable}}
|+ '''Recommended functions and operations.
|----
! What to do || Functions to use routinely|| Functions to avoid except in special cases|| Examples and description
|----
| Manipulate data || data.frame || array ||
|----
| Draw raphs || ggplot, plot || || ggplot requires library(ggplot2)
|----
| Summarise data along a criterion || tapply || ||
|----
| Join two data.frames || merge || IntArray ||
|----
| Add rows to a data.frame || rbind || ||
|----
| Add columns to a data.frame || cbind || ||
|----
| Transform a table from long to wide or vice versa || reshape || ||
|----
| Get data from [[Opasnet Base]] || op_baseGetData || || Requires library(OpasnetBaseUtils) <br>> op_baseGetData("opasnet_base", "Op_en4523")[, -c(1,2)] # Gets the object with identifier Op_en4523 and slices columns 1 and 2 (id, obs) away.
|----
| Get index values from [[Opasnet Base]] || op_baseGetLocs || || Requires library(OpasnetBaseUtils)
|----
| Write data to [[Opasnet Base]] || op_baseWrite || || Requires library(OpasnetBaseUtils). Only works from a THL computer, not [[R-tools]]
|----
| Slicing R objects || data[rows, cols], data$col || || This is not a function but rather a list of practical ways of slicing an object.
|----
| || match  || ||
|----
| ||ifelse  || ||
|----
| ||is.na  || ||
|----
| ||colnames  || ||
|----
| Convert between data types || as.numeric, as.character, as.factor || ||
|----
| Convert between object types || as.data.frame, as.table || ||
|----
|}


===ggplot2===
===tapply===
===merge===
===rbind===
===cbind===
===reshape===
===op_baseGetData===
===op_baseGetLocs===
===op_baseWrite===
===slicing R objects ===
This is not a function but rather a list of practical ways of slicing an object.
===match ===
===ifelse ===
===is.na ===
===colnames ===
===as.numeric ===
===as.character ===
===as.data.frame ===
===as.table ===


==Rationale==
==Rationale==

Revision as of 22:01, 27 December 2011


Recommended R functions describes good practices for writing R code. The code should be short, straightforward to understand, efficient to run, and similar to everyone else's code.

Question

What are good practices for writing R code? The code should be short, straightforward to understand, efficient to run, and similar to everyone else's code.

Answer

Recommended functions and operations.
What to do Functions to use routinely Functions to avoid except in special cases Examples and description
Manipulate data data.frame array
Draw raphs ggplot, plot ggplot requires library(ggplot2)
Summarise data along a criterion tapply
Join two data.frames merge IntArray
Add rows to a data.frame rbind
Add columns to a data.frame cbind
Transform a table from long to wide or vice versa reshape
Get data from Opasnet Base op_baseGetData Requires library(OpasnetBaseUtils)
> op_baseGetData("opasnet_base", "Op_en4523")[, -c(1,2)] # Gets the object with identifier Op_en4523 and slices columns 1 and 2 (id, obs) away.
Get index values from Opasnet Base op_baseGetLocs Requires library(OpasnetBaseUtils)
Write data to Opasnet Base op_baseWrite Requires library(OpasnetBaseUtils). Only works from a THL computer, not R-tools
Slicing R objects data[rows, cols], data$col This is not a function but rather a list of practical ways of slicing an object.
match
ifelse
is.na
colnames
Convert between data types as.numeric, as.character, as.factor
Convert between object types as.data.frame, as.table


Rationale

Based on experience and testing.

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>