首页 > 解决方案 > How to add multiple numeric values in a single cell of a large list in R

问题描述

Hi I want to calculate the sum of product quantity against a shipment_id. I have dataframe with two columns.

    shipment_id    details
    XYQWE13        [{"orderid"=1,"quantity"=>2},{"orderid"=2,"quantity"=>1}]
    UYTDW54        [{"orderid"=1,"quantity"=>5},{"orderid"=2,"quantity"=>5}]
    SKFEF32        [{"orderid"=1,"quantity"=>2},{"orderid"=2,"quantity"=>1},{"orderid"=3,"quantity"=>5}]

So I will have to fetch the numeric value only after the quantity part. I tried this to extract quantity and the number against it

    y <- stringr::str_extract_all(string = raw_data_shipment2$details, pattern = '"quantity"=>[0-99]+')

Which gave me a large list

     Name           Type                   Value 
     y              list(3)                List of length 3
     [[1]]          character [1]          '"quantity"=>2''"quantity"=>1'
     [[2]]          character [1]          '"quantity"=>5''"quantity"=>5'
     [[2]]          character [1]          '"quantity"=>2''"quantity"=>1''"quantity"=>5'

Further using this

     y2 <- stringr::str_extract_all(string = y, pattern = '=>[0-99]+')
     y3 <- stringr::str_extract_all(string = y2, pattern = '[0-99]+')

Gave me this result of this large list

     Name           Type                   Value 
     y3             list(3)                List of length 3
     [[1]]          character [1]          '2''1'
     [[2]]          character [1]          '5''5'
     [[2]]          character [1]          '2''1''5' 

Now I wanted to sum the numeric part and was hoping I could cbind it with my original dataframe. But I'm stuck on how to sum it. Any help would be highly appreciated.

标签: rdataframe

解决方案


因为您正在使用stringr我将假设您正在使用其他tidyverse包 - 特别是purrr. 如果您是,您应该能够使用 map 函数对 y1、y2、y3 等进行求和:

z1 <- purrr::map(y1, ~sum(as.numeric(.)))
z2 <- purrr::map(y2, ~sum(as.numeric(.)))
z3 <- purrr::map(y3, ~sum(as.numeric(.)))

然后你可以绑定 z1、z2、z3 等。


推荐阅读