100% found this document useful (1 vote)
218 views13 pages

Codes From Pages 3-11

There are 79 cities and villages in New York that had a population of more than 10,000 residents in 2010 according to the data set. The population counts range from 8175133 in New York City to 11128 in Ogdensburg.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
218 views13 pages

Codes From Pages 3-11

There are 79 cities and villages in New York that had a population of more than 10,000 residents in 2010 according to the data set. The population counts range from 8175133 in New York City to 11128 in Ogdensburg.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Name: CIARWENA T.

PANGCOGA

Course/Section: STT 041.1 – PQ1

Codes from pages 3-11

> 4+5

[1] 9

> sqrt(3)

[1] 1.732051

> height <- c(171, 177, 178, 175, 202, 180, 192, 182, 195, 190)

> height

[1] 171 177 178 175 202 180 192 182 195 190

> sqrt(height)

[1] 13.07670 13.30413 13.34166 13.22876 14.21267 13.41641 13.85641 13.49074

[9] 13.96424 13.78405

> height/2.54

[1] 67.32283 69.68504 70.07874 68.89764 79.52756 70.86614 75.59055 71.65354

[9] 76.77165 74.80315

> sum(height)

[1] 1842

> length(height)

[1] 10

> meanHt <- sum(height)/length(height)

> meanHt
[1] 184.2

> height^2

[1] 29241 31329 31684 30625 40804 32400 36864 33124 38025 36100

> sum(height^2)

[1] 340196

> sum(height)^2

[1] 3392964

> (sum(height))^2

[1] 3392964

> sum((height-meanHt)^2)

[1] 899.6

> median(height)

[1] 181

> min(height)

[1] 171

> max(height)

[1] 202

> span <- c(173, 182, 182, 178, 202, 188, 198, 185, 193, 186)

> span

[1] 173 182 182 178 202 188 198 185 193 186
> difference <- span – height

> difference

[1] 2 5 4 3 0 8 6 3 -2 -4

> faculty.dat <- data.frame(span, height, difference)

> faculty.dat

span height difference

1 173 171 2

2 182 177 5

3 182 178 4

4 178 175 3

5 202 202 0

6 188 180 8

7 198 192 6

8 185 182 3

9 193 195 -2

10 186 190 -4

> faculty.dat[“ratio”] <- span/height

> faculty.dat

span height difference ratio

1 173 171 2 1.0116959

2 182 177 5 1.0282486

3 182 178 4 1.0224719

4 178 175 3 1.0171429

5 202 202 0 1.0000000

6 188 180 8 1.0444444

7 198 192 6 1.0312500


8 185 182 3 1.0164835

9 193 195 -2 0.9897436

10 186 190 -4 0.9789474

> sum(faculty.dat$ratio)/length(faculty.dat$ratio)

[1] 1.014043

> data.Ex01.2 <- read.table("http://waveland.com/Glover-Mitchell/Example01-2.txt",

+ header = TRUE)

> data.Ex01.2

CCH

1 17

2 19

3 31

4 39

5 48

6 56

7 68

8 73

9 73

10 75

11 80

12 122

> meanCCH <-sum(data.Ex01.2$CCH)/length(data.Ex01.2$CCH)

> meanCCH

[1] 58.4167

> sum((data.Ex01.2$CCH – meanCCH)^2)


[1] 9812.92

> head(data.Ex01.2$CCH)

[1] 17 19 31 39 48 56

> tail(data.Ex01.2$CCH, n = 2)

[1] 80 122

> help.start()

starting httpd help server … done

If nothing happens, you should open

‘http://127.0.0.1:24500/doc/html/index.html’ yourself

> help(foo)

No documentation for ‘foo’ in specified packages and libraries:

you could try ‘??foo’

> example(foo)

Warning message:

In example(foo) : no help found for ‘foo’

> x <- c(1, 4, 4, 5, 6)

>x

[1] 1 4 4 5 6

> y <- c(3:7)

>y

