Re: [R] converting string vector to integer/numeric vector

2010-09-15 Thread Uwe Ligges



On 05.09.2010 14:48, rajesh j wrote:

Hi,

Is it possible to convert a string vector to integer or numeric vector? In
my situation I receive data in a string vector and have to convert it based
on a given type.


?as.interger ?as.numeric

Uwe Ligges

__
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.


Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread rajesh j
great! Thanks

On Sun, Sep 5, 2010 at 9:31 PM, Joshua Wiley  wrote:

> On Sun, Sep 5, 2010 at 8:36 AM, rajesh j  wrote:
> > No, the examples dont cover it...if i have
> > dat <- list(INT = c("1","2","3"),
> > NUM = c("2.34","4.56","6.78"),
> > INT = c("4", "5", "6"),
> > NUM = c("3.44"))
> >
> > I need to do a which on INT[0],NUM[0],INT[0] and NUM[0]...ie the [0]
> index
> > of all the vectors...
> > and test if they hold a value
>
> Ah, alright in which case:
>
> lapply(dat, function(x) {x[1] == "4"})
>
> This lapply()s the little test x[1] == "4" to each element in the list
> 'dat'.  I am using the [1] index because there is no [0] in R.
>
> >
> > On Sun, Sep 5, 2010 at 9:03 PM, Joshua Wiley 
> wrote:
> >>
> >> On Sun, Sep 5, 2010 at 8:16 AM, rajesh j 
> wrote:
> >> > Hi,
> >> > in a way similar to names(dat), can I address a particular index of
> >> > every
> >> > vector?
> >> > like [0] of all the vectors? So that I could do something like,
> >> > ints<-which(=="x")
> >>
> >> Not sure I follow you exactly, but you can use which() with any
> >> logical test.  Take a look at these examples, are any of them what you
> >> mean?
> >>
> >> example(which)
> >>
> >> If so (or even before) it would be worth it to read through
> >>
> >> ?which
> >>
> >> >
> >> > On Sun, Sep 5, 2010 at 8:34 PM, Joshua Wiley 
> >> > wrote:
> >> >>
> >> >> Hi Rajesh,
> >> >>
> >> >> This will work, unfortunately it seems like lapply() drops the names
> >> >> before it passes each element of the list which lead to my clumsy
> work
> >> >> around using which().  I'm sure there are other ways.
> >> >>
> >> >> dat <- list(INT = c("1","2","3"),
> >> >>NUM = c("2.34","4.56","6.78"),
> >> >>INT = c("4", "5", "6"),
> >> >>NUM = c("3.44"))
> >> >>
> >> >> ints <- which(names(dat)=="INT")
> >> >> nums <- which(names(dat)=="NUM")
> >> >>
> >> >> dat[ints] <- lapply(dat[ints], as.integer)
> >> >> dat[nums] <- lapply(dat[nums], as.numeric)
> >> >>
> >> >> str(dat)
> >> >>
> >> >> Cheers,
> >> >>
> >> >> Josh
> >> >>
> >> >> On Sun, Sep 5, 2010 at 6:33 AM, rajesh j 
> >> >> wrote:
> >> >> > The string vector actually comes as a part of a list, and the
> vector
> >> >> > is
> >> >> > named "int", and the numbers are strings. I then have to make it a
> >> >> > vector
> >> >> > that is still called "int" and has 4,5,6 etc. the types are either
> >> >> > integer
> >> >> > or numeric. The number of items in the vector is unknown.
> >> >> >
> >> >> > here's an example,
> >> >> >
> >> >> > a list has vectors
> >> >> >
> >> >> > INT
> >> >> > "2"
> >> >> > "3"
> >> >> > "4"
> >> >> >
> >> >> > NUM
> >> >> > "2.37"
> >> >> > "4.56"
> >> >> >
> >> >> >
> >> >> >
> >> >> > On Sun, Sep 5, 2010 at 6:56 PM, David Winsemius
> >> >> > wrote:
> >> >> >
> >> >> >>
> >> >> >> On Sep 5, 2010, at 9:22 AM, rajesh j wrote:
> >> >> >>
> >> >> >> for e.g., I get the following as a string vector
> >> >> >> "int" "4" "5" "6"
> >> >> >> after reading the first element, I have to convert this to a
> integer
> >> >> >> vector
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >> But what is the right answer?  And what number of items are
> possble
> >> >> >> per
> >> >> >> line?  And what are the other possible type identifiers? We need
> an
> >> >> >> example
> >> >> >> that has enough complexity to allow testing.
> >> >> >>
> >> >> >> --
> >> >> >> David.
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >> On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius
> >> >> >> wrote:
> >> >> >>
> >> >> >>>
> >> >> >>> On Sep 5, 2010, at 8:48 AM, rajesh j wrote:
> >> >> >>>
> >> >> >>>  Hi,
> >> >> 
> >> >>  Is it possible to convert a string vector to integer or numeric
> >> >>  vector?
> >> >>  In
> >> >>  my situation I receive data in a string vector and have to
> convert
> >> >>  it
> >> >>  based
> >> >>  on a given type.
> >> >> 
> >> >> >>>
> >> >> >>> Can you give an example? I don't understand either what sort of
> >> >> >>> conversion
> >> >> >>> you desire or what you mean by "convert it based on a given
> type."
> >> >> >>>
> >> >> >>> There are a couple of function you may want to consider but I am
> >> >> >>> having
> >> >> >>> difficulty convincing myself they answer the problem posed:
> >> >> >>>
> >> >> >>> ?charToRaw
> >> >> >>> ?stroi
> >> >> >>>
> >> >> >>> > strtoi(charToRaw("123 this is a string"), base=16)   # convert
> to
> >> >> >>> decimal ASCII
> >> >> >>>  [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115
> >> >> >>> 116
> >> >> >>> 114
> >> >> >>> 105 110 103
> >> >> >>>
> >> >> >>> --
> >> >> >>>
> >> >> >>> David Winsemius, MD
> >> >> >>> West Hartford, CT
> >> >> >>>
> >> >> >>>
> >> >> >>
> >> >> >>
> >> >> >> --
> >> >> >> Rajesh.J
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>  David Winsemius, MD
> >> >> >> West Hartford, CT
> >> >> >>
> >> >> >>
> >> >> >
> >> >> >
> >> >> > --
> >> >> > Rajesh.J
> >> >> >
> >> >> >[[alternative HTML version 

Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread Joshua Wiley
On Sun, Sep 5, 2010 at 8:36 AM, rajesh j  wrote:
> No, the examples dont cover it...if i have
> dat <- list(INT = c("1","2","3"),
>             NUM = c("2.34","4.56","6.78"),
>             INT = c("4", "5", "6"),
>             NUM = c("3.44"))
>
> I need to do a which on INT[0],NUM[0],INT[0] and NUM[0]...ie the [0] index
> of all the vectors...
> and test if they hold a value

Ah, alright in which case:

lapply(dat, function(x) {x[1] == "4"})

This lapply()s the little test x[1] == "4" to each element in the list
'dat'.  I am using the [1] index because there is no [0] in R.

>
> On Sun, Sep 5, 2010 at 9:03 PM, Joshua Wiley  wrote:
>>
>> On Sun, Sep 5, 2010 at 8:16 AM, rajesh j  wrote:
>> > Hi,
>> > in a way similar to names(dat), can I address a particular index of
>> > every
>> > vector?
>> > like [0] of all the vectors? So that I could do something like,
>> > ints<-which(=="x")
>>
>> Not sure I follow you exactly, but you can use which() with any
>> logical test.  Take a look at these examples, are any of them what you
>> mean?
>>
>> example(which)
>>
>> If so (or even before) it would be worth it to read through
>>
>> ?which
>>
>> >
>> > On Sun, Sep 5, 2010 at 8:34 PM, Joshua Wiley 
>> > wrote:
>> >>
>> >> Hi Rajesh,
>> >>
>> >> This will work, unfortunately it seems like lapply() drops the names
>> >> before it passes each element of the list which lead to my clumsy work
>> >> around using which().  I'm sure there are other ways.
>> >>
>> >> dat <- list(INT = c("1","2","3"),
>> >>            NUM = c("2.34","4.56","6.78"),
>> >>            INT = c("4", "5", "6"),
>> >>            NUM = c("3.44"))
>> >>
>> >> ints <- which(names(dat)=="INT")
>> >> nums <- which(names(dat)=="NUM")
>> >>
>> >> dat[ints] <- lapply(dat[ints], as.integer)
>> >> dat[nums] <- lapply(dat[nums], as.numeric)
>> >>
>> >> str(dat)
>> >>
>> >> Cheers,
>> >>
>> >> Josh
>> >>
>> >> On Sun, Sep 5, 2010 at 6:33 AM, rajesh j 
>> >> wrote:
>> >> > The string vector actually comes as a part of a list, and the vector
>> >> > is
>> >> > named "int", and the numbers are strings. I then have to make it a
>> >> > vector
>> >> > that is still called "int" and has 4,5,6 etc. the types are either
>> >> > integer
>> >> > or numeric. The number of items in the vector is unknown.
>> >> >
>> >> > here's an example,
>> >> >
>> >> > a list has vectors
>> >> >
>> >> > INT
>> >> > "2"
>> >> > "3"
>> >> > "4"
>> >> >
>> >> > NUM
>> >> > "2.37"
>> >> > "4.56"
>> >> >
>> >> >
>> >> >
>> >> > On Sun, Sep 5, 2010 at 6:56 PM, David Winsemius
>> >> > wrote:
>> >> >
>> >> >>
>> >> >> On Sep 5, 2010, at 9:22 AM, rajesh j wrote:
>> >> >>
>> >> >> for e.g., I get the following as a string vector
>> >> >> "int" "4" "5" "6"
>> >> >> after reading the first element, I have to convert this to a integer
>> >> >> vector
>> >> >>
>> >> >>
>> >> >>
>> >> >> But what is the right answer?  And what number of items are possble
>> >> >> per
>> >> >> line?  And what are the other possible type identifiers? We need an
>> >> >> example
>> >> >> that has enough complexity to allow testing.
>> >> >>
>> >> >> --
>> >> >> David.
>> >> >>
>> >> >>
>> >> >>
>> >> >> On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius
>> >> >> wrote:
>> >> >>
>> >> >>>
>> >> >>> On Sep 5, 2010, at 8:48 AM, rajesh j wrote:
>> >> >>>
>> >> >>>  Hi,
>> >> 
>> >>  Is it possible to convert a string vector to integer or numeric
>> >>  vector?
>> >>  In
>> >>  my situation I receive data in a string vector and have to convert
>> >>  it
>> >>  based
>> >>  on a given type.
>> >> 
>> >> >>>
>> >> >>> Can you give an example? I don't understand either what sort of
>> >> >>> conversion
>> >> >>> you desire or what you mean by "convert it based on a given type."
>> >> >>>
>> >> >>> There are a couple of function you may want to consider but I am
>> >> >>> having
>> >> >>> difficulty convincing myself they answer the problem posed:
>> >> >>>
>> >> >>> ?charToRaw
>> >> >>> ?stroi
>> >> >>>
>> >> >>> > strtoi(charToRaw("123 this is a string"), base=16)   # convert to
>> >> >>> decimal ASCII
>> >> >>>  [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115
>> >> >>> 116
>> >> >>> 114
>> >> >>> 105 110 103
>> >> >>>
>> >> >>> --
>> >> >>>
>> >> >>> David Winsemius, MD
>> >> >>> West Hartford, CT
>> >> >>>
>> >> >>>
>> >> >>
>> >> >>
>> >> >> --
>> >> >> Rajesh.J
>> >> >>
>> >> >>
>> >> >>
>> >> >>  David Winsemius, MD
>> >> >> West Hartford, CT
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >> > --
>> >> > Rajesh.J
>> >> >
>> >> >        [[alternative HTML version deleted]]
>> >> >
>> >> > __
>> >> > 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.
>> >> >
>> >>
>> >>
>> >>
>> >> --
>> >> Joshua Wiley
>> >> Ph.D. Student, Health Psychology
>> >

Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread rajesh j
Hi,
in a way similar to names(dat), can I address a particular index of every
vector?
like [0] of all the vectors? So that I could do something like,
ints<-which(=="x")

On Sun, Sep 5, 2010 at 8:34 PM, Joshua Wiley  wrote:

> Hi Rajesh,
>
> This will work, unfortunately it seems like lapply() drops the names
> before it passes each element of the list which lead to my clumsy work
> around using which().  I'm sure there are other ways.
>
> dat <- list(INT = c("1","2","3"),
>NUM = c("2.34","4.56","6.78"),
>INT = c("4", "5", "6"),
>NUM = c("3.44"))
>
> ints <- which(names(dat)=="INT")
> nums <- which(names(dat)=="NUM")
>
> dat[ints] <- lapply(dat[ints], as.integer)
> dat[nums] <- lapply(dat[nums], as.numeric)
>
> str(dat)
>
> Cheers,
>
> Josh
>
> On Sun, Sep 5, 2010 at 6:33 AM, rajesh j  wrote:
> > The string vector actually comes as a part of a list, and the vector is
> > named "int", and the numbers are strings. I then have to make it a vector
> > that is still called "int" and has 4,5,6 etc. the types are either
> integer
> > or numeric. The number of items in the vector is unknown.
> >
> > here's an example,
> >
> > a list has vectors
> >
> > INT
> > "2"
> > "3"
> > "4"
> >
> > NUM
> > "2.37"
> > "4.56"
> >
> >
> >
> > On Sun, Sep 5, 2010 at 6:56 PM, David Winsemius  >wrote:
> >
> >>
> >> On Sep 5, 2010, at 9:22 AM, rajesh j wrote:
> >>
> >> for e.g., I get the following as a string vector
> >> "int" "4" "5" "6"
> >> after reading the first element, I have to convert this to a integer
> vector
> >>
> >>
> >>
> >> But what is the right answer?  And what number of items are possble per
> >> line?  And what are the other possible type identifiers? We need an
> example
> >> that has enough complexity to allow testing.
> >>
> >> --
> >> David.
> >>
> >>
> >>
> >> On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius  >wrote:
> >>
> >>>
> >>> On Sep 5, 2010, at 8:48 AM, rajesh j wrote:
> >>>
> >>>  Hi,
> 
>  Is it possible to convert a string vector to integer or numeric
> vector?
>  In
>  my situation I receive data in a string vector and have to convert it
>  based
>  on a given type.
> 
> >>>
> >>> Can you give an example? I don't understand either what sort of
> conversion
> >>> you desire or what you mean by "convert it based on a given type."
> >>>
> >>> There are a couple of function you may want to consider but I am having
> >>> difficulty convincing myself they answer the problem posed:
> >>>
> >>> ?charToRaw
> >>> ?stroi
> >>>
> >>> > strtoi(charToRaw("123 this is a string"), base=16)   # convert to
> >>> decimal ASCII
> >>>  [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115 116
> 114
> >>> 105 110 103
> >>>
> >>> --
> >>>
> >>> David Winsemius, MD
> >>> West Hartford, CT
> >>>
> >>>
> >>
> >>
> >> --
> >> Rajesh.J
> >>
> >>
> >>
> >>  David Winsemius, MD
> >> West Hartford, CT
> >>
> >>
> >
> >
> > --
> > Rajesh.J
> >
> >[[alternative HTML version deleted]]
> >
> > __
> > 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.
> >
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> http://www.joshuawiley.com/
>



-- 
Rajesh.J

[[alternative HTML version deleted]]

__
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.


Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread rajesh j
this should work great. thanks

On Sun, Sep 5, 2010 at 8:34 PM, Joshua Wiley  wrote:

> Hi Rajesh,
>
> This will work, unfortunately it seems like lapply() drops the names
> before it passes each element of the list which lead to my clumsy work
> around using which().  I'm sure there are other ways.
>
> dat <- list(INT = c("1","2","3"),
>NUM = c("2.34","4.56","6.78"),
>INT = c("4", "5", "6"),
>NUM = c("3.44"))
>
> ints <- which(names(dat)=="INT")
> nums <- which(names(dat)=="NUM")
>
> dat[ints] <- lapply(dat[ints], as.integer)
> dat[nums] <- lapply(dat[nums], as.numeric)
>
> str(dat)
>
> Cheers,
>
> Josh
>
> On Sun, Sep 5, 2010 at 6:33 AM, rajesh j  wrote:
> > The string vector actually comes as a part of a list, and the vector is
> > named "int", and the numbers are strings. I then have to make it a vector
> > that is still called "int" and has 4,5,6 etc. the types are either
> integer
> > or numeric. The number of items in the vector is unknown.
> >
> > here's an example,
> >
> > a list has vectors
> >
> > INT
> > "2"
> > "3"
> > "4"
> >
> > NUM
> > "2.37"
> > "4.56"
> >
> >
> >
> > On Sun, Sep 5, 2010 at 6:56 PM, David Winsemius  >wrote:
> >
> >>
> >> On Sep 5, 2010, at 9:22 AM, rajesh j wrote:
> >>
> >> for e.g., I get the following as a string vector
> >> "int" "4" "5" "6"
> >> after reading the first element, I have to convert this to a integer
> vector
> >>
> >>
> >>
> >> But what is the right answer?  And what number of items are possble per
> >> line?  And what are the other possible type identifiers? We need an
> example
> >> that has enough complexity to allow testing.
> >>
> >> --
> >> David.
> >>
> >>
> >>
> >> On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius  >wrote:
> >>
> >>>
> >>> On Sep 5, 2010, at 8:48 AM, rajesh j wrote:
> >>>
> >>>  Hi,
> 
>  Is it possible to convert a string vector to integer or numeric
> vector?
>  In
>  my situation I receive data in a string vector and have to convert it
>  based
>  on a given type.
> 
> >>>
> >>> Can you give an example? I don't understand either what sort of
> conversion
> >>> you desire or what you mean by "convert it based on a given type."
> >>>
> >>> There are a couple of function you may want to consider but I am having
> >>> difficulty convincing myself they answer the problem posed:
> >>>
> >>> ?charToRaw
> >>> ?stroi
> >>>
> >>> > strtoi(charToRaw("123 this is a string"), base=16)   # convert to
> >>> decimal ASCII
> >>>  [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115 116
> 114
> >>> 105 110 103
> >>>
> >>> --
> >>>
> >>> David Winsemius, MD
> >>> West Hartford, CT
> >>>
> >>>
> >>
> >>
> >> --
> >> Rajesh.J
> >>
> >>
> >>
> >>  David Winsemius, MD
> >> West Hartford, CT
> >>
> >>
> >
> >
> > --
> > Rajesh.J
> >
> >[[alternative HTML version deleted]]
> >
> > __
> > 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.
> >
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> http://www.joshuawiley.com/
>



-- 
Rajesh.J

[[alternative HTML version deleted]]

__
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.


Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread rajesh j
No, the examples dont cover it...if i have
dat <- list(INT = c("1","2","3"),
NUM = c("2.34","4.56","6.78"),
INT = c("4", "5", "6"),
NUM = c("3.44"))

I need to do a which on INT[0],NUM[0],INT[0] and NUM[0]...ie the [0] index
of all the vectors...
and test if they hold a value

On Sun, Sep 5, 2010 at 9:03 PM, Joshua Wiley  wrote:

> On Sun, Sep 5, 2010 at 8:16 AM, rajesh j  wrote:
> > Hi,
> > in a way similar to names(dat), can I address a particular index of every
> > vector?
> > like [0] of all the vectors? So that I could do something like,
> > ints<-which(=="x")
>
> Not sure I follow you exactly, but you can use which() with any
> logical test.  Take a look at these examples, are any of them what you
> mean?
>
> example(which)
>
> If so (or even before) it would be worth it to read through
>
> ?which
>
> >
> > On Sun, Sep 5, 2010 at 8:34 PM, Joshua Wiley 
> wrote:
> >>
> >> Hi Rajesh,
> >>
> >> This will work, unfortunately it seems like lapply() drops the names
> >> before it passes each element of the list which lead to my clumsy work
> >> around using which().  I'm sure there are other ways.
> >>
> >> dat <- list(INT = c("1","2","3"),
> >>NUM = c("2.34","4.56","6.78"),
> >>INT = c("4", "5", "6"),
> >>NUM = c("3.44"))
> >>
> >> ints <- which(names(dat)=="INT")
> >> nums <- which(names(dat)=="NUM")
> >>
> >> dat[ints] <- lapply(dat[ints], as.integer)
> >> dat[nums] <- lapply(dat[nums], as.numeric)
> >>
> >> str(dat)
> >>
> >> Cheers,
> >>
> >> Josh
> >>
> >> On Sun, Sep 5, 2010 at 6:33 AM, rajesh j 
> wrote:
> >> > The string vector actually comes as a part of a list, and the vector
> is
> >> > named "int", and the numbers are strings. I then have to make it a
> >> > vector
> >> > that is still called "int" and has 4,5,6 etc. the types are either
> >> > integer
> >> > or numeric. The number of items in the vector is unknown.
> >> >
> >> > here's an example,
> >> >
> >> > a list has vectors
> >> >
> >> > INT
> >> > "2"
> >> > "3"
> >> > "4"
> >> >
> >> > NUM
> >> > "2.37"
> >> > "4.56"
> >> >
> >> >
> >> >
> >> > On Sun, Sep 5, 2010 at 6:56 PM, David Winsemius
> >> > wrote:
> >> >
> >> >>
> >> >> On Sep 5, 2010, at 9:22 AM, rajesh j wrote:
> >> >>
> >> >> for e.g., I get the following as a string vector
> >> >> "int" "4" "5" "6"
> >> >> after reading the first element, I have to convert this to a integer
> >> >> vector
> >> >>
> >> >>
> >> >>
> >> >> But what is the right answer?  And what number of items are possble
> per
> >> >> line?  And what are the other possible type identifiers? We need an
> >> >> example
> >> >> that has enough complexity to allow testing.
> >> >>
> >> >> --
> >> >> David.
> >> >>
> >> >>
> >> >>
> >> >> On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius
> >> >> wrote:
> >> >>
> >> >>>
> >> >>> On Sep 5, 2010, at 8:48 AM, rajesh j wrote:
> >> >>>
> >> >>>  Hi,
> >> 
> >>  Is it possible to convert a string vector to integer or numeric
> >>  vector?
> >>  In
> >>  my situation I receive data in a string vector and have to convert
> it
> >>  based
> >>  on a given type.
> >> 
> >> >>>
> >> >>> Can you give an example? I don't understand either what sort of
> >> >>> conversion
> >> >>> you desire or what you mean by "convert it based on a given type."
> >> >>>
> >> >>> There are a couple of function you may want to consider but I am
> >> >>> having
> >> >>> difficulty convincing myself they answer the problem posed:
> >> >>>
> >> >>> ?charToRaw
> >> >>> ?stroi
> >> >>>
> >> >>> > strtoi(charToRaw("123 this is a string"), base=16)   # convert to
> >> >>> decimal ASCII
> >> >>>  [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115 116
> >> >>> 114
> >> >>> 105 110 103
> >> >>>
> >> >>> --
> >> >>>
> >> >>> David Winsemius, MD
> >> >>> West Hartford, CT
> >> >>>
> >> >>>
> >> >>
> >> >>
> >> >> --
> >> >> Rajesh.J
> >> >>
> >> >>
> >> >>
> >> >>  David Winsemius, MD
> >> >> West Hartford, CT
> >> >>
> >> >>
> >> >
> >> >
> >> > --
> >> > Rajesh.J
> >> >
> >> >[[alternative HTML version deleted]]
> >> >
> >> > __
> >> > 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.
> >> >
> >>
> >>
> >>
> >> --
> >> Joshua Wiley
> >> Ph.D. Student, Health Psychology
> >> University of California, Los Angeles
> >> http://www.joshuawiley.com/
> >
> >
> >
> > --
> > Rajesh.J
> >
> >
> >
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> http://www.joshuawiley.com/
>



-- 
Rajesh.J

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://

Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread Joshua Wiley
On Sun, Sep 5, 2010 at 8:16 AM, rajesh j  wrote:
> Hi,
> in a way similar to names(dat), can I address a particular index of every
> vector?
> like [0] of all the vectors? So that I could do something like,
> ints<-which(=="x")

Not sure I follow you exactly, but you can use which() with any
logical test.  Take a look at these examples, are any of them what you
mean?

example(which)

If so (or even before) it would be worth it to read through

?which

>
> On Sun, Sep 5, 2010 at 8:34 PM, Joshua Wiley  wrote:
>>
>> Hi Rajesh,
>>
>> This will work, unfortunately it seems like lapply() drops the names
>> before it passes each element of the list which lead to my clumsy work
>> around using which().  I'm sure there are other ways.
>>
>> dat <- list(INT = c("1","2","3"),
>>            NUM = c("2.34","4.56","6.78"),
>>            INT = c("4", "5", "6"),
>>            NUM = c("3.44"))
>>
>> ints <- which(names(dat)=="INT")
>> nums <- which(names(dat)=="NUM")
>>
>> dat[ints] <- lapply(dat[ints], as.integer)
>> dat[nums] <- lapply(dat[nums], as.numeric)
>>
>> str(dat)
>>
>> Cheers,
>>
>> Josh
>>
>> On Sun, Sep 5, 2010 at 6:33 AM, rajesh j  wrote:
>> > The string vector actually comes as a part of a list, and the vector is
>> > named "int", and the numbers are strings. I then have to make it a
>> > vector
>> > that is still called "int" and has 4,5,6 etc. the types are either
>> > integer
>> > or numeric. The number of items in the vector is unknown.
>> >
>> > here's an example,
>> >
>> > a list has vectors
>> >
>> > INT
>> > "2"
>> > "3"
>> > "4"
>> >
>> > NUM
>> > "2.37"
>> > "4.56"
>> >
>> >
>> >
>> > On Sun, Sep 5, 2010 at 6:56 PM, David Winsemius
>> > wrote:
>> >
>> >>
>> >> On Sep 5, 2010, at 9:22 AM, rajesh j wrote:
>> >>
>> >> for e.g., I get the following as a string vector
>> >> "int" "4" "5" "6"
>> >> after reading the first element, I have to convert this to a integer
>> >> vector
>> >>
>> >>
>> >>
>> >> But what is the right answer?  And what number of items are possble per
>> >> line?  And what are the other possible type identifiers? We need an
>> >> example
>> >> that has enough complexity to allow testing.
>> >>
>> >> --
>> >> David.
>> >>
>> >>
>> >>
>> >> On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius
>> >> wrote:
>> >>
>> >>>
>> >>> On Sep 5, 2010, at 8:48 AM, rajesh j wrote:
>> >>>
>> >>>  Hi,
>> 
>>  Is it possible to convert a string vector to integer or numeric
>>  vector?
>>  In
>>  my situation I receive data in a string vector and have to convert it
>>  based
>>  on a given type.
>> 
>> >>>
>> >>> Can you give an example? I don't understand either what sort of
>> >>> conversion
>> >>> you desire or what you mean by "convert it based on a given type."
>> >>>
>> >>> There are a couple of function you may want to consider but I am
>> >>> having
>> >>> difficulty convincing myself they answer the problem posed:
>> >>>
>> >>> ?charToRaw
>> >>> ?stroi
>> >>>
>> >>> > strtoi(charToRaw("123 this is a string"), base=16)   # convert to
>> >>> decimal ASCII
>> >>>  [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115 116
>> >>> 114
>> >>> 105 110 103
>> >>>
>> >>> --
>> >>>
>> >>> David Winsemius, MD
>> >>> West Hartford, CT
>> >>>
>> >>>
>> >>
>> >>
>> >> --
>> >> Rajesh.J
>> >>
>> >>
>> >>
>> >>  David Winsemius, MD
>> >> West Hartford, CT
>> >>
>> >>
>> >
>> >
>> > --
>> > Rajesh.J
>> >
>> >        [[alternative HTML version deleted]]
>> >
>> > __
>> > 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.
>> >
>>
>>
>>
>> --
>> Joshua Wiley
>> Ph.D. Student, Health Psychology
>> University of California, Los Angeles
>> http://www.joshuawiley.com/
>
>
>
> --
> Rajesh.J
>
>
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

__
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.


Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread Joshua Wiley
Hi Rajesh,

This will work, unfortunately it seems like lapply() drops the names
before it passes each element of the list which lead to my clumsy work
around using which().  I'm sure there are other ways.

dat <- list(INT = c("1","2","3"),
NUM = c("2.34","4.56","6.78"),
INT = c("4", "5", "6"),
NUM = c("3.44"))

ints <- which(names(dat)=="INT")
nums <- which(names(dat)=="NUM")

dat[ints] <- lapply(dat[ints], as.integer)
dat[nums] <- lapply(dat[nums], as.numeric)

str(dat)

Cheers,

Josh

On Sun, Sep 5, 2010 at 6:33 AM, rajesh j  wrote:
> The string vector actually comes as a part of a list, and the vector is
> named "int", and the numbers are strings. I then have to make it a vector
> that is still called "int" and has 4,5,6 etc. the types are either integer
> or numeric. The number of items in the vector is unknown.
>
> here's an example,
>
> a list has vectors
>
> INT
> "2"
> "3"
> "4"
>
> NUM
> "2.37"
> "4.56"
>
>
>
> On Sun, Sep 5, 2010 at 6:56 PM, David Winsemius wrote:
>
>>
>> On Sep 5, 2010, at 9:22 AM, rajesh j wrote:
>>
>> for e.g., I get the following as a string vector
>> "int" "4" "5" "6"
>> after reading the first element, I have to convert this to a integer vector
>>
>>
>>
>> But what is the right answer?  And what number of items are possble per
>> line?  And what are the other possible type identifiers? We need an example
>> that has enough complexity to allow testing.
>>
>> --
>> David.
>>
>>
>>
>> On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius 
>> wrote:
>>
>>>
>>> On Sep 5, 2010, at 8:48 AM, rajesh j wrote:
>>>
>>>  Hi,

 Is it possible to convert a string vector to integer or numeric vector?
 In
 my situation I receive data in a string vector and have to convert it
 based
 on a given type.

>>>
>>> Can you give an example? I don't understand either what sort of conversion
>>> you desire or what you mean by "convert it based on a given type."
>>>
>>> There are a couple of function you may want to consider but I am having
>>> difficulty convincing myself they answer the problem posed:
>>>
>>> ?charToRaw
>>> ?stroi
>>>
>>> > strtoi(charToRaw("123 this is a string"), base=16)   # convert to
>>> decimal ASCII
>>>  [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115 116 114
>>> 105 110 103
>>>
>>> --
>>>
>>> David Winsemius, MD
>>> West Hartford, CT
>>>
>>>
>>
>>
>> --
>> Rajesh.J
>>
>>
>>
>>  David Winsemius, MD
>> West Hartford, CT
>>
>>
>
>
> --
> Rajesh.J
>
>        [[alternative HTML version deleted]]
>
> __
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

__
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.


Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread David Winsemius


On Sep 5, 2010, at 10:47 AM, rajesh j wrote:

I'm sorry. you seem to have misunderstood my data representation for  
input.


Because you did not follow the Posting Guide's advice regarding  
producing examples using valid R code. You give me text; I work on text.


#-
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
#

I am going to change the name of the variable to cc because "c" is a  
unfortunate name for a variable, since it is also the name of a  
crucial function.



Here's what I have
cc<-list(INT=c("1","2","3"),NUM=c("2.34","4.56","6.78"))
I need
c<-list(INT=c(1,2,3),NUM=c(2.34,4.56,6.78))


> cc <- lapply(cc, as.numeric)
> cc
$INT
[1] 1 2 3

$NUM
[1] 2.34 4.56 6.78


--
David.




On Sun, Sep 5, 2010 at 7:57 PM, David Winsemius > wrote:
So there is one item per line and the task is to recognize the  
strings "INT" and "NUM" and create variables with numeric type and


INT=c(2,3,4)
NUM=c(2.37, 4.56)  # ???

I worry that is not a full description of the task  if there be  
more than just two variable names and if all the INTs have the same  
name, then they will get overwritten   but perhaps you have  
specified the problem completely, so here goes:


> txt <- textConnection('INT

+ "2"
+ "3"
+ "4"
+
+ NUM
+ "2.37"
+ "4.56"')
> indat <- read.table(txt, stringsAsFactors=FALSE)

> indat$nflag <- as.numeric(indat$V1)
> cumsum(is.na(indat$nflag))
[1] 1 1 1 1 2 2 2

> by(indat$V1, cumsum(is.na(indat$nflag)), function(x)  
assign(as.character(x[1]), as.numeric(x[-1]) ,envir = .GlobalEnv) )

cumsum(is.na(indat$nflag)): 1
[1] 1 3 4
-
cumsum(is.na(indat$nflag)): 2
[1] 2 5
> INT
[1] 1 3 4
> NUM
[1] 2 5
.




On Sep 5, 2010, at 9:33 AM, rajesh j wrote:

The string vector actually comes as a part of a list, and the vector  
is named "int", and the numbers are strings. I then have to make it  
a vector that is still called "int" and has 4,5,6 etc. the types are  
either integer or numeric. The number of items in the vector is  
unknown.


here's an example,

a list has vectors

INT
"2"
"3"
"4"

NUM
"2.37"
"4.56"



On Sun, Sep 5, 2010 at 6:56 PM, David Winsemius > wrote:


On Sep 5, 2010, at 9:22 AM, rajesh j wrote:

for e.g., I get the following as a string vector
"int" "4" "5" "6"
after reading the first element, I have to convert this to a integer  
vector


But what is the right answer?  And what number of items are possble  
per line?  And what are the other possible type identifiers? We need  
an example that has enough complexity to allow testing.


--
David.



On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius > wrote:


On Sep 5, 2010, at 8:48 AM, rajesh j wrote:

Hi,

Is it possible to convert a string vector to integer or numeric  
vector? In
my situation I receive data in a string vector and have to convert  
it based

on a given type.

Can you give an example? I don't understand either what sort of  
conversion you desire or what you mean by "convert it based on a  
given type."


There are a couple of function you may want to consider but I am  
having difficulty convincing myself they answer the problem posed:


?charToRaw
?stroi

> strtoi(charToRaw("123 this is a string"), base=16)   # convert to  
decimal ASCII
 [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115 116  
114 105 110 103


--


David Winsemius, MD
West Hartford, CT

__
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.


Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread rajesh j
I'm sorry. you seem to have misunderstood my data representation for input.
Here's what I have
c<-list(INT=c("1","2","3"),NUM=c("2.34","4.56","6.78"))
I need
c<-list(INT=c(1,2,3),NUM=c(2.34,4.56,6.78))


On Sun, Sep 5, 2010 at 7:57 PM, David Winsemius wrote:

> So there is one item per line and the task is to recognize the strings
> "INT" and "NUM" and create variables with numeric type and
>
> INT=c(2,3,4)
> NUM=c(2.37, 4.56)  # ???
>
> I worry that is not a full description of the task  if there be more
> than just two variable names and if all the INTs have the same name, then
> they will get overwritten   but perhaps you have specified the problem
> completely, so here goes:
>
> > txt <- textConnection('INT
>
> + "2"
> + "3"
> + "4"
> +
> + NUM
> + "2.37"
> + "4.56"')
> > indat <- read.table(txt, stringsAsFactors=FALSE)
>
> > indat$nflag <- as.numeric(indat$V1)
> > cumsum(is.na(indat$nflag))
> [1] 1 1 1 1 2 2 2
>
> > by(indat$V1, cumsum(is.na(indat$nflag)), function(x)
> assign(as.character(x[1]), as.numeric(x[-1]) ,envir = .GlobalEnv) )
> cumsum(is.na(indat$nflag)): 1
> [1] 1 3 4
> -
> cumsum(is.na(indat$nflag)): 2
> [1] 2 5
> > INT
> [1] 1 3 4
> > NUM
> [1] 2 5
> .
>
>
>
>
> On Sep 5, 2010, at 9:33 AM, rajesh j wrote:
>
>  The string vector actually comes as a part of a list, and the vector is
>> named "int", and the numbers are strings. I then have to make it a vector
>> that is still called "int" and has 4,5,6 etc. the types are either integer
>> or numeric. The number of items in the vector is unknown.
>>
>> here's an example,
>>
>> a list has vectors
>>
>> INT
>> "2"
>> "3"
>> "4"
>>
>> NUM
>> "2.37"
>> "4.56"
>>
>>
>>
>> On Sun, Sep 5, 2010 at 6:56 PM, David Winsemius 
>> wrote:
>>
>> On Sep 5, 2010, at 9:22 AM, rajesh j wrote:
>>
>>  for e.g., I get the following as a string vector
>>> "int" "4" "5" "6"
>>> after reading the first element, I have to convert this to a integer
>>> vector
>>>
>>
>> But what is the right answer?  And what number of items are possble per
>> line?  And what are the other possible type identifiers? We need an example
>> that has enough complexity to allow testing.
>>
>> --
>> David.
>>
>>
>>
>>> On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius 
>>> wrote:
>>>
>>> On Sep 5, 2010, at 8:48 AM, rajesh j wrote:
>>>
>>> Hi,
>>>
>>> Is it possible to convert a string vector to integer or numeric vector?
>>> In
>>> my situation I receive data in a string vector and have to convert it
>>> based
>>> on a given type.
>>>
>>> Can you give an example? I don't understand either what sort of
>>> conversion you desire or what you mean by "convert it based on a given
>>> type."
>>>
>>> There are a couple of function you may want to consider but I am having
>>> difficulty convincing myself they answer the problem posed:
>>>
>>> ?charToRaw
>>> ?stroi
>>>
>>> > strtoi(charToRaw("123 this is a string"), base=16)   # convert to
>>> decimal ASCII
>>>  [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115 116 114
>>> 105 110 103
>>>
>>> --
>>>
>>> David Winsemius, MD
>>> West Hartford, CT
>>>
>>>
>>>
>>>
>>> --
>>> Rajesh.J
>>>
>>>
>>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>>
>>
>>
>> --
>> Rajesh.J
>>
>>
>>
> David Winsemius, MD
> West Hartford, CT
>
>


-- 
Rajesh.J

[[alternative HTML version deleted]]

__
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.


Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread David Winsemius
So there is one item per line and the task is to recognize the strings  
"INT" and "NUM" and create variables with numeric type and


INT=c(2,3,4)
NUM=c(2.37, 4.56)  # ???

I worry that is not a full description of the task  if there be  
more than just two variable names and if all the INTs have the same  
name, then they will get overwritten   but perhaps you have  
specified the problem completely, so here goes:


> txt <- textConnection('INT
+ "2"
+ "3"
+ "4"
+
+ NUM
+ "2.37"
+ "4.56"')
> indat <- read.table(txt, stringsAsFactors=FALSE)

> indat$nflag <- as.numeric(indat$V1)
> cumsum(is.na(indat$nflag))
[1] 1 1 1 1 2 2 2

> by(indat$V1, cumsum(is.na(indat$nflag)), function(x)  
assign(as.character(x[1]), as.numeric(x[-1]) ,envir = .GlobalEnv) )

cumsum(is.na(indat$nflag)): 1
[1] 1 3 4
-
cumsum(is.na(indat$nflag)): 2
[1] 2 5
> INT
[1] 1 3 4
> NUM
[1] 2 5
.



On Sep 5, 2010, at 9:33 AM, rajesh j wrote:

The string vector actually comes as a part of a list, and the vector  
is named "int", and the numbers are strings. I then have to make it  
a vector that is still called "int" and has 4,5,6 etc. the types are  
either integer or numeric. The number of items in the vector is  
unknown.


here's an example,

a list has vectors

INT
"2"
"3"
"4"

NUM
"2.37"
"4.56"



On Sun, Sep 5, 2010 at 6:56 PM, David Winsemius > wrote:


On Sep 5, 2010, at 9:22 AM, rajesh j wrote:


for e.g., I get the following as a string vector
"int" "4" "5" "6"
after reading the first element, I have to convert this to a  
integer vector


But what is the right answer?  And what number of items are possble  
per line?  And what are the other possible type identifiers? We need  
an example that has enough complexity to allow testing.


--
David.




On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius > wrote:


On Sep 5, 2010, at 8:48 AM, rajesh j wrote:

Hi,

Is it possible to convert a string vector to integer or numeric  
vector? In
my situation I receive data in a string vector and have to convert  
it based

on a given type.

Can you give an example? I don't understand either what sort of  
conversion you desire or what you mean by "convert it based on a  
given type."


There are a couple of function you may want to consider but I am  
having difficulty convincing myself they answer the problem posed:


?charToRaw
?stroi

> strtoi(charToRaw("123 this is a string"), base=16)   # convert to  
decimal ASCII
 [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115  
116 114 105 110 103


--

David Winsemius, MD
West Hartford, CT




--
Rajesh.J




David Winsemius, MD
West Hartford, CT




--
Rajesh.J




David Winsemius, MD
West Hartford, CT

__
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.


Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread rajesh j
The string vector actually comes as a part of a list, and the vector is
named "int", and the numbers are strings. I then have to make it a vector
that is still called "int" and has 4,5,6 etc. the types are either integer
or numeric. The number of items in the vector is unknown.

here's an example,

a list has vectors

INT
"2"
"3"
"4"

NUM
"2.37"
"4.56"



On Sun, Sep 5, 2010 at 6:56 PM, David Winsemius wrote:

>
> On Sep 5, 2010, at 9:22 AM, rajesh j wrote:
>
> for e.g., I get the following as a string vector
> "int" "4" "5" "6"
> after reading the first element, I have to convert this to a integer vector
>
>
>
> But what is the right answer?  And what number of items are possble per
> line?  And what are the other possible type identifiers? We need an example
> that has enough complexity to allow testing.
>
> --
> David.
>
>
>
> On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius wrote:
>
>>
>> On Sep 5, 2010, at 8:48 AM, rajesh j wrote:
>>
>>  Hi,
>>>
>>> Is it possible to convert a string vector to integer or numeric vector?
>>> In
>>> my situation I receive data in a string vector and have to convert it
>>> based
>>> on a given type.
>>>
>>
>> Can you give an example? I don't understand either what sort of conversion
>> you desire or what you mean by "convert it based on a given type."
>>
>> There are a couple of function you may want to consider but I am having
>> difficulty convincing myself they answer the problem posed:
>>
>> ?charToRaw
>> ?stroi
>>
>> > strtoi(charToRaw("123 this is a string"), base=16)   # convert to
>> decimal ASCII
>>  [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115 116 114
>> 105 110 103
>>
>> --
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>>
>
>
> --
> Rajesh.J
>
>
>
>  David Winsemius, MD
> West Hartford, CT
>
>


-- 
Rajesh.J

[[alternative HTML version deleted]]

__
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.


Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread David Winsemius

On Sep 5, 2010, at 9:22 AM, rajesh j wrote:

> for e.g., I get the following as a string vector
> "int" "4" "5" "6"
> after reading the first element, I have to convert this to a integer  
> vector

But what is the right answer?  And what number of items are possble  
per line?  And what are the other possible type identifiers? We need  
an example that has enough complexity to allow testing.

-- 
David.


>
> On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius  > wrote:
>
> On Sep 5, 2010, at 8:48 AM, rajesh j wrote:
>
> Hi,
>
> Is it possible to convert a string vector to integer or numeric  
> vector? In
> my situation I receive data in a string vector and have to convert  
> it based
> on a given type.
>
> Can you give an example? I don't understand either what sort of  
> conversion you desire or what you mean by "convert it based on a  
> given type."
>
> There are a couple of function you may want to consider but I am  
> having difficulty convincing myself they answer the problem posed:
>
> ?charToRaw
> ?stroi
>
> > strtoi(charToRaw("123 this is a string"), base=16)   # convert to  
> decimal ASCII
>  [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115 116  
> 114 105 110 103
>
> -- 
>
> David Winsemius, MD
> West Hartford, CT
>
>
>
>
> -- 
> Rajesh.J
>
>

David Winsemius, MD
West Hartford, CT


[[alternative HTML version deleted]]

__
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.


Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread rajesh j
for e.g., I get the following as a string vector
"int" "4" "5" "6"
after reading the first element, I have to convert this to a integer vector

On Sun, Sep 5, 2010 at 6:44 PM, David Winsemius wrote:

>
> On Sep 5, 2010, at 8:48 AM, rajesh j wrote:
>
>  Hi,
>>
>> Is it possible to convert a string vector to integer or numeric vector? In
>> my situation I receive data in a string vector and have to convert it
>> based
>> on a given type.
>>
>
> Can you give an example? I don't understand either what sort of conversion
> you desire or what you mean by "convert it based on a given type."
>
> There are a couple of function you may want to consider but I am having
> difficulty convincing myself they answer the problem posed:
>
> ?charToRaw
> ?stroi
>
> > strtoi(charToRaw("123 this is a string"), base=16)   # convert to decimal
> ASCII
>  [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115 116 114
> 105 110 103
>
> --
>
> David Winsemius, MD
> West Hartford, CT
>
>


-- 
Rajesh.J

[[alternative HTML version deleted]]

__
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.


Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread David Winsemius


On Sep 5, 2010, at 8:48 AM, rajesh j wrote:


Hi,

Is it possible to convert a string vector to integer or numeric  
vector? In
my situation I receive data in a string vector and have to convert  
it based

on a given type.


Can you give an example? I don't understand either what sort of  
conversion you desire or what you mean by "convert it based on a given  
type."


There are a couple of function you may want to consider but I am  
having difficulty convincing myself they answer the problem posed:


?charToRaw
?stroi

> strtoi(charToRaw("123 this is a string"), base=16)   # convert to  
decimal ASCII
 [1]  49  50  51  32 116 104 105 115  32 105 115  32  97  32 115 116  
114 105 110 103


--

David Winsemius, MD
West Hartford, CT

__
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.


Re: [R] converting string vector to integer/numeric vector

2010-09-05 Thread Ista Zahn
See
?numeric
?integer
and possible the colClasses argument of
read.table

-Ista

On Sun, Sep 5, 2010 at 12:48 PM, rajesh j  wrote:
> Hi,
>
> Is it possible to convert a string vector to integer or numeric vector? In
> my situation I receive data in a string vector and have to convert it based
> on a given type.
> --
> Rajesh.J
>
>        [[alternative HTML version deleted]]
>
> __
> 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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

__
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.


[R] converting string vector to integer/numeric vector

2010-09-05 Thread rajesh j
Hi,

Is it possible to convert a string vector to integer or numeric vector? In
my situation I receive data in a string vector and have to convert it based
on a given type.
-- 
Rajesh.J

[[alternative HTML version deleted]]

__
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.