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]]
See ?numeric ?integer and possible the colClasses argument of read.table -Ista On Sun, Sep 5, 2010 at 12:48 PM, rajesh j <akshay.rajesh at gmail.com> 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 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
David Winsemius
2010-Sep-05 13:14 UTC
[R] converting string vector to integer/numeric vector
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
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
Hi Professor: When I read the file, the time in military time and date are stored in the same vector as follows: 2011-01-29 16:15:11.823547 I want to take all the hours saved in that file and categorize them as day and night. For example, the day would be between 06:00:00-19:00:00 and the night would be between 19:00:01-05:00:59. Given that condition, I intend to allocate time to their respective categories, and then calculate how many hours to read from the file fall into the category of the night and many more day. Then, each category be averaged, ie, the hours during the day and the hours during the night. If one day he drove 10 hours and 3 were day and 7 night, calculate the average. From: amy_ruiz_goyco@hotmail.com To: ripley@stats.ox.ac.uk Subject: RE: [R] Converting the time in a numeric value Date: Sun, 10 Jul 2011 17:39:38 -0400 When I read the file, the time in military time and date are stored in the same vector as follows: 2011-01-29 16:15:11.823547 I want to take all the hours saved in that file and categorize them as day and night. For example, the day would be between 06:00:00-19:00:00 and the night would be between 19:00:01-05:00:59. Given that condition, I intend to allocate time to their respective categories, and then calculate how many hours to read from the file fall into the category of the night and many more day. Then, each category be averaged, ie, the hours during the day and the hours during the night. If one day he drove 10 hours and 3 were day and 7 night, calculate the average.> Date: Sun, 10 Jul 2011 08:15:00 +0100 > From: ripley@stats.ox.ac.uk > To: amy_ruiz_goyco@hotmail.com > CC: r-help@r-project.org > Subject: Re: [R] Converting the time in a numeric value > > On Sun, 10 Jul 2011, Amy Ruiz Goyco wrote: > > > Hello: > > > I am new using R. I have a file that contain in the same columns > > date and time like for example 2011/10/03 12:34:45.123423 p.m., but > > when I read the file and display the vector, I see of this way > > "2011-10-03 12:34:45.123423". I need to convert the time in a > > numeric and the date if is possible, but I don't need this to > > compute. Thus, I used this tiempo=substr(time,12,26) to selected > > the data that I need, but I don't know how I can change this to a > > numeric values. > > You need to clarify what you mean by 'date' and 'numeric' here. At a > guess, 'numeric' might mean 'number of seconds past midnight'. We > can't even do that, since we don't know the timezone (and it differs > on DST transition days). So you need to read a well-informed article > on date-times (not the un-refereed one in R-News 4/1) to gain more > understanding. > > Also, your AM/PM indicator is very non-standard. > > But here are some pieces for you to work with > > dt <- "2011/10/03 12:34:45.123423 p.m." > t0 <- substr(dt, 12, 26) > PM <- substr(dt, 28, 31) > > date <- strptime(dt, "%Y/%m/%d") > t1 <- unclass(as.POSIXct(paste("1970-01-01", t0))) > time <- ifelse(PM == "p.m.", t1+12*3600, t1) > > > date > [1] "2011-10-03" > > print(time, digits=12) > [1] 84885.123423 > > > > [[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. > > > > -- > Brian D. Ripley, ripley@stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595[[alternative HTML version deleted]]