In: Computer Science
Data Analysis & Visualization
Topic R vector and save the r code in a text file
Problem 1.
Create two vectors named v and w
with the following contents:
v : 21,10,32,2,-3,4,5,6,7,4,-22
w : -18,72,11,-9,10,2,34,-5,18,9,2
A) Print the length of the vectors
B) Print all elements of the vectors
C) Print elements at indices 3 through 7.
D) Print the sum of the elements in each vector.
E) Find the mean of each vector. (Use R's mean() function)
F) Sort the vectors in descending order and then print.
G) Add vectors v and w.
H) Multiply vectors v and w.
I) In vector v select all elements that are greater than
zero.
J) In vector w select all elements that are less than zero.
K) Multiply each element of vector v by 6. And then print.
L) Find maximum and minimum of each vector.
M) In each vector, replace all negative values with the average of
all numbers in the vector.
Problem 2.
Write a for loop to print the numbers 100, 98, 96, . . . , 4, 2.
Problem 3.
Create a data frame with the following content:
city |
county |
state |
population |
Chicago |
Cook |
IL |
2853114 |
Kenosha |
Kenosha |
WI |
90352 |
Aurora |
Kane |
IL |
171782 |
Elgin |
Kane |
IL |
94487 |
Gary |
Lake(IN) |
IN |
102746 |
Joliet |
Kendall |
IL |
106221 |
Naperville |
DuPage |
IL |
147779 |
Arlington Heights |
Cook |
IL |
76031 |
Bolingbrook |
Will |
IL |
70834 |
Cicero |
Cook |
IL |
72616 |
Evanston |
Cook |
IL |
74239 |
Hammond |
Lake(IN) |
IN |
83048 |
Palatine |
Cook |
IL |
67232 |
Schaumburg |
Cook |
IL |
75386 |
Skokie |
Cook |
IL |
63348 |
Waukegan |
Lake(IL) |
IL |
91452 |
Hint: Create vectors for each column and then
create a data frame using vectors and the data.frame() function. A
sample is given in the lecture notes.
city<-
c("Chicago","Kenosha","Aurora","Elgin","Gary","Joliet","Naperville","Arlington
Heights","Bolingbrook","Cicero","Evanston","Hammond","Palatine","Schaumburg","Skokie","Waukegan")
county<- c( "Cook", "Kenosha", "Kane", "Kane", "Lake(IN)",
"Kendall", "DuPage", "Cook", "Will", "Cook", "Cook", "Lake(IN)",
"Cook", "Cook", "Cook", "Lake(IL)")
state<- c( "IL", "WI", "IL", "IL", "IN", "IL", "IL", "IL", "IL", "IL", "IL", "IN", "IL", "IL", "IL", "IL")
population<-c(2853114,90352,171782,94487,102746,106221,147779,76031,70834,72616,74239,83048,67232,75386,63348,91452)
A) Print the mean of population.
B) Print the population of all cities greater than 800,000.
C) Print city, county,state and population of all cities with population less than 1,000,000.
D) Print city, county, state, and population of the most crowded city.
Hint: You can use subset() function with max() in your selection criteria in the subset() function.Then print the data in the subset.
E) Print the city and state of the least crowded city.
Hint: You can use subset() function with min() in your selection criteria in the subset() function. Then print the data in the subset.
#creating vector v and w
v <- c(21,10,32,2,-3,4,5,6,7,4,-22)
w <- c(-18,72,11,-9,10,2,34,-5,18,9,2)
#lenth of vectors
length(v)
length(w)
#printing the elementsof vectors
print(v)
print(w)
#printing the elements of vectors from index 3 to 7
print(v[3:7])
print(w[3:7])
#printing the sum of elements in vector
sum(v)
sum(w)
#mean of vectors
mean(v)
mean(w)
#sorting th vectors
v <- sort(v,decreasing = TRUE)
w <- sort(w,decreasing = TRUE)
#adding the vectors
v+w
#print all values of v greater than 0
for (i in v){
if(i>0){
print(i)
}
}
#print all values of w less than 0
for (i in w){
if(i<0){
print(i)
}
}
#multipling each element by 6
print(v*6)
#max and min of each vector
print(min(v))
print(max(v))
print(min(w))
print(max(w))
#replace with mean
meanv = mean(v)
v[v<0] <- meanv
meanw = mean(w)
w[w<0] <- meanw
#loop to print 100 98 ...2
i=100
while(i>=2){
print(i)
i=i-2
}
#dataframe creation
city <-
c("Chicago","Kenosha","Aurora","Elgin","Gary","Joliet","Naperville","Arlington
Heights","Bolingbrook","Cicero","Evanston","Hammond","Palatine","Schaumburg","Skokie","Waukegan")
county <- c( "Cook", "Kenosha", "Kane", "Kane", "Lake(IN)",
"Kendall", "DuPage", "Cook", "Will", "Cook", "Cook", "Lake(IN)",
"Cook", "Cook", "Cook", "Lake(IL)")
state <- c( "IL", "WI", "IL", "IL", "IN", "IL", "IL", "IL",
"IL", "IL", "IL", "IN", "IL", "IL", "IL", "IL")
population <-c
(2853114,90352,171782,94487,102746,106221,147779,76031,70834,72616,74239,83048,67232,75386,63348,91452)
df = data.frame(city,county,state,population)
#mean of population
print(mean(df[["population"]]))
#poplutaion grater than 800,000
popgreater <- subset(df,population>800000,select =
c("city","population"))
popgreater
#poplutaion less than 1,000,000
poplessthan <- subset(df,population<1000000,select =
c("city","county","state","population"))
poplessthan
#most crowded city,state,county
print(subset(df,population==max(df[["population"]]),select =
c("city","county","state","population")))
#least crowded city,state
print(subset(df,population==min(df[["population"]]),select =
c("city","state")))