Question

In: Computer Science

Data Analysis & Visualization Topic R vector and save the r code in a text file...

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.

Solutions

Expert Solution

#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")))


Related Solutions

Download the file data.csv (comma separated text file) and read the data into R using the...
Download the file data.csv (comma separated text file) and read the data into R using the function read.csv(). Your data set consists of 100 measurements in Celsius of body temperatures from women and men. Use the function t.test() to answer the following questions. Do not assume that the variances are equal. Denote the mean body temperature of females and males by μFμF and μMμMrespectively. (a) Find the p-value for the test H0:μF=μMH0:μF=μM versus HA:μF≠μM.HA:μF≠μM. Answer (b) Are the body temperatures...
Whats the code to open a text file and every line in that text file that...
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
Python 3 Code does not save properly the data in the excel file. it stores the...
Python 3 Code does not save properly the data in the excel file. it stores the data in the same row over and over # required library import tkinter as tk from tkcalendar import DateEntry import xlsxwriter # frame window = tk.Tk() window.title("daily logs") #window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=3, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money...
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
Data Analysis & Visualization subject Question 1. What does "R is a vectorized language" mean? Question...
Data Analysis & Visualization subject Question 1. What does "R is a vectorized language" mean? Question 2. What is unexplainable variance? Question 3. What is confusion matrix? A) What is model precision in a confusion matrix? B) What is model recall in a confusion matrix? C) Consider the following confusion matrix of daily movements of a stock market. ACTUAL Down Up PREDICTED Down 30 30 Up 70 110 i) Compute the precision of the model. ii) Compute the recall of...
APPLIED STATISTICS 2 USE R CODE! SHOW R CODE Use data file RecordMath2526.txt, to produce a...
APPLIED STATISTICS 2 USE R CODE! SHOW R CODE Use data file RecordMath2526.txt, to produce a plot graph with Exam1 as x, Exam2 as y, use Gender as color, and Hw1 as pch. RecordMath2526 information Index Gender Hw1 Hw2 Hw3 Exam1 Hw4 Exam2 Hw5 Hw6 Hw7 Final 1 F 9 6 8 60 7 82 10 10 9 69 2 M 10 10 10 94 9 98 10 10 8 91 3 M 9 10 8 79 9 55 10...
Find the 95% confidence interval of the mean of a vector in r code. The vector...
Find the 95% confidence interval of the mean of a vector in r code. The vector length is 100.
Research and discuss the topics of data analysis and data visualization as it relates to the...
Research and discuss the topics of data analysis and data visualization as it relates to the accounting profession. How do they impact and enhance financial statement analysis? Research and discuss the topic of Sustainability and Corporate Social Responsibility. How can this reported information affect or enhance financial statement analysis. Should this type of reporting be required? Why or why not?
Here is the R code for running a t-test: t.test( numeric vector of data values, another...
Here is the R code for running a t-test: t.test( numeric vector of data values, another optional numeric vector of data values,        alternative = c("two.sided", "less", "greater"),        mu = Ho, paired = c(TRUE, FALSE), var.equal = c(TRUE,FALSE),conf.level =1-) 1.) Suppose 30 students are all taking the same Math 115 and English 101 classes at CSUN. You want to know in which class students tend to do better. The data below represents the class averages of the students in both classes....
Here is the R code for running a t-test: t.test( numeric vector of data values, another...
Here is the R code for running a t-test: t.test( numeric vector of data values, another optional numeric vector of data values,        alternative = c("two.sided", "less", "greater"),        mu = Ho, paired = c(TRUE, FALSE), var.equal = c(TRUE,FALSE),conf.level =1-) 2) You want to determine if the average height of men in California is greater than the average height of men in Nebraska. You take a random sample of 30 men in California and 30 men in Nebraska. The data below represents...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT