logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

You have to make two custom functions, each function should take a vector

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

# Task 1 - (15 points) ---------------------------------------------------------

# Create a variable "mydata" - a data structure with the output presented 

# below as comments

# > print(mydata)

# [[1]]

# [1] "Some long text"

# [[2]]

# [1] 1 2 3 4 5

#

# $mix

# $mix$a

# [1] "text"

# $mix$b

#   a  b  c

# 1 a -2  3

# 2 b  0  4

# 3 c  2 -5

# 4 d  4 77 

 

# ---- your code here ---- #

 

mydata <- data.frame(a = letters[1:4], b = c(-2,0,2,4) , c= c(3,4,-5,77))

# Use different ways of indexing (single and double squared brackets, names,

# indexes, $-notation) to extract value 77 from that object.

# Provide at lease three different options.

# ---- your code here ---- #

1- mydata[c(4),"c"]

   [1] 77

2- mydata$c[c(4)]

   [1] 77

3- mydata[[4,3]]

   [1] 77

# Task 2 - (20 points) ---------------------------------------------------------

# Create a variable "df" - a data structure with the output presented 

# below as comments

 

# > print(df)

#   x  y  z

# 1 a -4  3

# 2 b -2  4

# 3 c  0 -5

# 4 d  2  6

# 5 e  4 -8

# ---- your code here ---- #

df <- data.frame(x = letters[1:5], y = c(-4,-2,0,2,4) , z= c(3,4,-5,6,-8))

> print(df)

  x  y  z

1 a -4  3

2 b -2  4

3 c  0 -5

4 d  2  6

5 e  4 -8

 

 

# Your main task is to change some values in "df": all negative numbers 

# should be multiplied by 10. Everything else remains the same.

# Create two functions. Each function should take variable "df" as input and

# output the same type of object but with changed values as per the rule above.

# First function can use control flow operations - loops (for/while) and 

# selection/comparison (if/else). 

df <-c(-4,-2,0,2,4,3,4,-5,6,-8)

res <- c(length(10))

for (i in d) {

    res <- c(res , i * 10) 

}

res

# Second function can use only vectorisation/indexing - no loops.

# ---- your code here ---- #

# Task 3 - (15 points) ---------------------------------------------------------

# There are two very useful functions - max() and min(). They return maximal 

# and minimal values of the numerical vector. 

# Create your own custom function minmax() that does the same job. That

# is, it takes a numerical vector as an input and outputs a vector with minimal 

# maximal of this vector values. 

# Use only control flow operators - min/max functions.

# Prepare data and check min/max values

test.vector <- rnorm(10)

test.vector

# These are results you should get from your function

c(min = min(test.vector), max = max(test.vector))

# ---- your code here ---- #

# Task 4 - (15 points) ---------------------------------------------------------

# Take a vector below. Assume that you start summation from the first element

# adding element by element. Your task is to find the maximal number 

# of elements you can take while the total summation stays below a given

# threshold.

 

# For example:  x <- c(1, 2, 3, 4, 5, 6). This summation (or running total, or

# cumulative sum) is c(1, 3, 6,10,15,21). Hence, for the summation to stay below 

# a threshold 13 you can take 4 first numbers.

x <- runif(50)  # Your test vector

y <- 10         # Your threshold

# You have to make two custom functions, each function should take a vector

# of numbers and threshold level; and it should return the number of values 

# to take before you hit a threshold.

# First function should use control flow operators and no vectorisation. 

# Second function should use vectorisation/indexing and function cumsum(), but

# no control flow operators.

# ---- your code here ---- #

Task 5 - (20 points) ---------------------------------------------------------

# Load dataset "Titanic" - count of passengers survived/died on Titanic.

# Run the following code:

data(Titanic)

print(Titanic)

?Titanic   # check the help file for this data set

# Extract information and answer the following questions:

# ---- your code here ---- #

# 1. How many female crew members survived?

# 2. How many people were on Titanic in total?

# 3. What proportion of females survived? 

# 4. What proportion of males survived? 

# 5. First-class passengers were on the higher decks and, as a result, they had 

# a higher chance to survive. How much higher was the probability to survive 

# for first-class passengers compared to the third class on the lower decks?

 Task 6 - (15 points) ---------------------------------------------------------

# In this task you will do a text analysis. Run the code below.

# Take a vector of characters below - variable "text". 

text <- c("Take a vector below", 

          "Assume that you start summation from the first element adding element by element", 

          "Your task is to find the maximal number of elements you can take while the total summation stays below some given threshold")

 

# Split the text in individual words.

# You don't know this function - it is OK, you will learn it later (in week 9)

# Check the data structure of the variable "words"

words <- strsplit(text, split = " ")    

# Create an array with counts of how many times a word with one, two, 

# three, ... etc. letters appears in each sentences. That is, the array with 

# 3 columns (one column per sentence) and 9 rows (1 letter, 2 letters, etc.)

 ---- your code here ---- #

# THE END - DON'T FORGET TO SAVE YOUR R-SCRIPT ---------------------------------

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Um e HaniScience

754 Answers

Hire Me
expert
Muhammad Ali HaiderFinance

929 Answers

Hire Me
expert
Husnain SaeedComputer science

555 Answers

Hire Me
expert
Atharva PatilComputer science

774 Answers

Hire Me

Get Free Quote!

366 Experts Online