[R] What is the returned type of strsplit?
Patrick Connolly
p_connolly at slingshot.co.nz
Sun Aug 16 21:59:28 CEST 2009
On Sun, 16-Aug-2009 at 10:40AM -0500, Peng Yu wrote:
|> Hi,
|>
|> I run the following program. I thought that 'b' was a matrix. But it
|> is actually not, right? Can somebody elaborate more on the type
|> difference between 'b' and 'c' to help me understand it better?
|>
|> Why 'c' is what it is now? Why 'c' is not the transpose of what it is now?
|>
|> Regards,
|> Peng
|>
|> $ Rscript split.R
|> > a=array(c('a,b','c,d','e,f'),c(3))
|> > a[[1]]
|> [1] "a,b"
|> > a
|> [1] "a,b" "c,d" "e,f"
|> > b=strsplit(a,split=',')
|> > b[[1]]
|> [1] "a" "b"
|> > b
|> [[1]]
|> [1] "a" "b"
|>
|> [[2]]
|> [1] "c" "d"
|>
|> [[3]]
|> [1] "e" "f"
|>
|> > c=sapply(b,function(x){x[1:2]})
|> > c
|> [,1] [,2] [,3]
|> [1,] "a" "c" "e"
|> [2,] "b" "d" "f"
I think your question is about sapply, not strsplit. It "simplifies"
and if it's going to make a matrix, it will set byrow to FALSE.
HTH
--
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
___ Patrick Connolly
{~._.~} Great minds discuss ideas
_( Y )_ Average minds discuss events
(:_~*~_:) Small minds discuss people
(_)-(_) ..... Eleanor Roosevelt
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
More information about the R-help
mailing list