【问题标题】:How can I strip dollar signs ($) from a data frame in R?如何从 R 中的数据框中去除美元符号 ($)?
【发布时间】:2014-12-30 23:10:33
【问题描述】:

我对 R 很陌生,并且正在与一个看似极其简单的查询作斗争。

我已使用 read.csv 将 csv 文件导入 R,并尝试在整理数据和进一步分析之前删除美元符号 ($)(美元符号对图表造成严重破坏)。

我一直在尝试使用 dplyr 和 gsub 从数据框中删除 $ ,但我非常感谢一些关于如何去做的建议。

我的数据框如下所示:

> str(data)
 'data.frame':  50 obs. of  17 variables:
 $ Year            : int  1 2 3 4 5 6 7 8 9 10 ...
 $ Prog.Cost       : Factor w/ 2 levels "-$3,333","$0": 1 2 2 2 2 2 2 2 2 2 ...
 $ Total.Benefits  : Factor w/ 44 levels "$2,155","$2,418",..: 25 5 7 11 12 10 9 14 13 8 ...
 $ Net.Cash.Flow   : Factor w/ 45 levels "-$2,825","$2,155",..: 1 6 8 12 13 11 10 15 14 9 ...
 $ Participant     : Factor w/ 46 levels "$0","$109","$123",..: 1 1 1 45 46 2 3 4 5 6 ...
 $ Taxpayer        : Factor w/ 48 levels "$113","$114",..: 19 32 35 37 38 40 41 45 48 47 ...
 $ Others          : Factor w/ 47 levels "-$9","$1,026",..: 12 25 26 24 23 11 9 10 8 7 ...
 $ Indirect        : Factor w/ 42 levels "-$1,626","-$2",..: 1 6 10 18 22 24 28 33 36 35 ...
 $ Crime           : Factor w/ 35 levels "$0","$1","$10",..: 6 11 13 19 21 23 28 31 33 32 ...
 $ Child.Welfare   : Factor w/ 1 level "$0": 1 1 1 1 1 1 1 1 1 1 ...
 $ Education       : Factor w/ 1 level "$0": 1 1 1 1 1 1 1 1 1 1 ...
 $ Health.Care     : Factor w/ 38 levels "-$10","-$11",..: 7 7 7 7 2 8 12 36 30 9 ...
 $ Welfare         : Factor w/ 1 level "$0": 1 1 1 1 1 1 1 1 1 1 ...
 $ Earnings        : Factor w/ 41 levels "$0","$101","$104",..: 1 1 1 22 23 24 25 26 27 28 ...
 $ State.Benefits  : Factor w/ 37 levels "$102","$117",..: 37 1 3 4 6 10 12 18 24 27 ...
 $ Local.Benefits  : Factor w/ 24 levels "$115","$136",..: 24 1 2 12 14 16 19 22 23 21 ...
 $ Federal.Benefits: Factor w/ 39 levels "$0","$100","$102",..: 1 1 1 12 12 17 20 19 19 21 ...

【问题讨论】:

  • 剥离$后的列类不清楚。对我来说好像numeric

标签: r csv dataframe


【解决方案1】:

如果您只需要删除$ 而不想更改列的class

indx <- sapply(data, is.factor) 
data[indx] <- lapply(data[indx], function(x) 
                            as.factor(gsub("\\$", "", x)))

如果您需要numeric 列,您也可以去掉,(由@David 提供 Arenburg)并通过as.numeric转换为numeric

data[indx] <- lapply(data[indx], function(x) as.numeric(gsub("[,$]", "", x)))

你可以把它包装在一个函数中

f1 <- function(dat, pat="[$]", Class="factor"){
  indx <- sapply(dat, is.factor)
  if(Class=="factor"){
  dat[indx] <- lapply(dat[indx], function(x) as.factor(gsub(pat, "", x)))
     }
  else {
  dat[indx] <- lapply(dat[indx], function(x) as.numeric(gsub(pat, "", x)))
   }
  dat
 }

 f1(data)
 f1(data, pat="[,$]", "numeric")

数据

set.seed(24)
data <- data.frame(Year=1:6, Prog.Cost= sample(c("-$3,3333", "$0"),
          6, replace=TRUE), Total.Benefits= sample(c("$2,155","$2,418",
         "$2,312"), 6, replace=TRUE))

【讨论】:

  • @David Arenburg 谢谢,我试图向 OP 询问有关数字部分的问题。
  • @David Arenburg,谢谢,我会添加它作为替代。
  • 如果将其详细说明为 sprintf 的反转,那将非常酷,因此可以指定格式,并且该函数将创建适当的正则表达式以退出未格式化的数值。跨度>
  • @akrun,David Arenburg - 非常感谢您的建议。我正在寻找数字列,您的解决方案非常有效。干杯。
  • +1,但是对于这样的情况,我通常喜欢在读取数据之前尝试解决问题......
【解决方案2】:

如果您必须读取大量包含此类数据的 csv 文件,也许您应该考虑创建自己的 as 方法以与 colClasses 参数一起使用,如下所示:

setClass("dollar")
setAs("character", "dollar",
      function(from) 
        as.numeric(gsub("[,$]", "", from, fixed = FALSE)))

在演示如何使用它之前,让我们将@akrun 的示例数据写入一个名为“A”的 csv 文件。在您直接读取文件的实际用例中,这不是必需的......

## write @akrun's sample data to a csv file named "A"
set.seed(24)
data <- data.frame(
  Year=1:6, 
  Prog.Cost= sample(c("-$3,3333", "$0"), 6, replace = TRUE), 
  Total.Benefits = sample(c("$2,155","$2,418","$2,312"), 6, replace=TRUE))

A <- tempfile()
write.csv(data, A, row.names = FALSE)

现在,colClasses 有了一个新选项,可以与 read.csv 一起使用 :-)

read.csv(A, colClasses = c("numeric", "dollar", "dollar"))
#   Year Prog.Cost Total.Benefits
# 1    1    -33333           2155
# 2    2    -33333           2312
# 3    3         0           2312
# 4    4         0           2155
# 5    5         0           2418
# 6    6         0           2418

【讨论】:

  • (+1) 你为什么在这里使用gsub 两次?
  • @DavidArenburg,午餐后食物昏迷的答案? :-)
  • @DavidArenburg,但是,另一方面,我想知道从长远来看,两个gsubs 与fixed = TRUE 是否比使用[,$] 作为fixed = FALSE 的模式更快或更慢....
【解决方案3】:

再读一遍可能会更有益,这次是readLines。我将 akrun 的数据写入文件“data.text”并在读取表格之前修复了字符串。不确定逗号是小数点还是烦人的逗号,所以我选择了小数点。

r <- gsub("[$]", "", readLines("data.txt"))
read.table(text = r, dec = ",")
#   Year Prog.Cost Total.Benefits
# 1    1   -3.3333          2.155
# 2    2   -3.3333          2.312
# 3    3    0.0000          2.312
# 4    4    0.0000          2.155
# 5    5    0.0000          2.418
# 6    6    0.0000          2.418

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-11
    • 2021-02-20
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 2022-11-30
    相关资源
    最近更新 更多