Top 20 commands for R-statistical software for new users

Top 20 commands for R-statistical software.

What is R?

·        R is a programming language.

·        A program that can do cool statistics.

·        Modeled after S and S-Plus.

·        R project was started by Robert Gentleman and Ross Ihaka of the statistics Department of the University of Auckland

Top 10 Tips for the new R learners:

1. Use help() or help.start() if you have any questions! Or even use ? median.
Change median to whatever you are looking for.

2. Lines that begin with something other than the > symbol represent the output from R

> x<-2 # input

> x   #input

[1] 2 #output

3. To load a file in Windows with a R –command use: > load(file.choose())

4. The R program is built from a series of modules, called packages. Use search() to look for a list of packages installed.

Ø  search()

·
[1] “.GlobalEnv”        “package:grid”
“package:stats”

·
[4] “package:graphics”  “package:grDevices”
“package:utils”

·
[7] “package:datasets”  “package:methods”

·
[10] “package:base”

5. Several other packages are ready-installed but not automatically loaded. Use installed.packages() to look for the complete list:

6. To load packages as required. Start by issuing the following command: library(package)

7. To Install a library (package) of commands from the CRAN website type: install.packages(“pkg”)

8. If you enjoy PYTHON and used PYTHON as a calculator, you already
know how to use R as a calculator too!

A.    > 5*5

B.
[1] 25

C.
> 5-5

D.    [1] 0

E.
> 5/5

F.
[1] 1

G.    > 5/0

H.    [1] Inf

I.
> 0/5

J.
[1] 0

K.    > pi # The value of pi (?), which is
approximately 3.142.

L.
[1] 3.141593

M.   > x=15

N.    > sqrt(x) # The square root of x.

O.    [1] 3.872983

P.
> factorial(x) # The factorial of x.

Q.    [1] 1.307674e+12

R.
> log(x, base = 10) # The logarithm of x
using base = n (natural log if none specified).

S.
[1] 1.176091

T.
> log(x, base = 2)

U.    [1] 3.906891

9. To store result, use variable

A.    > variable1=sin(45 * pi / 180)

B.
> variable1

C.
[1] 0.7071068sd

10.  Creating sample data:

A.    Use
the c()command.
c(item.1,
item.2, item.3, item.n)

B.
use
scan()

>
data=scan()

>1: 12

>2: 12

>3: 22

>4: 34

>5: 1
22 44 12 44

>10:

items

> data
#prints the data

[1] 12 12
22 34  1 22 44 12 44

C.    To enter character using scan()use: scan(what = ‘character’)

D.    To read a file with a scan() command use: scan(file = ‘test data.txt’) or do > scan(file =
file.choose())

11.  To find the default directory use: getwd()

12.  To set a default directory use: setwd()

13.  To see the directory and see which files/folders are within it, use dir() or list.dir()

14.  To read data from a CSV file use

15.  To view previously loaded named objects, use ls()

16.  To remove objects from R, use: rm(list) or remove(list). Note: R
doesn’t give you a warning before it removes the data.

17.  When R reads the data from the data file it assumes that the text column corresponds to the numeric column and sets the text to a factor rather than as a character. If you want the text to be a character, use: d=read.csv(file.choose(),header=T,sep=”,”, as.is=2)

18.  To find the type of an object, use: class()

19.  To view previous command history, use: history(max.show=100)

20. to save data use: write(data2,file=”data2.txt”,sep=”,”)

Example of mean and trimmed mean in R:

> x=c(0:50,50)

> x

[1]  0  1  2  3  4  5  6
7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

[26] 25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

[51] 50 50

> mx=mean(x)

> mx

[1] 25.48077

>
c(mx,mean(x,trim=.10))

[1] 25.48077 25.50000

> mx2=mean(x,trim=0.10)

> mx2

[1] 25.5