Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Write an R program to replace all negative numbers in a vector with zeros.

Easy

Problem Statement

Write an R program to replace all negative numbers in a vector with zeros.

Sample Input

Vector: c(-5, 3, -2, 7, -1)

Sample Output

[1] 0 3 0 7 0

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
# Predefined structure vector <- c(-5, 3, -2, 7, -1) vector[vector < 0] <- 0 print(vector)
Log in or sign up to run or submit your code.Log inSign up
Input
Vector: c(-5, 3, -2, 7, -1)
Expected Output
[1] 0 3 0 7 0