将货币字符串值重新将新变量作为数字

发布于 2025-02-12 12:14:30 字数 729 浏览 0 评论 0原文

首先 - 与R相关的新手与我同在。我正在尝试将字符串值重新编码为数字。我的问题是我的价值中有两种不同的弦模式:“ M”和“ B”分别为'百万'和'十亿'。

df <- (funds = c($1.76M, $2B, $57M, $9.87B)

我已经成功地敲掉了美元符号,现在有:

df <- (funds = c($1.76M, $2B, $57M, $9.87B),
       fundsR = c(1.76M, 2B, 57M, 9.87B)
       )

如何在保留各自的货币价值的同时将其重新描述为数字?我已经尝试使用各种if语句,用于循环,有或没有str_detect,管道操作员,case_when,stutate等。将值和“ m”和“ b”值分离,转换为数字并乘以乘数值值 - 在新列中。看似简单的任务事实证明并不像我想象的那样简单,我将其归因于新手。在这一点上,我想从头开始,看看是否有人有新的想法。我的rstudio是一团糟。

这样的事情会很好:

df <- (funds = c($1.76M, $2B, $57M, $9.87B),
       fundsR = c(1.76M, 2B, 57M, 9.87B),
       fundsFinal = c(1760000, 2000000000, 57000000, 9870000000)
       )

我真的很感谢您的意见。

First off - newbie with R so bear with me. I'm trying to recode string values as numeric. My problem is I have two different string patterns present in my values: "M" and "B" for 'million' and 'billion', respectively.

df <- (funds = c($1.76M, $2B, $57M, $9.87B)

I've successfully knocked off the dollar sign and now have:

df <- (funds = c($1.76M, $2B, $57M, $9.87B),
       fundsR = c(1.76M, 2B, 57M, 9.87B)
       )

How can I recode these as numeric while retaining their respective monetary values? I've tried using various if statements, for loops, with or without str_detect, pipe operators, case_when, mutate, etc. to isolate values with "M" and values with "B", convert to numeric and multiply to come up the complimentary numeric value--all in a new column. This seemingly simple task turned out not as simple as I imagined it would be and I'd attribute it to being a novice. At this point I'd like to start from scratch and see if anyone has any fresh ideas. My Rstudio is a MESS.

Something like this would be nice:

df <- (funds = c($1.76M, $2B, $57M, $9.87B),
       fundsR = c(1.76M, 2B, 57M, 9.87B),
       fundsFinal = c(1760000, 2000000000, 57000000, 9870000000)
       )

I'd really appreciate your input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦年海沫深 2025-02-19 12:14:30

您可以创建一个助手函数f,然后将其应用于资金列:


library(dplyr)
library(stringr)

f <- function(x) {
  curr = c("M"=1e6, "B" = 1e9)
  val = str_remove(x,"\\$")
  as.numeric(str_remove_all(val,"B|M"))*curr[str_extract(val, "B|M")]
}

df %>% mutate(fundsFinal = f(funds))

输出:

   funds fundsFinal
1 $1.76M   1.76e+06
2    $2B   2.00e+09
3   $57M   5.70e+07
4 $9.87B   9.87e+09

输入:

df = structure(list(funds = c("$1.76M", "$2B", "$57M", "$9.87B")), class = "data.frame", row.names = c(NA, 
-4L))

You could create a helper function f, and then apply it to the funds column:


library(dplyr)
library(stringr)

f <- function(x) {
  curr = c("M"=1e6, "B" = 1e9)
  val = str_remove(x,"\\
quot;)
  as.numeric(str_remove_all(val,"B|M"))*curr[str_extract(val, "B|M")]
}

df %>% mutate(fundsFinal = f(funds))

Output:

   funds fundsFinal
1 $1.76M   1.76e+06
2    $2B   2.00e+09
3   $57M   5.70e+07
4 $9.87B   9.87e+09

Input:

df = structure(list(funds = c("$1.76M", "$2B", "$57M", "$9.87B")), class = "data.frame", row.names = c(NA, 
-4L))
对岸观火 2025-02-19 12:14:30

这有效,但我敢肯定存在更好的解决方案。假设资金是一个字符向量:

library(tidyverse)
options(scipen = 999)
df <- data.frame(funds = c('$1.76M', '$2B', '$57M', '$9.87B'))


df = df %>%
  mutate( fundsFinal = ifelse(str_sub(funds,nchar(funds),-1) =='M',
                          as.numeric(substr(funds, 2, nchar(funds) - 1))*10^6,
                          as.numeric(substr(funds, 2, nchar(funds) - 1))*10^9))

This works but I'm sure better solutions exist. Assuming funds is a character vector:

library(tidyverse)
options(scipen = 999)
df <- data.frame(funds = c('$1.76M', '$2B', '$57M', '$9.87B'))


df = df %>%
  mutate( fundsFinal = ifelse(str_sub(funds,nchar(funds),-1) =='M',
                          as.numeric(substr(funds, 2, nchar(funds) - 1))*10^6,
                          as.numeric(substr(funds, 2, nchar(funds) - 1))*10^9))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文