[R] converting string vector to integer/numeric vector

Joshua Wiley jwiley.psych at gmail.com
Sun Sep 5 17:33:11 CEST 2010


On Sun, Sep 5, 2010 at 8:16 AM, rajesh j <akshay.rajesh at gmail.com> 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(<required usage>=="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 <jwiley.psych at gmail.com> 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 <akshay.rajesh at gmail.com> 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
>> > <dwinsemius at comcast.net>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
>> >> <dwinsemius at comcast.net>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 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.
>> >
>>
>>
>>
>> --
>> 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/



More information about the R-help mailing list