在dplyr中使用case_when更改新列时遇到问题

时间:2019-08-15 16:34:36

标签: r dplyr mutate case-when

我想使用this数据帧上Código列的不同长度值来改变新列。

[
    {
        "order_id": 4,
        "total_amount": "444.700000",
        "currency": "USD",
        "payment_lines": [
            {
                "item_number": 1100,
                "currency": "USD",
                "available_quantity": 1,
                "order_total_amount": 25.982012,

            },
            {
                "item_number": 1200,
                "currency": "USD",
                "available_quantity": 1,
                "order_total_amount": 224.982012,

            }
        ]
    },
    {
        "order_id": 4,
        "total_amount": "-25.982012",
        "external_reference_type": "MAG",
        "currency": "USD",
        "payment_lines": [
            {
                "item_number": 1100,
                "currency": "USD",
                "available_quantity": -1,
                "order_total_amount": -25.982012,

            }
        ]
    }
]

我已经尝试过在类似这样的mutate中使用case_when:

ods <- readODS::read_ods('http://www.arcotel.gob.ec/wp-content/uploads/2016/09/proyeccion_cantonal_total_2010-2020_seg%C3%BAn_INEC1.ods', skip = 2)

每当其值的长度为3时,应该使用Código的第一个数字创建一个新的Provincia列,否则应提取两个数字。运行上面的代码时,我只会得到不适用的

1 个答案:

答案 0 :(得分:1)

使用nchar,它将计算每次观察中的字符数:

ods <- mutate(ods, Provincia = case_when(
         nchar(ods$Código) == 3 ~ str_extract(ods$Código, '[[:digit:]]{1}'),
         nchar(ods$Código) == 4 ~ str_extract(ods$Código, '[[:digit:]]{2}')
 ))

结果:

    > ods %>% pull(Provincia)
  [1] "1"  "1"  "1"  "1"  "1"  "1"  "1"  "1"  "1"  "1"  "1"  "1"  "1"  "1"  "1"  "2"  "2"  "2"  "2"  "2"  "2" 
 [22] "2"  "3"  "3"  "3"  "3"  "3"  "3"  "3"  "4"  "4"  "4"  "4"  "4"  "4"  "5"  "5"  "5"  "5"  "5"  "5"  "5" 
 [43] "6"  "6"  "6"  "6"  "6"  "6"  "6"  "6"  "6"  "6"  "7"  "7"  "7"  "7"  "7"  "7"  "7"  "7"  "7"  "7"  "7" 
 [64] "7"  "7"  "7"  "8"  "8"  "8"  "8"  "8"  "8"  "8"  "8"  "9"  "9"  "9"  "9"  "9"  "9"  "9"  "9"  "9"  "9" 
 [85] "9"  "9"  "9"  "9"  "9"  "9"  "9"  "9"  "9"  "9"  "9"  "9"  "9"  "9"  "9"  "10" "10" "10" "10" "10" "10"
[106] "11" "11" "11" "11" "11" "11" "11" "11" "11" "11" "11" "11" "11" "11" "11" "11" "12" "12" "12" "12" "12"
[127] "12" "12" "12" "12" "12" "12" "12" "12" "13" "13" "13" "13" "13" "13" "13" "13" "13" "13" "13" "13" "13"
[148] "13" "13" "13" "13" "13" "13" "13" "13" "13" "14" "14" "14" "14" "14" "14" "14" "14" "14" "14" "14" "14"
[169] "15" "15" "15" "15" "15" "16" "16" "16" "16" "17" "17" "17" "17" "17" "17" "17" "17" "18" "18" "18" "18"
[190] "18" "18" "18" "18" "18" "19" "19" "19" "19" "19" "19" "19" "19" "19" "20" "20" "20" "21" "21" "21" "21"
[211] "21" "21" "21" "22" "22" "22" "22" "23" "24" "24" "24" "90" "90" "90"