1

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.

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? Any suggestions?

M--
  • 25,431
  • 8
  • 61
  • 93
Amy Chen
  • 25
  • 4
  • `Manuf[] <- lapply(Manuf, gsub, pattern = '[$%]', replacement ="")`? – Wiktor Stribiżew Feb 10 '19 at 20:37
  • 1
    Sorry, I figure out what went wrong, I left out [] after database, the correct code should be database[] <-lapply (database, gsub, pattern, replacement) – Amy Chen Feb 10 '19 at 21:10
  • Thank you @WiktorStribiżew!! – Amy Chen Feb 10 '19 at 21:10
  • Glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see [How to upvote on Stack Overflow?](http://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow)) as you are entitled to the upvoting privilege after reaching 15 rep points. Note you may upvote all the answers that turned out helpful. – Wiktor Stribiżew Feb 12 '19 at 15:50

1 Answers1

0

You may use

Manuf[] <- lapply(Manuf, gsub, pattern = '[$%]', replacement ="") 

The [$%] pattern will remove both $ and % symbols from the data frame.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563