4
$\begingroup$

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.

$\endgroup$
6
  • 1
    $\begingroup$ 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. $\endgroup$
    – hrbrmstr
    Commented May 26, 2018 at 2:01
  • $\begingroup$ @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. $\endgroup$
    – Stephen Rauch
    Commented May 26, 2018 at 2:04
  • 1
    $\begingroup$ I'm gd with only being a 1st class citizen on SO @StephenRauch :-) $\endgroup$
    – hrbrmstr
    Commented May 26, 2018 at 2:13
  • $\begingroup$ Are you familiar with regular expressions? $\endgroup$ Commented May 26, 2018 at 2:23
  • 1
    $\begingroup$ Why not just lapply(strsplit(gsub("\\*","",x),","),as.numeric)? $\endgroup$
    – r2evans
    Commented May 29, 2018 at 3:05

1 Answer 1

1
$\begingroup$

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)
    
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.