0

I have a column within a dataframe where some values are like this

   Col1
   Y 183.21
   500.23

   432.89
   Y 428.29


   Y500

I am looking for a way to remove only those Y prior to those strings that have Y and some characters separated by a space ( Y 183.21, Y 428.29 ) . Not the Y that is not separated by the space (Y500) but only Ys that are separated by space ( Y 183.21, Y 428.29 ). The expected output would be

   Col1
   183.21
   500.23

   432.89
   428.29


   Y500

I tried some examples but unsuccessful. Any advice or tips are much appricated.

Community
  • 1
  • 1
Kim Jenkins
  • 438
  • 3
  • 17
  • It looks like you have two variables in one. I'd extract the `Y`s into a new column (maybe `grepl('Y\\d', df$Col1)`), and parse to an actual number with `readr::parse_number(df$Col1)`. Or you could get creative with `tidyr::separate` and lookarounds. – alistaire Dec 09 '16 at 04:33

2 Answers2

2

We can use package stringi

library(stringi)

new.df<-stri_replace_all(df,"" ,fixed = "Y " )
MFR
  • 2,049
  • 3
  • 29
  • 53
1

We can use sub assuming you have only one match

sub("Y ", "", df$Col1)

#[1] "183.21" "500.23" "432.89" "428.29" "Y500"  
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213