Hi,
I have a data frame with strings that have two letters and four numbers. When I
store a whole row as a new vector and try to remove the preceding letters using
the gsub command, it returns characters of single numbers that have no relation
to the numbers in each string. I also noticed that when I view the new vector
before using gsub, it includes the original headers from the data frame. For
example,
The original row will contain (i'm not showing the headers):
MT2342 MT0982 MT2874
and after I use the command,
'gsub('[^0-9]','',PthwyGenes),' I get:
"6" "6" "8"
and this result no longer has any headers.
Does anyone know why this happens and how I can fix it?
Thanks,
-Nina
Dear Nina,
Is this what you want?
x=c("MT2342","MT0982","MT2874")
substr(x,3,nchar(x))
HTH,
Jorge
On Wed, Jul 2, 2008 at 10:24 AM, <naw3@duke.edu> wrote:
> Hi,
>
> I have a data frame with strings that have two letters and four numbers.
> When I
> store a whole row as a new vector and try to remove the preceding letters
> using
> the gsub command, it returns characters of single numbers that have no
> relation
> to the numbers in each string. I also noticed that when I view the new
> vector
> before using gsub, it includes the original headers from the data frame.
> For
> example,
>
> The original row will contain (i'm not showing the headers):
>
> MT2342 MT0982 MT2874
>
> and after I use the command,
'gsub('[^0-9]','',PthwyGenes),' I get:
>
> "6" "6" "8"
>
> and this result no longer has any headers.
>
> Does anyone know why this happens and how I can fix it?
>
> Thanks,
> -Nina
>
> ______________________________________________
> R-help@r-project.org mailing list
> 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]]
Seems to work fine for me:> x <- c("MT2342", "MT0982", "MT2874") > gsub("[^0-9]", "", x)[1] "2342" "0982" "2874">You might have 'factors' so you should use as.character to convert to character strings: gsub('[^0-9]','',as.character(PthwyGenes)) On Wed, Jul 2, 2008 at 10:24 AM, <naw3 at duke.edu> wrote:> Hi, > > I have a data frame with strings that have two letters and four numbers. When I > store a whole row as a new vector and try to remove the preceding letters using > the gsub command, it returns characters of single numbers that have no relation > to the numbers in each string. I also noticed that when I view the new vector > before using gsub, it includes the original headers from the data frame. For > example, > > The original row will contain (i'm not showing the headers): > > MT2342 MT0982 MT2874 > > and after I use the command, 'gsub('[^0-9]','',PthwyGenes),' I get: > > "6" "6" "8" > > and this result no longer has any headers. > > Does anyone know why this happens and how I can fix it? > > Thanks, > -Nina > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?