如何从R中的数据框中删除美元符号($)?

时间:2014-11-04 06:06:23

标签: r csv dataframe

我对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 ...

3 个答案:

答案 0 :(得分:5)

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

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

如果您需要numeric列,则可以删除,(由@David提供)  Arenburg)并按numeric

转换为as.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))

答案 1 :(得分:3)

如果你必须用这样的数据阅读很多csv文件,也许你应该考虑创建自己的as方法来与colClasses参数一起使用,如下所示:

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

在演示如何使用它之前,让我们将@ akrun的样本数据写入名为&#34; A&#34;的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

答案 2 :(得分:2)

再次阅读它可能会更有益,这次是readLines。我将akrun的数据写入文件&#34; data.text&#34;并在读取表格之前修复了字符串。不确定逗号是小数点还是令人讨厌的逗号,所以我选择了小数点。

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