[1] 3 4 5 6 7
> students <- c(“Tom”, “Maria”, “Keberlei”, “Jordan”, “Chequira”)

> students

[1] “Tom” “Maria” “Keberlei” “Jordan” “Chequira”

> studentRecord <- data.frame(Name = students, A = x, B = y,+ GPA = (4*x + 3*y)/(x + y))

> studentRecord

Name A B GPA

1 Tom 1 3 3.25000

2 Maria 4 4 3.50000

3 Keberlei 4 5 3.44444

4 Jordan 5 6 3.45455

5 Chequira 6 7 3.46154

> max(studentRecord$GPA)

[1] 3.5

> data.Ex00.2 <- read.table(“http://waveland.com/Glover-Mitchell/Example00-2.txt”,+ header = TRUE)

> head(data.Ex00.2, n = 3)

Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

1 1921 129.9 16.9 194.7 203.0 19.8 197.9 167.7 4.3 44.8 28.4 57.2 226.4

2 1922 67.0 230.1 26.9 8.4 54.0 39.6 118.1 3.4 66.6 37.2 51.9 114.4

3 1923 46.7 21.4 87.1 192.4 9.1 73.9 64.5 24.4 36.1 10.9 47.1 62.5

Annual

1 1291.0

2 817.6

3 676.1

> tail(data.Ex00.2)

Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
73 1993 71.0 44.4 83.1 12.8 22.0 6.2 66.6 41.8 55.4 65.8 77.4

74 1994 235.6 101.7 139.0 49.2 65.6 9.4 9.0 2.2 53.0 41.4 41.6

75 1995 62.8 330.4 71.3 34.8 42.6 39.4 6.0 19.8 30.8 45.4 183.8

76 1996 241.8 52.6 28.6 78.2 678.4 34.2 50.2 41.0 49.2 31.4 82.8

77 1997 117.6 54.4 49.2 7.4 209.4 27.4 32.0 2.8 45.0 141.8 114.6

78 1998 132.6 36.2 41.4 164.8 140.0 11.2 23.6 91.6 112.6 26.0 104.8

Dec Annual

73 87.2 633.7

74 64.0 811.7

75 244.0 1111.1

76 117.0 1485.4

77 101.6 903.2

78 70.5 955.3

> max(data.Ex00.2$Annual)

[1] 2203.7

> min(data.Ex00.2$Annual)

[1] 555.1

> median(data.Ex00.2$Annual)

[1] 1102.4

> sum(data.Ex00.2$Annual)

[1] 89166
Problems

1. Create two data vectors x and y that contain the integers 1 to 21 and −10 to 10.
(a) Add, subtract, multiply, and divide x by y. What happens when dividing?
> x <- c (1:21)
>x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

> y <- c (-10:10)


>y
[1] -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10

> myTable <- data.frame (x,y)


> myTable
x y
1 1 -10
2 2 -9
3 3 -8
4 4 -7
5 5 -6
6 6 -5
7 7 -4
8 8 -3
9 9 -2
10 10 -1
11 11 0
12 12 1
13 13 2
14 14 3
15 15 4
16 16 5
17 17 6
18 18 7
19 19 8
20 20 9
21 21 10

> x+y
[1] -9 -7 -5 -3 -1 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31

> x-y
[1] 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11

> x*y
[1] -10 -18 -24 -28 -30 -30 -28 -24 -18 -10 0 12 26 42 60 80 102 126 152 180 210
> x/y
[1] -0.1000000 -0.2222222 -0.3750000 -0.5714286 -0.8333333 -1.2000000 -1.7500000 -
2.6666667 -4.5000000 -10.0000000
[12] 12.0000000 6.5000000 4.6666667 3.7500000 3.2000000 2.8333333 2.5714286
2.3750000 2.2222222 2.1000000

(b) Find the mean of each data set.


> meanHt <- sum(x)/length(x)
> meanHt
[1] 11

> mean <- sum(y)/length(y)


> mean0
[1] 0

(c) Find the corrected sum of squares for each data set.
> sum ((x-meanHt)^2)
[1] 770

> sum ((y-mean)^2)


[1] 770

(d) Find the mean of the product of x and y


mean (x*y)
[1] 36.66667

2. The data set http://waveland.com/Glover-Mitchell/Problem00-2.txt lists all cities and villages in New
York that had a population of more than 10,000 residents in 2010.
(a) How many such cities and villages are there?

> Problem00.2 <- read.table("http://waveland.com/Glover-Mitchell/Problem00-2.txt",header =


TRUE)

> Problem00.2

City Population

1 NewYork 8175133

2 Buffalo 261310

3 Rochester 210565

4 Yonkers 195976

5 Syracuse 145170
6 Albany 97856

7 NewRochelle 77062

8 MountVernon 67292

9 Schenectady 66135

10 Utica 62235

11 WhitePlains 56853

12 Hempstead 53891

13 NiagaraFalls 50193

14 Troy 50129

15 Binghamton 47376

16 Freeport 42860

17 ValleyStream 37511

18 Rome 33725

19 LongBeach 33275

20 Poughkeepsie 32736

21 NorthTonawanda 31568

22 SpringValley 31347

23 Jamestown 31146

24 Ithaca 30014

25 Elmira 29200

26 PortChester 28967

27 Newburgh 28866

28 Middletown 28086

29 Auburn 27687

30 Harrison 27472

31 Lindenhurst 27253

32 Watertown 27023

33 GlenCove 26964

34 SaratogaSprings 26586
35 Ossining 25060

36 RockvilleCentre 24023

37 Kingston 23893

38 Peekskill 23583

39 GardenCity 22371

40 Lockport 21165

41 KiryasJoel 20175

42 Plattsburgh 19989

43 Lynbrook 19427

44 Cortland 19204

45 Mamaroneck 18929

46 Mineola 18799

47 Amsterdam 18620

48 Oswego 18142

49 Lackawanna 18141

50 Scarsdale 17166

51 MassapequaPark 17008

52 Cohoes 16168

53 FloralPark 15863

54 Rye 15720

55 Gloversville 15665

56 Beacon 15541

57 Batavia 15465

58 Kenmore 15423

59 Depew 15303

60 JohnsonCity 15174

61 Westbury 15146

62 Tonawanda 15130

63 GlensFalls 14700
64 Olean 14452

65 Oneonta 13901

66 Endicott 13392

67 Geneva 13261

68 Dunkirk 12563

69 Babylon 12166

70 Haverstraw 11910

71 Fulton 11896

72 Patchogue 11798

73 Oneida 11393

74 Tarrytown 11277

75 Fredonia 11230

76 Corning 11183

77 LakeGrove 11163

78 Ogdensburg 11128

79 Massena 10936

80 MountKisco 10877

81 DobbsFerry 10875

82 Suffern 10723

83 Woodbury 10686

84 Canandaigua 10545

85 Lancaster 10352

86 Watervliet 10254

87 WestHaverstraw 10165

> length(Problem00.2$City)

[1] 87

(b) Find the total population of all these cities and villages.
> sum(Problem00.2$Population)

[1] 11008581

(c) Find the median population of these cities and villages.

> median(Problem00.2$Population)

[1] 19204

(e) Find the mean population of these cities and villages.


> mean(Problem00.2$Population)

[1] 126535.4

(f) Find the corrected sum of squares for these data.


> meanPopulation <- sum(Problem00.2$Population)/length(Problem00.2$Population)
> meanPopulation

[1] 126535.4

> sum ((Problem00.2$Population - meanPopulation)^2)

[1] 6.568503e+13

(f) The total population of New York State was 19,378,102 at the time of the census. What proportion
of the population lived outside these cities and towns?
> NewYork <- c(19378102)
> NewYork

[1] 19378102

> NewYork - sum(Problem00.2$Population)

[1] 8369521

You might also like