0

Let's say I have a data frame with a vector:

column3
1
3
5
5
4
5
10
5

Now I want to create an entirely new vector that goes something like:

If value in column3 < 3, then value in new vector is "Small". If value in column3 >3 and <5, then value in new vector is "Medium".

I tried nested ifelse and it didn't create a new vector, it only tested one value. Example:

newcolumn <- ifelse(as.numeric(data$column3) < 3,"Small",  ifelse(as.numeric(data$column3) > 3 && as.numeric(data$column3) <5, "Medium"))
user6472523
  • 211
  • 3
  • 8

3 Answers3

0
library(dplyr)

data <- data.frame(column3=c(1,3,5,5,4,5,10,5))

newcolumn <- case_when(data$column3 < 3 ~ "Small", data$column3 < 5 ~ "Medium", TRUE ~ "Big")

[1] "Small"  "Medium" "Big"    "Big"    "Medium" "Big"    "Big"    "Big"  

Here's an example using dplyr case_when. What is the result when value is bigger than 5?

Using nested if else:

newcolumn <- ifelse(as.numeric(data$column3) < 3,"Small",  ifelse(as.numeric(data$column3) <5, "Medium", "Big"))
thc
  • 9,527
  • 1
  • 24
  • 39
0

A clearer way to do this is to use base::cut.

The typical use is:

temp <- c(1L, 3L, 5L, 5L, 4L, 5L, 10L, 5L)
# four cutpoints define three buckets
cut(temp, c(-Inf, 3, 5, Inf))
[1] (-Inf,3] (-Inf,3] (3,5]    (3,5]    (3,5]    (3,5]    (5, Inf] (3,5]   
Levels: (-Inf,3] (3,5] (5, Inf]

The returned vector is a factor, which can be useful. A further improvement is to make it an ordered factor, and to rename the labels like you wanted:

cut(temp, c(-Inf, 3, 5, Inf), labels = c("Small", "Med", "Large"), ordered_result = T) 
[1] Small Small Med   Med   Med   Med   Large Med  
Levels: Small < Med < Large
Brian
  • 7,900
  • 1
  • 27
  • 41
0

Did you meant small if column3<=3?

column3 <- c(1,3,5,5,4,5,10,5)
newcolumn <- rep(NA, length(column3))
newcolumn[column3<=3] <- "Small"
newcolumn[3<column3 & column3<=5] <- "Medium"
newcolumn[5<column3] <- "Large"
> newcolumn
[1] "Small"  "Small"  "Medium" "Medium" "Medium"
[6] "Medium" "Large"  "Medium"
Alex Jang
  • 21
  • 2