gsub在R中应用组合

时间:2015-03-19 16:40:45

标签: r

我正在尝试在数据帧的每一列上使用gsub来删除一些字符,我尝试使用apply来做到这一点但没有成功:

data<-apply(data,2, function(x) gsub("£","",data[x]))

返回错误

Error in `[.data.frame`(data, x) : undefined columns selected

我知道如果我这样做了

for(i in 1: length(data)){data[,i]<-gsub("£","",data[,i]) }

但是为什么申请呼叫不起作用?

1 个答案:

答案 0 :(得分:5)

这是下一个最可重复的例子。如果我认为有点困难,可能会有更好/更快(矢量化)的方式。但既然你要求申请:

# just turn it to characters in order to 
# turn . to , ... was just the first dataset that came to
# but as character should not be necessary for your data
ds[] <- sapply(mtcars,function(x) gsub("\\.",",",as.character(x)))