【发布时间】:2018-02-05 03:06:57
【问题描述】:
使用R,如果是数字,如何删除逗号,如果是字母,如何用空格替换逗号?:
Company | Sales |
-------------------------
go, go, llc |2,550.40 |
tires & more | 500 |
l-m tech |1,000.67 |
样本数据:
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)
预期输出:
Company | Sales |
-----------------------
go go llc |2550.40 |
tires & more | 500 |
l-m tech |1000.67 |
我尝试过的:
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
【问题讨论】:
-
是不是就像
company列总是有字母和Sales列号一样? -
不,有些公司里面有数字
标签: r comma string-substitution