Question

In: Computer Science

Consider a numeric vector x (e.g., x=c(2,3,-1,1.2,2.6,0)), write the R code that determine whether each element...

Consider a numeric vector x (e.g., x=c(2,3,-1,1.2,2.6,0)), write the R code that determine whether each element of x is (Verify that your code work correctly):

(a) positive

(b) non-positive

(c) even or odd

(d) an integer

Solutions

Expert Solution

Given code is in R:

x <- c(2,3,-1,1.2,2.6,0)
for(i in x){    #iterating through the vector.
    if(i > 0){      #to check if the number is positive.
        print(paste(i, " is positive."))
    }
    if(i <= 0){     #to check if the number is not positive.
        print(paste(i, " is non-positive."))
    }
    if(i == round(i)){      #to check if the number is integer, if yes check for odd and even.
        if(i%%2 == 0){      
            print(paste(i, " is even."))
        }
        if(i%%2 != 0){
            print(paste(i, " is odd."))
        }
    }
    if(i == round(i)){
        print(paste(i, " is an integer."))
    }
}

Output is as follows:

Every case has been divided into if cases so that it can be easily understood.

Kindly like if this helps :)


Related Solutions

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...
Write a function in R named counts. This function should take as parameters a numeric vector...
Write a function in R named counts. This function should take as parameters a numeric vector x and also a number indicating a number of bins n. The function will consider the range [min(x),max(x)], and then consider a parti- tion of this interval into n non-overlapping equally sized half open intervals: I1 = [min(x),b1),I2 = [b1,b − 2),...,In = (bn−1,max(x)]. Note that since the intervals are equally sized, the value of bi is constrained. The function will then return a...
For c++ Write the code to initialize an array such that each element gets two times...
For c++ Write the code to initialize an array such that each element gets two times the value of its index (e.g. 0, 2, 4, 6, …)
For each problem, write an R code to compute each probability. (a)   Binomial Distribution: X ~...
For each problem, write an R code to compute each probability. (a)   Binomial Distribution: X ~ Bin(10, 0.7)     (i)            P(X = 2)     (ii)           P(X < 2)     (iii)          P(X > 2)     (iv)          P(1 < X < 7) (b) Normal Distribution: X ~ N(0, 4). Note that variance is 4 and, hence, the standard deviation is 2.     (i)             P(X < 3)     (ii)           P(X > 3)     (iii)          P(1 < X < 3) (c) Choose either (a) or (b) and interpret the results in...
In R: write a function that inputs a vector x and a number n and returns...
In R: write a function that inputs a vector x and a number n and returns the first n elements of x. When n is greater than length(x), your function should just return x. We are not allowed to use any control flow statements
Determine whether each statement is true or false, and prove or disprove, as appropriate. (a) (∀x∈R)(∃y∈R)[xy=1].(∀x∈R)(∃y∈R)[xy=1]....
Determine whether each statement is true or false, and prove or disprove, as appropriate. (a) (∀x∈R)(∃y∈R)[xy=1].(∀x∈R)(∃y∈R)[xy=1]. (b) (∃x∈R)(∀y∈R)[xy=1].(∃x∈R)(∀y∈R)[xy=1]. (c) (∃x∈R)(∀y∈R)[xy>0].(∃x∈R)(∀y∈R)[xy>0]. (d) (∀x∈R)(∃y∈R)[xy>0].(∀x∈R)(∃y∈R)[xy>0]. (e) (∀x∈R)(∃y∈R)(∀z∈R)[xy=xz].(∀x∈R)(∃y∈R)(∀z∈R)[xy=xz]. (f) (∃y∈R)(∀x∈R)(∃z∈R)[xy=xz].(∃y∈R)(∀x∈R)(∃z∈R)[xy=xz]. (g) (∀x∈Q)(∃y∈Z)[xy∈Z].(∀x∈Q)(∃y∈Z)[xy∈Z]. (h) (∃x∈Z+)(∀y∈Z+)[y≤x].(∃x∈Z+)(∀y∈Z+)[y≤x]. (i) (∀y∈Z+)(∃x∈Z+)[y≤x].(∀y∈Z+)(∃x∈Z+)[y≤x]. (j) (∀x,y∈Z)[x<y⇒(∃z∈Z)[x<z<y]].(∀x,y∈Z)[x<y⇒(∃z∈Z)[x<z<y]]. (k) (∀x,y∈Q)[x<y⇒(∃z∈Q)[x<z<y]].(∀x,y∈Q)[x<y⇒(∃z∈Q)[x<z<y]].
In C. Write a loop that subtracts 1 from each element in lowerScores. If the element...
In C. Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}.
Linear Algebra we know that x ∈ R^n is a nonzero vector and C is a...
Linear Algebra we know that x ∈ R^n is a nonzero vector and C is a real number. find all values of C such that ( In − Cxx^T ) is nonsingular and find its inverse knowing that its inverse is of the same form
Please use c++ Consider the code on the next page. It creates a vector of five...
Please use c++ Consider the code on the next page. It creates a vector of five strings of vegetable names and you want to make the vector contain only vegetable names that you like. In the spaces designated, perform the following: Declare an iterator for a vector of strings, move it to the position of 'Tomato', and erase it because it's not a vegetable. With the same iterator, use .begin() to point it to the beginning of the list, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT