Question Details

No question body available.

Tags

r dplyr tidyverse

Answers (3)

March 17, 2026 Score: 7 Rep: 134,653 Quality: High Completeness: 60%

I would do a join. I assume you can do that with the tidyverse but I recommend using package data.table. This apporach is efficient and scales well to large datasets.

library(data.table)

DT
March 17, 2026 Score: 3 Rep: 106,187 Quality: Medium Completeness: 60%

Maybe you can use reframe + tibble along with casewhen

# library(tidyverse)

library(dplyr) library(tibble)

diamonds %>% mutate(case
when( color %in% c('E','I') ~ tibble(kpia = "A", kpib = 1.1), color %in% c('J','H') ~ tibble(kpia = "B", kpib = 1.2), TRUE ~ tibble(kpia = "C", kpib = 2) ))

and you will obtain

# A tibble: 53,940 × 12
   carat cut       color clarity depth table price     x     y     z kpia kpib

1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 A 1.1 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 A 1.1 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 A 1.1 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63 A 1.1 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 B 1.2 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48 B 1.2 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47 A 1.1 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53 B 1.2 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49 A 1.1 10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39 B 1.2

ℹ 53,930 more rows

March 17, 2026 Score: 1 Rep: 273,704 Quality: Low Completeness: 50%

Assign strings containing both and then separate them.

library(dplyr) library(tidyr)

BOD %>% mutate( kp = casewhen( Time == 1 ~ "A 1.1", Time == 2 ~ "B 1.2", .default = "C 2" )) %>% separate(kp, c("kp1", "kp2"), sep = " ", convert = TRUE)

Time demand kp1 kp2
1 1 8.3 A 1.1
2 2 10.3 B 1.2
3 3 19.0 C 2.0
4 4 16.0 C 2.0
5 5 15.6 C 2.0
6 7 19.8 C 2.0

This variation also works:

library(dplyr) BOD %>% mutate( case
when( Time == 1 ~ "A 1.1", Time == 2 ~ "B 1.2", .default = "C 2" ) %>% read.table(text = ., col.names = c("kp1", "kp2")) )