You are on page 1of 16

chapter 2 – Starting Out : Becoming Familiar with “R”

> 23+23

[1] 46

> 45-15

[1] 30

> 15*5

[1] 75

> 2*3-4+5/6

[1] 2.833333

> 2**3

[1] 8

> 2^3

[1] 8

> c(2,3,5,7)^c(2,3)

[1] 4 27 25 343

> c(2,3,5,7)^c(2,3,4)

[1] 4 27 625 49

Warning message:

In c(2, 3, 5, 7)^c(2, 3, 4) :

longer object length is not a multiple of shorter object length

> C(2,3,5,7)*3

Error in C(2, 3, 5, 7) : object not interpretable as a factor

> c(2,3,5,7)+10

[1] 12 13 15 17

> c(2,3,5,7)*3
[1] 6 9 15 21

> c(2,3,5,7)+c(8,9,10)

[1] 10 12 15 15

Warning message:

In c(2, 3, 5, 7) + c(8, 9, 10) :

longer object length is not a multiple of shorter object length

> c(2,3,5,7)%/%c(2,3)

[1] 1 1 2 2

> c(2,3,5,7)%%2

[1] 0 1 1 1

> max(1.2,3.4,-7.8)

[1] 3.4

> min(c(1.2,3.5,-7.8))

[1] -7.8

> pi*2^3-sqrt(4)

[1] 23.13274

> abs(12-17*2/3-9)

[1] 8.333333

> factorial(4)

[1] 24

> log(2,10)

[1] 0.30103

> log(2)

[1] 0.6931472

> log10(2)
[1] 0.30103

> exp(0.6931472)

[1] 2

> 10^0.30103

[1] 2

> sin(45)

[1] 0.8509035

> sin(45*pi/180)

[1] 0.7071068

> asin(0.7071068)*180/pi

[1] 45

> ans1+23+14/2-18+(7*pi/2)

Error: object 'ans1' not found

> ans1=23+14/2-18+(7*pi/2)

> ans1

[1] 22.99557

> ans2=13+11+(17-4/7)

> ans1+ans2

[1] 63.42415

> ans1+ans2/2

[1] 43.20986

> ans3=ans2+9-2+pi

> ans3

[1] 50.57016

> ans4-3+5
Error: object 'ans4' not found

> ans4 - 3+5

Error: object 'ans4' not found

> ans4=3+5

> ans4

[1] 8

> ans5=ans1*ans2

> ans5

[1] 929.6782

> data1= c(3,5,7,5,3,2,6,8,5,6,9)

> data1

[1] 3 5 7 5 3 2 6 8 5 6 9

> data2=c(data1,4,5,7,3,4)

> data2

