Sie sind auf Seite 1von 7

Hw1 5pt due Jan 21 11:55am

Xinjie Hu
Email your code and your answers to jzhang47@gsu.edu before the deadline, also hand in
a hard copy on class.

1. The simplest data structure in R is a vector. Create the following


vectors:
(1). x with elements 20.1, 17.1, 12.5, and 10
> x=c(20.1,17.1,12.5,10)
> x
[1] 20.1 17.1 12.5 10.0

(2). An integer vector y with elements 79 to 100


> y=c(79:100)
> y
[1]
6

79

97

80

98

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

99 100

(3). A logical vector z indicating the elements of y that are below 86


> z=(y<86)
> z
[1]

TRUE

TRUE

TRUE

TRUE

TRUE

TRUE

TRUE FALSE FALSE FALSE FALSE F

ALSE FALSE FALSE FALSE


[16] FALSE FALSE FALSE FALSE FALSE FALSE FALSE

(4). A named character vector pets with elements dog, cat, and bird.
> pets=c("dog","cat","bird")
> pets
[1] "dog"

"cat"

"bird"

2. The following are a sample of observations on incoming solar radiation


8.8 11.1 10.6 6.3 11.2 8.9 12.2 10.7
(a) Assign the data to an vector called solar.radiation
> solar.radiation=c(8.8,11.1,10.6,6.3,11.2,8.9,12.2,10.7)
> solar.radiation
[1]

8.8 11.1 10.6

6.3 11.2

8.9 12.2 10.7

(b) Find the mean, median and variance of the radiation observations.
> mean
[1] 9.975

> median=median(solar.radiation)
> median
[1] 10.65
> variance=var(solar.radiation)
> variance
[1] 3.525

(c) Add 50 to each observation of solar.radiation, and assign the result


to sr50. Find the mean, median, and variance of sr50.
> sr50=solar.radiation+50
> mean50=mean(sr50)
> mean50
[1] 59.975
> median50=median(sr50)
> median50
[1] 60.65
> variance50=var(sr50)
> variance50
[1] 3.525

(d) Multiply each observation by -7, and assign the result to srm7.
Find the mean, median, and variance of srm7. How do the statistics change now?
> srm7=(-7)*solar.radiation
> srm7
[1] -61.6 -77.7 -74.2 -44.1 -78.4 -62.3 -85.4 -74.9
> mean7=mean(srm7)
> mean7
[1] -69.825
> median7=median(srm7)
> median7
[1] -74.55
> variance7=var(srm7)
> variance7
[1] 172.725

solar.radiation
sr50
srm7

Mean

Median

Variance

9.975

10.65

3.525

59.975

60.65

3.525

-69.825

-74.55

172.725

After comparison, we find that the mean and median of srm7 become (-7) times of the
original mean and median. The variance of srm7 become (-7)2=49 times of the
original variance.

(e) Plot a histogram of the solar.radiation, sr50, and srm7.


> hist(solar.radiation)

>hist(sr50)

>hist(srm7)

3. Input the data set from hw 1 data 1.txt" to a data frame called rain.df.
Use the header=FALSE option.
> getwd()
[1] "/Users/xinjiehu/Desktop/2016 spring/8670 computational methods
in statistics/HW1"
> rain.df=read.table("hw_1_data_1.txt",header=F)
> data.frame(rain.df)

(a) Display the row 1, column 3 element of rain.df.


> rain.df[1,3]
[1] 1

(b) What are the names of the columns of rain.df.


> names(rain.df)
[1] "V1"

"V2"

"V3"

"V4"

"V5"

"V6"

"V7"

"V8"

"V9"

"V10" "V11"

"V12" "V13" "V14" "V15"


[16] "V16" "V17" "V18" "V19" "V20" "V21" "V22" "V23" "V24" "V25"
"V26" "V27"

(c) Display the contents of the second row of the rain dataset.
> rain.df[2,]
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19
V20 V21 V22 V23 V24 V25 V26 V27
2 60
0

2
0

0
0

(d) Use the following command to re-label the columns of this data
frame:
> names(rain.df) <- c("year", "month", "day", seq(0, 23))
> names(rain.df) <- c("year", "month", "day", seq(0, 23))
> names(rain.df)
[1] "year"
"6"

"0"

"1"

"2"

"3"

"4"

"5"

"13"

"14"

"15"

"16"

"7"

[12] "8"
"17"

"month" "day"
"9"

"10"

"11"

"12"

"20"

"21"

"22"

"23"

"18"

[23] "19"

(e) Create a new column called daily which is the sum of the 24 hourly
columns.

> rain.df$daily=c(rowSums(rain.df[,4:27]))
> rain.df

(f) Plot a histogram of the daily rainfall amounts.


> hist(rain.df$daily,nclass=10)

4. What are the return values of the following statements?


(a) 3 == 3 & 5 == 5
> 3 == 3 & 5 == 5
[1] TRUE

(b) 5 != 5 | 6 == 6
> 5 != 5 | 6 == 6
[1] TRUE

(c) 7 == 7 | 6 != 6
> 7 == 7 | 6 != 6
[1] TRUE

(d) x <- 5 & y <- 6


> x <- 5 & y <- 6
Error in 5 & y <- 6 : target of assignment expands to non-language
object

5. Let x <- seq(1,1000, by=2). What is the value of x[1]^2 + ... + x[n]^2 divided
by the number of elements of x?
> x <- seq(1,1000, by=2)
> sum(x^2)/length(x)
[1] 333333

Reference
R codes:
# Q1
x=c(20.1,17.1,12.5,10)
x
y=c(79:100)
y
z=(y<86)
z
pets=c("dog","cat","bird")
pets
# Q2
solar.radiation=c(8.8,11.1,10.6,6.3,11.2,8.9,12.2,10.7)
solar.radiation
mean=mean(solar.radiation)
mean
median=median(solar.radiation)
median
variance=var(solar.radiation)
variance
sr50=solar.radiation+50
mean50=mean(sr50)
mean50
median50=median(sr50)
median50
variance50=var(sr50)
variance50
srm7=(-7)*solar.radiation
srm7
mean7=mean(srm7)
mean7
median7=median(srm7)
median7
variance7=var(srm7)
variance7
hist(solar.radiation)
hist(sr50)
hist(srm7)
# Q3
getwd()
rain.df=read.table("hw_1_data_1.txt",header=F)

data.frame(rain.df)
rain.df[1,3]
names(rain.df)
rain.df[2,]
names(rain.df) <- c("year", "month", "day", seq(0, 23))
names(rain.df)
rain.df$daily=c(rowSums(rain.df[,4:27]))
hist(rain.df$daily,nclass=10)
# Q4
3 == 3 & 5 == 5
5 != 5 | 6 == 6
7 == 7 | 6 != 6
x <- 5 & y <- 6
# Q5
x <- seq(1,1000, by=2)
sum(x^2)/length(x)

Das könnte Ihnen auch gefallen