新列中字母的货币符号

Currency symbol to letters in new column

如何将货币符号与数字分开,并在 R 中这样的数据框中创建一个用 EUR、USD、YEN 等替换符号的新列:

> a
  prices
1 0.00
2 200.00€
3 75.40¥
4 £51.98
5 154.00 EUR
6 59.00 USD

所需的输出应如下所示:

> a
  prices    currency
1 100.00    USD
2 200.00    EUR
3 75.40     JPY
4 51.98     GBP
5 154.00    EUR
6 59.00     USD


提前致谢!

使用tidyverse 函数。没有 built-in 转换器,所以恐怕您必须手动进行货币 symbol/name 转换(您可以使用 case_when 进行转换)。

library(tidyverse)
dat %>% 
  mutate(value = parse_number(prices),
         text = gsub("[0-9.]+", "", prices), 
         text = case_when(text == "$" ~ "USD", 
                          text == "€" ~ "EUR",
                          text == "¥" ~ "YEN", 
                          text == "£" ~ "GBP",
                          T ~ text))

输出

      prices  value text
1    0.00 100.00  USD
2    200.00€ 200.00  EUR
3     75.40¥  75.40  YEN
4     £51.98  51.98  GBP
5 154.00 EUR 154.00  EUR
6  59.00 USD  59.00  USD

数据

dat <- read.table(header = T, text = "  prices
1 0.00
2 200.00€
3 75.40¥
4 £51.98
5 '154.00 EUR'
6 '59.00 USD'")

添加到 Maël 的回答中,但有正则表达式的 stringr 解决方案,但我还提出了一个处理小写字母的案例。

dat %>% mutate(numbers = str_extract(prices, "[0-9.]+"),
               currency = str_replace(prices, numbers, ""),
               currency = case_when(currency == "$" ~ "USD", 
                                    currency == "€" ~ "EUR",
                                    currency == "¥" ~ "YEN", 
                                    currency == "£" ~ "GBP",
                                    TRUE ~ toupper(currency)))