Re: [R] split string into individual valus while reading in R

From: Upton, Stephen (Steve) (CIV) <scupton_at_nps.edu>
Date: Wed, 13 Apr 2011 08:20:12 -0700

Hi Ram,

Try this on your V4:
d <- yourdf$V4
unlist(lapply(1:nchar(d),function(x) substr(d,x,x)))

HTH
steve
-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Ram H. Sharma
Sent: Wednesday, April 13, 2011 7:34 AM
To: r-help_at_r-project.org
Subject: Re: [R] split string into individual valus while reading in R

It looks like my question is not clear, I have not get any suggestion, yet let me reiterate my problem:

My data looks like this to be read from a text file. As I provided earlier the A, B, and H in V4 column has much longer chain.

V1     V2       V3        V4
1_1    ch1       0.0       AHAH
1_2    ch1       0.20     AHAAA

...........................so on

I want to read this as (splitting AHB........into individual characters)
V1     V2       V3         V4   V5        V6      V7      V8
1_1    ch1       0.0       A    H          A        H        NULL
1_2    ch1       0.20     A    H          A        A        A

Any trick to do this?

Thanks;

Ram H

On Tue, Apr 12, 2011 at 5:19 PM, Ram H. Sharma <sharma.ram.h_at_gmail.com>wrote:

> Dear R experts
>
> Sorry for posting:
> I have text file that I need to read into R (this is out from different
> program). A portion of data look like:
>
> ;example data
> 1_1 ch1 0.0
>
HAAAHHAAAHHAAAAAHHAHHAHAHHHAAHHAHAAAAHAAAHHAHHAAHAHHAAAAHAAAHHHAAAHHAHHHAAAH HHHAHHAAHAAAAHAHHHHHAAAHAHHAHHHHAHHHHHHHHHAAHAAHHHAHAAAAAHHHHHHAHHHHHAHHHH
> 1_2 ch1 0.1
>
HAAAHHAAAHHAAAAAHHAHHAHAHHHAAHHAHAAAAHAAAHHAHHAAHAHHAAAAHAAAHHHAAAHHAHHHAAAH HHHAHHAAHAAAAHAHHHHHAAAHAHHAHHHHAHHHHHHHHHAAHAAHHHAHAAAAAHHHHHHAHHHHHAHHHH
> 1_3 ch1 0.1
>
HAAAHHAAAHHAAAAAHHAHHAHAHHHAAHHAHAAAAHAAAHHAHHAA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HHHHAAAHAHHAHHHHAHHHHHHHHHAAHAAHHHAHAAAAAHHHHHHAHHHHHHHHHH
>
> read_data <- read.table ("my_text_file.txt",header=F,comment=";")
> How can I read this into table where columns first five column are
> maintained as such (e.g. 1_1 ch1 0.0 H ) where as the long single column
> (filled with A, H, B) split into individual columns. Or alternatively
first
> read using the following command and then able to split the variable 4.
>
> Thank you for help;
>
>
> Ram H
>
>

-- 

Ram H

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



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

Received on Wed 13 Apr 2011 - 15:23:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 13 Apr 2011 - 15:40:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive