我怎样才能取代连字符"细胞"在R数据框中用零?

时间:2014-07-14 20:29:37

标签: r dataframe plyr lapply

我有一个数据框,其中包含一些正数,一些负数,一些单词和一些连字符"单元格"在其中,如此:

Revenue                 73.88   74.76    78.02   78.19  68.74
Other Revenue - Total   -       -        -       -       -
Total Revenue           73.88   74.76    78.02   78.19  68.74
Cost of Revenue - Total 21.09   21.61    23.01   22.76  19.99
Gross Profit            52.80    -53.15  -55.01  55.43  48.75

我想将仅在倒数第二列中找到的连字符替换为0,但仅当连字符不在数字的开头时才更换。例如,我不想将负数转为正数。

我试过了:

df[-1] <- lapply(df[-1], function(x) as.numeric(gsub("-", 0, x)))

但是返回前一个数据框:

Revenue                 NA      NA       NA      NA     NA
Other Revenue - Total   0       0        0       0      0
Total Revenue           NA      NA       NA      NA     NA
Cost of Revenue - Total NA      NA       NA      NA     NA
Gross Profit            NA      NA       NA      NA     NA 

这是我绝对不想要的。我该如何解决这个问题?

感谢。

这是我调用str()时的输出:

str(income)
'data.frame':   49 obs. of  6 variables:
 $ Items  : Factor w/ 49 levels "Accounting Change",..: 44 40 47 7 23 45 43 9 29 49 ...
 $ Recent1: Factor w/ 14 levels "-","0.00","11,305.00",..: 4 1 4 11 14 6 5 1 1 1 ...
 $ Recent2: Factor w/ 16 levels "-","-29.00","0.00",..: 5 1 5 15 16 9 6 1 1 2 ...
 $ Recent3: Factor w/ 17 levels "-","0.00","11,449.00",..: 5 1 5 15 17 10 6 1 1 4 ...
 $ Recent4: Factor w/ 18 levels "-","-31.00","0.00",..: 6 1 6 15 17 9 4 1 1 18 ...
 $ Recent5: Factor w/ 14 levels "-","0.00","1,617.00",..: 4 1 4 10 13 5 3 1 1 1 ...

2 个答案:

答案 0 :(得分:1)

正如@Joe暗示的那样,data.frame列中的值必须属于同一类型,因此假设-位于与numeric相同的向量中{ {1}} s(52.80,21.09等等),每列被强制键入character(大概)。尝试使用gsub而不是"0" 0 bing,然后将列转换为numeric。由于您要将0强制转换为character列向量,因此它会将其余的向量元素强制转换为NA

DF <- data.frame(
  X1=c(12,45,67,"-",9),
  X2=c(34,45,56,"-",12))
str(DF)
'data.frame':   5 obs. of  2 variables:
 $ X1: chr  "12" "45" "67" "-" ...
 $ X2: chr  "34" "45" "56" "-" ...
##
DF2 <- DF
DF2$X1 <- gsub("-","0",DF2$X1)
DF2$X1 <- as.numeric(DF2$X1)
str(DF2)
'data.frame':   5 obs. of  2 variables:
 $ X1: num  12 45 67 0 9
 $ X2: chr  "34" "45" "56" "-" ...

编辑:要删除值中的逗号,

DF <- data.frame(
  X0=c("A","B","C","D"),
  X1=c("12,300.04","45.5","-","9,046.78"),
  X2=c("1,0001.12","33","-","12.6"))
for(j in 2:ncol(DF)){
  DF[,j] <- gsub(",","",as.character(DF[,j]))
  for(i in 1:nrow(DF)){
    if(nchar(DF[i,j])==1){
      DF[i,j] <- gsub("-","0",DF[i,j])
    } else {
      next
    }
  }
  DF[,j] <- as.numeric(DF[,j])
  DF[,j]
}

使用*apply函数和正则表达式有更有效的方法,但这应该有效。我必须考虑到你的一些值是负值的事实,所以假设其中只有一个-的单元格只有一个字符长,这应该修复它们而不影响其他单元格中的负值。

答案 1 :(得分:0)

假设它名为dat

  dat[2:6] <- lapply( dat[2:6], function(col) as.numeric( gsub("-$|\\,", "", col) ) )
  dat[is.na(dat)] <- 0

只替换字符串末尾的减号,删除逗号和gsub强制字符因素,这样您就不需要添加as.character。当我使用read.fwftextConnection导入数据时,我得到了尾随空格。您可以先使用gdata :: trim删除它们,但这有效:

lapply(dat[2:6], function(col) as.numeric( gsub("-[ ]*$|\\,", "", col ) ) ) # on RHS 

 dat<-read.fwf(textConnection("Revenue                 73.88   74.76    78.02   78.19  68.74
 Other Revenue - Total   -       -        -       -       -
 Total Revenue           73.88   74.76    78.02   78.19  68.74
 Cost of Revenue - Total 21.09   21.61    23.01   22.76  19.99
 Gross Profit            52.80    -53.15  -55.01  55.43  48.75"), widths=c(24, rep(8,5)))

 dat[2:6] <- lapply( dat[2:6], function(col) as.numeric( gsub("-$|\\,", "", col) ) )
 dat[is.na(dat)] <- 0
 dat
#----------
                        V1    V2     V3     V4    V5    V6
1 Revenue                  73.88  74.76  78.02 78.19 68.74
2 Other Revenue - Total     0.00   0.00   0.00  0.00  0.00
3 Total Revenue            73.88  74.76  78.02 78.19 68.74
4 Cost of Revenue - Total  21.09  21.61  23.01 22.76 19.99
5 Gross Profit             52.80 -53.15 -55.01 55.43 48.75