Ayush Raj Singh
Ayush Raj Singh

Reputation: 873

How to scale a numeric vector in 1 or 2 steps in R?

I have a numeric vector as below:-

num <- c(1.2,2.3,2.4,4.6,1.43,8.99,7.12,6.77,......)

This vector is quite a big vector.

What I want to do is to replace all values between 1 and 2 with 1, values between 2 and 3 with 2 and so on.

num_scaled <- c(1,2,2,4,1,8,7,6,.....)

Is there any simpler way to do this in R? A function?

Upvotes: 1

Views: 125

Answers (1)

user1981275
user1981275

Reputation: 13372

num <- c(1.2,2.3,2.4,4.6,1.43,8.99,7.12,6.77)
num_scaled <- trunc(num)

gives you:

> num_scaled
[1] 1 2 2 4 1 8 7 6

Upvotes: 1

Related Questions