Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

dplyr - How do I mutate over a set of variations for each row in R?

I have the following data:

| parent_sku |    sku   | stock_status | regular_price | tax_class | 
|:----------:|:--------:|:------------:|:-------------:|:---------:|
| ABBBOA01   | ABBBOA01 | instock      | 1299          | parent    | 
| ABBBOA03   | ABBBOA03 | instock      | 1299          | parent    | 
| ABBBOA02   | ABBBOA02 | instock      | 1299          | parent    | 
| ABBBOA04   | ABBBOA04 | instock      | 1299          | parent    | 

I would like to mutate four variations for each parent_sku, M,L, XL and XXL. For example, the above data should be transformed to:

| parent_sku |    sku   | stock_status | regular_price | tax_class |    attribute_size   |
|:----------:|:--------:|:------------:|:-------------:|:---------:|:-------------------:|
| ABBBOA01   | ABBBOA01 | instock      | 1299          | parent    | M                   |
| ABBBOA01   | ABBBOA01 | instock      | 1299          | parent    | X                   |
| ABBBOA01   | ABBBOA01 | instock      | 1299          | parent    | XL                  |
| ABBBOA01   | ABBBOA01 | instock      | 1299          | parent    | XXL                 |
| ABBBOA03   | ABBBOA03 | instock      | 1299          | parent    | M                   |
| ABBBOA03   | ABBBOA03 | instock      | 1299          | parent    | L                   |
| ABBBOA03   | ABBBOA03 | instock      | 1299          | parent    | XL                  |
...
and so on

I have tried doing something like this:

data %>% group_by(parent_sku) %>%
  rowwise() %>% 
  mutate(attribute_size = 'M') %>%
  mutate(attribute_size = 'L') %>%
  mutate(attribute_size = 'XL') %>%
  mutate(attribute_size = 'XXL')

But this doesn't work.

question from:https://stackoverflow.com/questions/65938808/how-do-i-mutate-over-a-set-of-variations-for-each-row-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One way to do this is to add it all of the sizes in as a list column containing the vector of sizes. Then use unnest_longer() to expand it out into rows.

library(dplyr)
library(tidyr)

# this should work with all the columns
data <- tibble(parent_sku = paste0("ABBBOA0", 1:4),
               stock_status = rep("instock", 4))

sizes <- c("M", "L", "XL", "XXL")

data %>% 
  mutate(attribute_size = list(sizes)) %>% 
  unnest_longer(attribute_size)

Which gives you the expansion...

# A tibble: 16 x 3
   parent_sku stock_status attribute_size
   <chr>      <chr>        <chr>         
 1 ABBBOA01   instock      M             
 2 ABBBOA01   instock      L             
 3 ABBBOA01   instock      XL            
 4 ABBBOA01   instock      XXL           
 5 ABBBOA02   instock      M             
 6 ABBBOA02   instock      L             
 7 ABBBOA02   instock      XL            
 8 ABBBOA02   instock      XXL           
 9 ABBBOA03   instock      M             
10 ABBBOA03   instock      L             
11 ABBBOA03   instock      XL            
12 ABBBOA03   instock      XXL           
13 ABBBOA04   instock      M             
14 ABBBOA04   instock      L             
15 ABBBOA04   instock      XL            
16 ABBBOA04   instock      XXL  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...