Using R: Write a function called positives that accepts a numeric vector. It ret
ID: 3817828 • Letter: U
Question
Using R: Write a function called positives that accepts a numeric vector. It returns a logical vector where positive numbers in the numeric vector result in TRUE, and zero or negative numbers in the numeric vector result in FALSE. For example, positives(c(1,0,-2,4)) returns TRUE FALSE FALSE TRUE.Using R: Write a function called positives that accepts a numeric vector. It returns a logical vector where positive numbers in the numeric vector result in TRUE, and zero or negative numbers in the numeric vector result in FALSE. For example, positives(c(1,0,-2,4)) returns TRUE FALSE FALSE TRUE.
Using R: Write a function called positives that accepts a numeric vector. It returns a logical vector where positive numbers in the numeric vector result in TRUE, and zero or negative numbers in the numeric vector result in FALSE. For example, positives(c(1,0,-2,4)) returns TRUE FALSE FALSE TRUE.
Explanation / Answer
Here is the code:
positives <- function(arr){ # function named positives which takes arr as argument
vec <- vector(,length(arr)) # declaring a vector named vec which is the logical vector to be returned. It is #initialized as false and its size is equal to that of arr.
for(i in arr){ # iterating arr
if(i>0) # the required >0 condition
vec[i]="TRUE"
}
return(vec) # returning the new vector
}
temp<-c(1,0,-2,4) # vector for which the function is called
ans <- positives(temp) # calling the function
print(ans) # printing the output
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.