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

Upton, Stephen (Steve) (CIV) scupton at nps.edu
Wed Apr 13 17:20:12 CEST 2011


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
>
HAAAHHAAAHHAAAAAHHAHHAHAHHHAAHHAHAAAAHAAAHHAHHAAHAHHAAAAHAAAHH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HHHAAAHAHHAHHHHAHHHHHHHHHAAHAAHHHAHAAAAAHHHHHHAHHHHHHHHHH
>
>  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.


More information about the R-help mailing list