[英]Replace special character with lapply and gsub in rstudio
I am trying to clean up my data by applying the following codes:我正在尝试通过应用以下代码来清理我的数据:
Manuf <- lapply(Manuf, gsub, pattern ='%', replacement ='')
Manuf <- lapply(Manuf, gsub, pattern='\\$', replacement ='')
I noticed the moment I applied the code, it turned my data into values.我注意到我应用代码的那一刻,它把我的数据变成了值。 (from data with 366 observation with 14 variables, to values of list of 14).This caused a problem when I applied this code to designate columns to from characters to numeric.
(从具有 14 个变量的 366 个观察值的数据到 14 个列表的值)。当我应用此代码将列指定为从字符到数字时,这导致了问题。
Manuf[, c(4:7,13:14)] <- sapply(Manuf[, c(4:7,13:14)], as.numeric)
It returned an error of "incorrect number of dimensions" How do I avoid my database from changing to List when replacing the character?它返回了“维数不正确”的错误,如何在替换字符时避免我的数据库更改为 List? Any suggestions?
有什么建议?
You may use您可以使用
Manuf[] <- lapply(Manuf, gsub, pattern = '[$%]', replacement ="")
The [$%]
pattern will remove both $
and %
symbols from the data frame. [$%]
模式将从数据框中删除$
和%
符号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.