[Rd] strsplit convert data

Hans-Jörg Bibiko bibiko at eva.mpg.de
Tue Oct 25 01:04:46 CEST 2011


On 25 Oct 2011, at 00:34, RMSOPS wrote:
>   To be more specific, my problem is here
> Line <-dataset $ Items [i]
>   print (line)
>   in.s <- strsplit (line, '')
> 
> I am reading lines from a file
> Line 1 A, B, C, D, G
> Line 2 A, C, E,
> ...
> line n F, G
> 
> the problem is that I can not make the split of the comma, so I can not get
> the
> output
>      A B C D E F G O
> [1,] 1 1 1 1 1 0 0 0
> [2,] 1 0 1 0 1 0 0 1
> [n] 0 0 0 0 0 1 1 0

Hi,

hmm, to be honest I didn't get your point yet but let me try.

If you read lines like :

"A, B, C, D, G" etc.

then you can split them by using:

strsplit(line, ', *')
[1] "A" "B" "C" "D" "G"

[', *' is a regular expression to match a "," followed by 0 or more spaces]

Then next issue I see here is that you create num times output matrices in your for loop "for(i in 1:num)" and overwrite the file res.csv all the time. Either you know in beforehand which columns could appear (and how many rows) then you can create your matrix before that loop or you should go with cbind() and rbind() resp. or similar.


Hope that helps a bit,
--Hans


More information about the R-devel mailing list