从字符串和数字中删除逗号

时间:2022-05-28 22:28:49

Using R, how do I remove commas if its a number and replace commas with a space if its a letter?:

使用R,如果它是一个数字,如何删除逗号,如果是一个字母,用空格替换逗号?:

  Company        | Sales   |
  -------------------------
  go, go, llc    |2,550.40 |
  tires & more   |  500    |
  l-m tech       |1,000.67 |

Sample data:

data = matrix(c('go, go,llc', 'tires & more', 'l-m technology',
 formatC(2550.40, format="f", big.mark=",", digits=2), 500, 
 formatC(1000.67, format="f", big.mark=",", digits=2)), 
 nrow=3, 
 ncol=2)

Expected output:

  Company      | Sales  |
  -----------------------
  go go llc    |2550.40 |
  tires & more |  500   |
  l-m tech     |1000.67 |

What I've tried:

我尝试过的:

data <- sapply(data, function(x){
           if (grepl("[[:punct:]]",x)){
              if (grepl("[[:digit:]]",x)){
                 x <- gsub(",","",x)
              }
              else{
                 x <- gsub(","," ",x)
              }
           }
        })

print(nrow(data)) # returns NULL

1 个解决方案

#1


4  

You can do this easily with a nested gsub:

您可以使用嵌套的gsub轻松完成此操作:

gsub(",", "", gsub("([a-zA-Z]),", "\\1 ", input)

The inner pattern matches a letter followed by a comma and replaces it with just the letter. The outer gsub replaces any remaining commas with spaces.

内部模式匹配一​​个字母后跟一个逗号,并用字母替换它。外部gsub用空格替换任何剩余的逗号。

Applying it to your matrix:

将其应用于您的矩阵:

    apply(data, 2, function(x) gsub(",", "", gsub("([a-zA-Z]),", "\\1 ", x)))
    #      [,1]             [,2]     
    # [1,] "go  go llc"     "2550.40"
    # [2,] "tires & more"   "500"    
    # [3,] "l-m technology" "1000.67"

#1


4  

You can do this easily with a nested gsub:

您可以使用嵌套的gsub轻松完成此操作:

gsub(",", "", gsub("([a-zA-Z]),", "\\1 ", input)

The inner pattern matches a letter followed by a comma and replaces it with just the letter. The outer gsub replaces any remaining commas with spaces.

内部模式匹配一​​个字母后跟一个逗号,并用字母替换它。外部gsub用空格替换任何剩余的逗号。

Applying it to your matrix:

将其应用于您的矩阵:

    apply(data, 2, function(x) gsub(",", "", gsub("([a-zA-Z]),", "\\1 ", x)))
    #      [,1]             [,2]     
    # [1,] "go  go llc"     "2550.40"
    # [2,] "tires & more"   "500"    
    # [3,] "l-m technology" "1000.67"