R fundamental 1

1.1 R Environment

It is doable to run on the systems like Windows、 Linux and Unix. The most convience way to manipulate R is through graphies interface.

1.2 Interactively

All command work through a prompt and default prompt is '>', which could be the same either on Linux or Windows shell prompt.

1.3 Getting help

R has a build-in help facility. The command is

>help(object)

An alternative is

>?object

If a command is not finished at the end of a line, R will give a default symbol '+', and it will read the input from second line or subsequent lines until command is done.

1. 4 Numbers and Vectors

R operates on named data structures. The very simplest one is numeric vector.The keyword is 'c' and assignment operator '<-'. For example, to set up a vector named x, concludes total 3 numbers, namely 1, 2, 3, the command is

> x <- c(1, 2, 3)

or in the oppsite direction

> c(1, 2, 3) -> x

Assignment can also be done using by function 'assign()'.

> assign("x", c(1, 2, 3))

1.5 Arithmetic

Just use arithmetic expressions can fulfill the calculation needs. The elementary arithmetic operators are the usual '+, -, *, /, ^'. Additional operators includes 'min, max, length, sum, log, exp, sin, cos, tan, sqrt...'. Symbol "i" for the complex numbers.

1.6 Sequences

The function seq() is one way to generate sequences. For instance, the command is

>seq(1, 3) , is the same vector as > x <- c(1, 2, 3)

the more efficient way is use colon operator, the command is

> x <- c(1:3)

seq() has multiple parameters, seq(from=value, to=value, by=value, length=value, along=vector)

function rep() is related to seq(), but works in a replicating way. Simple form is

> x <- rep(1, each = 5)

1.7 Logical

The logical value TRUE, FALSE, and NA are quite as familiar as the logical operators <, <=, >, >=, ==, !=. Furthermore the logical expressions are &, |, !. TRUE regarded as 1 and FALSE as 0 in default situation.

1.8 Character Vectors

Character strings are entered using either matching double " or single ' quotes, \ as the escape character

The paste() function takes an arbitary number of arguments and concatenates them one by one into character strings. The command is

> e <- paste(c("a","b"), 1:6, sep=""), which makes e as c("a1", "b2", "a3", "b4", "a5", "b6")

1.9 Index Vectors

If the index vector is positive, it must lie in the set {1, 2, ...., length(x)} like > x[6]

if the index vector is below number zero, such an index vector specifies the value to be excluded. As

> y <- x[-(1:3)], means y got all but the first three elements of x.

If the index vector is strings which could be the attribute of an object, it could be used in the same way as the positive.

1.10 Missing Values

In general, a missing value would be assigned as special value "NA". Any operation on an NA becomes an NA commonly.

There is a special missing value called NaN, meaning Not a Number.

In summary, "is.na(x)" is TRUE both for NA and NaN values, though "is.nan(x)" is only TRUE for NaNs.

你可能感兴趣的:(R fundamental 1)