将货币字符串值重新将新变量作为数字
首先 - 与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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个助手函数
f
,然后将其应用于资金
列:输出:
输入:
You could create a helper function
f
, and then apply it to thefunds
column:Output:
Input:
这有效,但我敢肯定存在更好的解决方案。假设
资金
是一个字符向量:This works but I'm sure better solutions exist. Assuming
funds
is a character vector: