You are on page 1of 5

Hw1 5pt due Jan 26 11:55pm

Lizhi Fu 002-02-3612
1. The simplest data structure in R is a vector. Create the following
vectors:
(1). x with elements 10.1, 14.1, 2.5, and 10
> x<-c(10.1,14.1,2.5,10)
>x
[1] 10.1 14.1 2.5 10.0

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


> y<-seq(39,100)
>y
[1] 39 40 41 42
[16] 54 55 56 57
[31] 69 70 71 72
[46] 84 85 86 87
[61] 99 100

43
58
73
88

44
59
74
89

45
60
75
90

46
61
76
91

47
62
77
92

48
63
78
93

49
64
79
94

50
65
80
95

51
66
81
96

52
67
82
97

53
68
83
98

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


> z<-y<76
>z
[1] TRUE TRUE
[11] TRUE TRUE
[21] TRUE TRUE
[31] TRUE TRUE
[41] FALSE FALSE
[51] FALSE FALSE
[61] FALSE FALSE

TRUE
TRUE
TRUE
TRUE
FALSE
FALSE

TRUE
TRUE
TRUE
TRUE
FALSE
FALSE

TRUE TRUE TRUE TRUE TRUE TRUE


TRUE TRUE TRUE TRUE TRUE TRUE
TRUE TRUE TRUE TRUE TRUE TRUE
TRUE TRUE TRUE FALSE FALSE FALSE
FALSE FALSE FALSE FALSE FALSE 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
at a greenhouse:
11.1 10.6 6.3 8.8 10.7 11.2 8.9 12.2
(a) Assign the data to an vector called solar.radiation
> solar.radiation<-c(11.1,10.6,6.3,8.8,10.7,11.2,8.9,12.2)
> solar.radiation
[1] 11.1 10.6 6.3 8.8 10.7 11.2 8.9 12.2

(b) Find the mean, median and variance of the radiation


observations.
> mean(solar.radiation)

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

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


result
to sr10. Find the mean, median, and variance of sr10.
> sr10<-solar.radiation+10
> sr10
[1] 21.1 20.6 16.3 18.8 20.7 21.2 18.9 22.2
> mean(sr10)
[1] 19.975
> median(sr10)
[1] 20.65
> var(sr10)
[1] 3.525

(d) Multiply each observation by -2, and assign the result to srm2.
Find the mean, median, and variance of srm2. How do the statistics
change now?
> srm2<-solar.radiation*(-2)
> srm2
[1] -22.2 -21.2 -12.6 -17.6 -21.4 -22.4 -17.8 -24.4
> mean(srm2)
[1] -19.95
> median(srm2)
[1] -21.3
> var(srm2)
[1] 14.1

Statistical change: the mean and midian are both multiply by -2


The variance changed
(e) Plot a histogram of the solar.radiation, sr10, and srm2.
> hist(solar.radiation)

>hist(sr10)

>hist(srm2)

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] "C:/Users/biouser/Documents"
> rain.df<-read.table("hw_1_data_1.txt", header=F)
> rain.df

(a) Display the row 2, column 4 element of rain.df.


> rain.df[2,4]
[1] 0

(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"
[12]"V12" "V13" "V14" "V15" "V16" "V17" "V18" "V19" "V20" "V21" "V22"
[23] "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
2 60 4 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
V21 V22 V23 V24 V25 V26 V27
2 0 0 0 0 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" "month" "day" "0"
"1"
"2"
"3"
"4"
[9] "5"
"6"
"7"
"8"
"9"
"10" "11" "12"
[17] "13" "14" "15" "16" "17" "18" "19" "20"
[25] "21" "22" "23"

(e) Create a new column called daily which is the sum of the 24
hourly
columns.
rain.df["daily"]<-NA
> rain.df$daily<-apply(rain.df[,4:23],1,sum)
> rain.df

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


> hist(rain.df$daily)

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


(a) 4 == 4 & 5 == 5
TRUE

(b) 5 != 5 | 6 == 6
TRUE

(c) 5 == 5 | 6 != 6
TRUE

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


ERROR

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

You might also like