# how to remove unwanted characters from data

I need to write a function which identifies and removes the "*" character after some numeric values in a vector. I also need that the resultant vector is a numeric vector.

c("21,34,99*", "56,90*", "45*")


I need to remove "*" which is unwanted.

I want to write function so whenever such data cleaning requirement I can use function and pass certain parameters.

• i tried to edit your post but my 50K plus rep on SO-proper counts for naught here. Anyway, consider deleting this and posting it to SO-proper with what you've tried. Right now, you're asking for someone to write code for you so you can take credit for it where you work or school and that's not cool. May 26 '18 at 2:01
• @hrbrmstr, I approved your edit, but you are right in that this question belongs on SO. FYI, you can still flag for closure at your current rep level. May 26 '18 at 2:04
• I'm gd with only being a 1st class citizen on SO @StephenRauch :-) May 26 '18 at 2:13
• Are you familiar with regular expressions? May 26 '18 at 2:23
• Why not just lapply(strsplit(gsub("\\*","",x),","),as.numeric)? May 29 '18 at 3:05

Should have been posted in SO as others rightly pointed out.

Simple solution would be to:

1. Split on '*' and select the first part of the split.

2. Apply (1) over list items and return a vector.

x <- c("21,34,99*", "56,90*", "45*")               #test case

data_clean <- function(x) sapply (strsplit(x , '[*]' ), [ , 1)
data_clean(x)