0

I am using a file that relies on user input. Of course this input is inconsistent and I am not sure what the best way to deal with this? I am working with numbers, which are I have read in as character:

mydata$output1
    [1] "PP150" NA "50,376.45\r\n" "40096.21" "43721.59" NA NA "27282272.16" NA  

"50,376.45\r\n" actually shows up as a simple number in the preview, i have (in my limited experience) never seen anything similiar. How can i create a numeric vector out of this?

I want to get:

[1] "PP150" NA "50376.45" "40096.21" "43721.59" NA NA "27282272.16" NA
Waldi
  • 39,242
  • 6
  • 30
  • 78
Nneka
  • 1,764
  • 2
  • 15
  • 39

1 Answers1

2

You can use gsub to remove characters that you don't want :

x <- c("PP150",NA, "50,376.45\r\n","40096.21","43721.59",NA,NA,"27282272.16", NA)

gsub('[,\r\n]', '', x)
#[1] "PP150"   NA  "50376.45" "40096.21" "43721.59"  NA  NA   "27282272.16" NA

The final output would still be a string since "PP150" is not a number.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213