2017-06-30 8 views
0

Big Cartelには、注文をcsvファイルにエクスポートするオプションがあります。しかし、構造は私が必要とする分析にはあまり適していません。BigCartel csvファイルのRをdataframeまたはdata.tableのlong形式に分割する

Big Cartelのcsvオーダーのダウンロードの列と行のサブセットです(現在の問題にはあまり重要ではない他の列があります)。

Number, Buyer name,Items,Item count,Item total,Total price,Total shipping,Total tax,Total discount 
1,jim,product_name:Plate|product_option_name:Red|quantity:1|price:9.99|total:9.99,1,9.99,11.98,1.99,0,0 
2,bill,product_name:Plate|product_option_name:Green|quantity:1|price:9.99|total:9.99;product_name:Plate|product_option_name:Blue|quantity:1|price:9.99|total:9.99,2,19.98,22.98,3,0,0 
3,jane,product_name:Plate|product_option_name:Red|quantity:1|price:6.99|total:6.99;product_name:Thingy|product_option_name:|quantity:1|price:9.99|total:9.99;product_name:Mug|product_option_name:Grey|quantity:1|price:10.99|total:10.99;product_name:Cup|product_option_name:Grey|quantity:1|price:9.99|total:9.99;product_name:Saucer|product_option_name:Grey|quantity:1|price:9.99|total:9.99;product_name:Stopper|product_option_name:|quantity:1|price:9.99|total:9.99,6,57.94,64.94,7,0,0 
4,dale,product_name:Plate|product_option_name:Green|quantity:1|price:10.99|total:10.99,1,10.99,13.99,4.99,0,1.99 

項目列には、セミコロン(;)をセパレータとして複数の「行項目」を含めることができます。各「ラインアイテム」には、パイプ(|)で区切られた5つの属性、つまりproduct_name、product_option_name、quantity、priceおよびtotal(すなわち、ライン)があります。 「品目数」の列には、「明細」の数と(注文)合計価格、出荷、税金および割引の列が表示されます。分析のために、私は船積み、税金および割引が「商品アイテム」として扱われる次の長いフォーマットのデータを希望します。

tstrsplitを使用して
Number Buyer name line-item product_option_name quantity price total 
1  jim  Plate  Red     1  9.99 9.99 
1  jim  shipping       1  1.99 1.99 
1  jim  tax        0  0  0 
1  jim  discount       0  0  0 
2  bill  Plate  Green    1  9.99 9.99 
2  bill  Plate  Blue    1  9.99 9.99 
2  bill  shipping       1  3  3 
2  bill  tax        0  0  0 
2  bill  discount       0  0  0 
3  jane  Plate  Red     1  6.99 6.99 
3  jane  Thingy       1  9.99 9.99 
3  jane  Mug   Grey    1  10.99 10.99 
3  jane  Cup   Grey    1  9.99 9.99 
3  jane  Saucer  Grey    1  9.99 9.99 
3  jane  Stopper       1  9.99 9.99 
3  jane  shipping       1  7  7 
3  jane  tax        0  0  0 
3  jane  discount       0  0  0 
4  dale  Plate  Green    1  10.99 10.99 
4  dale  shipping       1  4.99 4.99 
4  dale  tax        0  0  
4  dale  discount       0  -1.99 -1.99 

()Rから:data.tableとcsplitは()Rから:splitstackshapeが解決策になるようだが、私は、構文の権利を取得することはできません。私はまた、tidyverse/dplyr関数を分離/散布してみましたが、私が何をしても必要な出力を得ることはできません。

私は、すべてのSOの質問を通して検索してきました。近いうちに解決策がいくつかありますが、どれも「長い」よりも広いフォーマットを想定しています。

答えて

0

このようなものは、あなたが探しているものを得ることがあります。

library(dplyr) 
library(tidyr) 
library(stringr) 

filepath <- # Path to datafile here 

df <- read.csv(filepath, stringsAsFactors = FALSE) 

cols <- paste0("col", 1:(max(str_count(df$Items, ";")) + 1)) 

df <- df %>% 
     separate(col = Items, into = cols, sep = ";", fill = "right") %>% 
     gather_("column", "details", cols, na.rm = TRUE) %>% 
     select(-column) %>% 
     separate(col = details, into = c("product_name", "product_option_name","quantity","price","total"), sep = "\\|", fill = "right") %>% 
     mutate(product_name = sub("^.*\\:", "", product_name), 
      product_option_name = sub("^.*\\:", "", product_option_name), 
      quantity = sub("^.*\\:", "", quantity), 
      price = sub("^.*\\:", "", price), 
      total = sub("^.*\\:", "", total)) %>% 
     gather("line", "item", c(Total.shipping, Total.discount, Total.tax, product_name)) %>% 
     mutate(product_option_name = ifelse(line == "product_name" & product_option_name != "", product_option_name, NA), 
      line_item = ifelse(line == "product_name", item, sub("^.*\\.","", line)), 
      price = ifelse(line == "product_name", price, item), 
      price = ifelse(line_item == "discount", as.numeric(price) * (-1), price), 
      quantity = ifelse(line_item %in% c("shipping","discount","tax") & price == "0", 0, quantity), 
      total = as.numeric(price) * as.numeric(quantity)) %>% 
     distinct() %>% 
     select(Number, Buyer.name, line_item, product_option_name, quantity, price, total) %>% 
     arrange(Number) 
+0

本当に素晴らしいです。これを見ていただきありがとうございます。私は私が持っているより大きいデータセットでそれを試してみるつもりです。 – Martyn

関連する問題