[R] Removing a dollar sign from a character vector
Jeff Newmiller
jdnewmil at dcn.davis.ca.us
Thu Feb 11 06:53:50 CET 2016
y <- as.numeric( gsub( "[$, ]", "", y ) )
--
Sent from my phone. Please excuse my brevity.
On February 10, 2016 9:39:16 PM PST, James Plante <jimplante at me.com> wrote:
>What I’ve got:
># sessionInfo()
>R version 3.2.3 (2015-12-10)
>Platform: x86_64-apple-darwin13.4.0 (64-bit)
>Running under: OS X 10.11.3 (El Capitan)
>
>locale:
>[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
>attached base packages:
>[1] stats graphics grDevices utils datasets methods base
>
>
>other attached packages:
>[1] XML_3.98-1.3 dplyr_0.4.3
>
>loaded via a namespace (and not attached):
>[1] magrittr_1.5 R6_2.1.2 assertthat_0.1
>rsconnect_0.4.1.4
>[5] parallel_3.2.3 DBI_0.3.1 tools_3.2.3 Rcpp_0.12.3
>
>
>> str(y) #toy vector, subset of larger vector in a dataframe of ~4,600
>rows.
>chr [1:5] "$1,000.00 " "$1,000.00 " "$1,000.00 " "$2,600.00 "
>"$2,600.00 “
>
>y is a subset of a column in a dataframe that’s too big to post. I
>tried the commands listed here on the dataframe and it didn’t work. So
>I’m using a small subset to find out where my error is. It’s being a
>PITA, and I’m trying to solve it. What I want is a vector of numbers:
>1000, 1000, 1000, 2600, 2,600.
>
>What I’ve tried:
>> y
>[1] "$1,000.00 " "$1,000.00 " "$1,000.00 " "$2,600.00 " "$2,600.00 "
>> gsub("$", "", y)
>[1] "$1,000.00 " "$1,000.00 " "$1,000.00 " "$2,600.00 " "$2,600.00 “ #
>no change. Why?
>> gsub(".00", "", y) # note: that’s dot zero zero, replace with “"
>[1] "$10 " "$10 " "$10 " "$2, " "$2, “ #WTF?
>
>I’ve also tried sapply and apply, but haven’t yet tried a loop. (These
>were done in desperation; gsub ought to work the way the help says.)
>I’ve tried lots more than is listed here, over and over, with no
>results. I’d be grateful for any guidance you can provide.
>
>Thanks in advance,
>
>Jim Plante
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help
mailing list