Question

In R programming # given a numeric vector x, and a string vector 'labels' of the...

In R programming

# given a numeric vector x, and a string vector 'labels' of the
# same length as x, return the vector containing the labels that
# correspond to the k smallest values in vector x.
#
# example:
# if x      = c(3.1, 2.3, 0.1, 4.2) and
#    labels = c("a", "b", "c", "d") and
#    k = 2
# then smallest_labels(x, labels, k) should return c("c", "b")
smallest_labels = function(x, labels, k) {

# YOUR CODE HERE (2)


# HINT: R function 'order' is very handy for this

}

Homework Answers

Answer #1
  • Inside the function we could bind x, labels into a dataframe in a ordered manner usind order()
  • the second row and first k columns of the dataframe contains the k labels will smallest x, access these elements and return it.

function:

smallest_labels = function(x, labels, k) {

ii <- order(x, label);
smallest <- c(rbind(x,label)[,ii][2,1:k])
return(smallest)
}

sample output:

  • Let's test the function with sample data in the question

smallest_labels = function(x, labels, k) {

ii <- order(x, label);
smallest <- c(rbind(x,label)[,ii][2,1:k])
return(smallest)
}

x <- c(3.1, 2.3, 0.1, 4.2)
label <- c("a", "b", "c", "d")
k <- 2
print(smallest_labels(x, label, k))

output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C programming Write a function that takes in a 2D char array (string array) and return...
C programming Write a function that takes in a 2D char array (string array) and return a 1D char array with all the elements connected together Hint: strlen(-char*-) //returns the length of a string strcat(-char* accum-, something to add) //works like string+= in java
Given the following R code listing: x<-c(1,4,2,1,2,3,1,2,3,2,1,2,4,3,2,1,2,1,2,1) if variable x was a label that indicated the...
Given the following R code listing: x<-c(1,4,2,1,2,3,1,2,3,2,1,2,4,3,2,1,2,1,2,1) if variable x was a label that indicated the incoming classification, what would the correct data type to apply for visualization be? In other words, what should we make the x data type be? numeric, factor, string, logical? Please provide your reasoning as well as your selection.
(a) Consider binary codes determined by a parity-check matrix H. Let r be a vector of...
(a) Consider binary codes determined by a parity-check matrix H. Let r be a vector of received symbols. The syndrome of r happens to be a sum of some columns of H. What columns are these? Hint: They are determined by the errors occurring in transmissions. (b) Let G be a k × n generating matrix of a code C the form G = [I_k | B] , where Ik is the k × k identity matrix, and B is...
Determine whether the following sets define vector spaces over R: (a) A={x∈R:x=k^2,k∈R} (b) B={x∈R:x=k^2,k∈Z} (c) C...
Determine whether the following sets define vector spaces over R: (a) A={x∈R:x=k^2,k∈R} (b) B={x∈R:x=k^2,k∈Z} (c) C ={p∈P^2 :p=ax^2,a∈R} (d) D={z∈C:|z|=1} (e) E={z∈C:z=a+i,a∈R} (f) F ={p∈P^2 : d (p)∈R}
For any given string x=x1x2x3⋯xm, a hash function is defined below as: (This is the entire...
For any given string x=x1x2x3⋯xm, a hash function is defined below as: (This is the entire question, i cannot update it because this is it in its entirety) h(x)=[c(x1)+c(x2)q+c(x3)q^2+⋯+c(xm)q^m-1 mod P Here, both  and P are given small prime numbers, and denote the ASCII integer of the letter xi,1≤i≤m. For any string y=y1y2⋯yn, please show that if the hash value of the hash value yiyi+1⋯yi+k is known, then the hash value of yi+1yi+2⋯yi+k+1 can be computed in constant time. Please type...
particle of mass m in R3 has position function r(t) =<x(t),y(t),z(t)>. Given that the tangent vector...
particle of mass m in R3 has position function r(t) =<x(t),y(t),z(t)>. Given that the tangent vector r0(t) has a constant length of 5, please prove that at all t values, the force F(t) acting on the particle is orthogonal to the tangent vector
Find the domain of the vector function r (t) = <sent, lnt, 1 / (x-2)> a.(0,...
Find the domain of the vector function r (t) = <sent, lnt, 1 / (x-2)> a.(0, inf) b. (0, 2) U (2, inf) c. (-inf, 2) U (2, inf) d. (-inf, inf) e. another answer
Let V be a finite dimensional vector space over R with an inner product 〈x, y〉...
Let V be a finite dimensional vector space over R with an inner product 〈x, y〉 ∈ R for x, y ∈ V . (a) (3points) Let λ∈R with λ>0. Show that 〈x,y〉′ = λ〈x,y〉, for x,y ∈ V, (b) (2 points) Let T : V → V be a linear operator, such that 〈T(x),T(y)〉 = 〈x,y〉, for all x,y ∈ V. Show that T is one-to-one. (c) (2 points) Recall that the norm of a vector x ∈ V...
Evaluate the line integral ∫CF⋅dr, where F(x,y,z)=5xi+yj−2zk and C is given by the vector function r(t)=〈sint,cost,t〉,...
Evaluate the line integral ∫CF⋅dr, where F(x,y,z)=5xi+yj−2zk and C is given by the vector function r(t)=〈sint,cost,t〉, 0≤t≤3π/2.
In R studio please explain how to do the stemplot? A sample of 20 endangered species...
In R studio please explain how to do the stemplot? A sample of 20 endangered species was obtained and the length of time (in months) since being placed on the list was recorded for each species. A stemplot of these data follows. In the stemplot, 5|2 represents 52 months. 5   2 4 8 9 9 6   0 0 3 5 6 7 8 9 7   3 4 7 8 9 9 8 9   8 The median length of time (in...