Professional Documents
Culture Documents
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
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
TRUE
TRUE
TRUE
TRUE
FALSE
FALSE
TRUE
TRUE
TRUE
TRUE
FALSE
FALSE
(4). A named character vector pets with elements dog, cat, and bird.
> pets<-c("dog","cat","bird")
> pets
[1] "dog" "cat" "bird"
[1] 9.975
> median(solar.radiation)
[1] 10.65
> var(solar.radiation)
[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
>hist(sr10)
>hist(srm2)
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
(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
(b) 5 != 5 | 6 == 6
TRUE
(c) 5 == 5 | 6 != 6
TRUE