[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4

> day1=c("mon","tue","wed","thu")

> day1

[1] "mon" "tue" "wed" "thu"

> day1=c(day1,"fri")

> day1

[1] "mon" "tue" "wed" "thu" "fri"

> mix=c(data1 ,day1)

> mix

[1] "3" "5" "7" "5" "3" "2" "6" "8" "5" "6" "9" "mon" "tue" "wed" "thu" "fri"

> data3=scan()
1: 6 7 8 7 6 3 8 9 10 7

11: 6 9

13:

Read 12 items

> data3

[1] 6 7 8 7 6 3 8 9 10 7 6 9

> data2= scan(what='charactewr')

1: Mon Tue Wed

4: Thur

5:

Read 4 items

> data2

[1] "Mon" "Tue" "Wed" "Thur"

> data4 = scan (sep=",")

1: 23,17,12.5,11,17,12,14.5,9

9: 11,9,12,5,14.5,17,8,21

17:

Read 16 items

> data4

[1] 23.0 17.0 12.5 11.0 17.0 12.0 14.5 9.0 11.0 9.0 12.0 5.0 14.5 17.0 8.0 21.0

> data5 = scan(sep=",",what='character')

1: jan,feb,mar

4:

Read 3 items
> data5

[1] "jan" "feb" "mar"

> data5 = scan(sep=",",what='character')

1: jan,feb,mar,apr,may,jun

7: jul,aug,sep,oct,nov,dec

13:

Read 12 items

> data5

[1] "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec"

> data6=scan (file='book1')

Error in file(file, "r") : cannot open the connection

In addition: Warning message:

In file(file, "r") : cannot open file 'book1': No such file or directory

> getwd()

[1] "C:/Users/HP/Documents"

> data6 =scan(file="book1.csv")

Error in scan(file = "book1.csv") :

scan() expected 'a real', got '23,17,12,5,11,17,12,14,,9'

[1] "C:/Users/HP/Documents"

> dir()

[1] "Book1.csv" "Book2.csv" "Corel User Files" "Deeksha R" "Default.rdp"


"desktop.ini"

[7] "My Music" "My Pictures" "My Videos" "rajeec singapore.docx"

> list.files()
[1] "Book1.csv" "Book2.csv" "Corel User Files" "Deeksha R" "Default.rdp"
"desktop.ini"

[7] "My Music" "My Pictures" "My Videos" "rajeec singapore.docx"

> data7=scan(file.choose())

Error in scan(file.choose()) :

scan() expected 'a real', got '"23,17,12.5,11,17,12,14.5,9,11,9,12.5,14.5,17.0,8,21"'

>

> data7

Error: object 'data7' not found

> data8=scan(file.choose(),what='char',sep=',')

Read 1 item

> data8

[1] "jan,feb,mar,apr,may,jun,jul,aug,"

> data8=scan(file.choose(),what='char',sep=',')

Read 1 item

> data8

[1] "jan,feb,mar,apr,may,jun,jul,aug,"

> fw

Error: object 'fw' not found

> fw=read.csv(file.choose())

> fw

ABUND FLOW

1 9 2

2 25 3

3 15 5

4 2 9
5 14 14

6 25 24

7 24 29

8 47 34

> my.ssv=read.table(file.choose(),header=TRUE)

> my.ssv

data1.data2.data3

1 1,2,4

2 4,5,3

3 3,4,5

4 3,6,6

5 4,5,9

> my.ssv=read.csv(file.choose(),sep=" ")

> my.ssv

data1.data2.data3

1 1,2,4

2 4,5,3

3 3,4,5

4 3,6,6

5 4,5,9

> my.tsv=read.delim(file.choose())

> my.tsv

data1.data2.data3

1 1,2,4

2 4,5,3
3 3,4,5

4 3,6,6

5 4,5,9

> my.ssv=read.csv(file.choose(),sep=" ")

Error in read.table(file = file, header = header, sep = sep, quote = quote, :

more columns than column names

> my.ssv

data1.data2.data3

1 1,2,4

2 4,5,3

3 3,4,5

4 3,6,6

5 4,5,9

> my.tsv=read.table(file.choose(),header=TRUE,sep="\t")

> my.tsv

data1.data2.data3

1 1 2 4 ,,

2 4 5 3 ,,

3 3 4 5,,

4 3 6 6,,

5 4 5 9,,

> grass=read.csv(file.choose())

> grass

mow unmow

1 12 8
2 15 9

3 17 7

4 11 9

5 15 NA

> answer1=23+17/2-pi/4

> answer1

[1] 30.7146

> ls()

[1] "ans1" "ans2" "ans3" "ans4" "ans5" "answer1" "data1" "data2" "data3" "data4" "data5"
"data8" "day1" "fw" "FW"

[16] "grass" "mix" "my.ssv" "my.tsv"

> ls(pattern="b")

character(0)

> ls(pattern="d")

[1] "data1" "data2" "data3" "data4" "data5" "data8" "day1"

> ls(pattern="^a"

+;

Error: unexpected ';' in:

"

;"

> ls {pattern="^a")

Error: unexpected '{' in "ls {"

> ls (pattern="^a")

[1] "ans1" "ans2" "ans3" "ans4" "ans5" "answer1"

> ls(pattern="a$")
character(0)

> ls(pattern="a.e")

character(0)

> ls(pattern="d.t")

[1] "data1" "data2" "data3" "data4" "data5" "data8"

> rm(answer1)

> rm

function (..., list = character(), pos = -1, envir = as.environment(pos),

inherits = FALSE)

dots <- match.call(expand.dots = FALSE)$...

if (length(dots) && !all(vapply(dots, function(x) is.symbol(x) ||

is.character(x), NA, USE.NAMES = FALSE)))

stop("... must contain names or character strings")

names <- vapply(dots, as.character, "")

if (length(names) == 0L)

names <- character()

list <- .Primitive("c")(list, names)

.Internal(remove(list, envir, inherits))

<bytecode: 0x02a0f900>

<environment: namespace:base>

> load{file="book4.csv")

Error: unexpected '{' in "load{"

> load(file="book4.csv")
Error in load(file = "book4.csv") :

bad restore file magic number (file may be corrupted) -- no data loaded

In addition: Warning message:

file ‘book4.csv’ has magic number 'ABUND'

Use of save versions prior to 2 is deprecated

> load(file=file.choose())

Error in load(file = file.choose()) :

bad restore file magic number (file may be corrupted) -- no data loaded

In addition: Warning message:

file ‘Book4.csv’ has magic number 'ABUND'

Use of save versions prior to 2 is deprecated

> load(file=file.choose())

Error in load(file = file.choose()) :

bad restore file magic number (file may be corrupted) -- no data loaded

In addition: Warning message:

file ‘Book2.csv’ has magic number '"jan,'

Use of save versions prior to 2 is deprecated

> data7=(23,17,12.5,14.5,11,12)

Error: unexpected ',' in "data7=(23,"

> data4

[1] 23.0 17.0 12.5 11.0 17.0 12.0 14.5 9.0 11.0 9.0 12.0 5.0 14.5 17.0 8.0 21.0

> data5

[1] "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec"

> cut

function (x, ...)


UseMethod("cut")

<bytecode: 0x074501a0>

<environment: namespace:base>

> grass

mow unmow

1 12 8

2 15 9

3 17 7

4 11 9

5 15 NA

> data7

Error: object 'data7' not found

> data4

[1] 23.0 17.0 12.5 11.0 17.0 12.0 14.5 9.0 11.0 9.0 12.0 5.0 14.5 17.0 8.0 21.0

> data4i=as.integer(data4)

> data4i

[1] 23 17 12 11 17 12 14 9 11 9 12 5 14 17 8 21

> data4n=as.numeric(data4i)

> data4n

[1] 23 17 12 11 17 12 14 9 11 9 12 5 14 17 8 21

> data4c=as.character(data4(

+ data4c=as.character(data4)

+s

Error: unexpected symbol in:


"

s"

> data4c=as.character(data4)

> data4c

[1] "23" "17" "12.5" "11" "17" "12" "14.5" "9" "11" "9" "12" "5" "14.5" "17" "8" "21"

> data5

[1] "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec"

> data5n=as.numeric(data5)

Warning message:

NAs introduced by coercion

> data5n

[1] NA NA NA NA NA NA NA NA NA NA NA NA

> data5c=as.numeric(as.factor(data5)))

Error: unexpected ')' in "data5c=as.numeric(as.factor(data5)))"

> data5c=as.numeric(as.factor(data5))

> data5c

[1] 5 4 8 1 9 7 6 2 12 11 10 3

> grass.1

> str(grass)

'data.frame': 5 obs. of 2 variables:

$ mow : int 12 15 17 11 15

$ unmow: int 8 9 7 9 NA

6 song thrush NA 4 0 6 0 0

> bird=read.csv(file.choose())

> bird
X X.1 Garden Hedgeroew parkland Pasture Woodland

1 blackbird NA 47 10 40 2 2

2 chaffinch NA 19 3 5 0 2

3 Great Tit NA 50 0 10 7 0

4 House sparow NA 46 16 8 4 0

5 Robin NA 9 3 0 0 2

6 song thrush NA 4 0 6 0 0

> str(bird)

'data.frame': 6 obs. of 7 variables:

$X : Factor w/ 6 levels "blackbird","chaffinch",..: 1 2 3 4 5 6

$ X.1 : logi NA NA NA NA NA NA

$ Garden : int 47 19 50 46 9 4

$ Hedgeroew: int 10 3 0 16 3 0

$ parkland : int 40 5 10 8 0 6

$ Pasture : int 2 0 7 4 0 0

$ Woodland : int 2 2 0 0 2 0

> class(grass

+ class(grassdd

Error: unexpected symbol in:

"class(grass

class"

> class(grass)

[1] "data.frame"

> class(bird)

[1] "data.frame"
> history()

> savehistory(file='bird.rhistory')

> loadhistory(file="bird.csv")

> sys.getenv('R.histfile')

Error in sys.getenv("R.histfile") : could not find function "sys.getenv"

> save()

Error in save() : 'file' must be specified

In addition: Warning message:

In save() : nothing specified to be save()d

> save(list,file="bird")

> save(list,file="bird.csv")

Error in gzfile(file, "wb") : cannot open the connection

In addition: Warning message:

In gzfile(file, "wb") :

cannot open compressed file 'bird.csv', probable reason 'Permission denied'

>

You might also like