Beruflich Dokumente
Kultur Dokumente
Xinjie Hu
Email your code and your answers to jzhang47@gsu.edu before the deadline, also hand in
a hard copy on class.
79
97
80
98
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
99 100
TRUE
TRUE
TRUE
TRUE
TRUE
TRUE
(4). A named character vector pets with elements dog, cat, and bird.
> pets=c("dog","cat","bird")
> pets
[1] "dog"
"cat"
"bird"
6.3 11.2
(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
(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.
>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)
"V2"
"V3"
"V4"
"V5"
"V6"
"V7"
"V8"
"V9"
"V10" "V11"
(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
(b) 5 != 5 | 6 == 6
> 5 != 5 | 6 == 6
[1] TRUE
(c) 7 == 7 | 6 != 6
> 7 == 7 | 6 != 6
[1] TRUE
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)