[R] Concatenate two strings in one in a string matrix

David Winsemius dwinsemius at comcast.net
Fri Aug 19 16:21:21 CEST 2011


On Aug 19, 2011, at 9:45 AM, Eduardo Mendes wrote:

> Hello
>
> Many thanks.
>
> * is not a typo.   The output is a description of a nonlinear system  
> so terms such as y(k-1)*y(k-2) are allowed.   I wonder whether ""  
> could be ignored so that the outputs such as y_{01}(k-003)* would  
> not show up.
>
Paste with "*"  then remove trailing "*"'s:

ifelse( grepl("*$", result), sub("*","", result), result)

> Cheers
>
> Ed
>
>
> On Thu, Aug 18, 2011 at 3:40 PM, David Winsemius <dwinsemius at comcast.net 
> > wrote:
>
> On Aug 18, 2011, at 2:35 PM, Eduardo Mendes wrote:
>
> Dear R-Users
>
> I have the following matrix
>
> out$desc      [,1]            [,2]
> [1,] ""              ""
> [2,] "y_{01}(k-001)" ""
> [3,] "y_{01}(k-002)" ""
> [4,] "y_{01}(k-003)" ""
> [5,] "u_{01}(k-001)" ""
> [6,] "u_{01}(k-002)" ""
> [7,] "u_{01}(k-003)" ""
> [8,] "y_{01}(k-001)" "y_{01}(k-001)"
> [9,] "y_{01}(k-001)" "y_{01}(k-002)"
> [10,] "y_{01}(k-001)" "y_{01}(k-003)"
> [11,] "y_{01}(k-001)" "u_{01}(k-001)"
>
>
> and need to concatenate each line to a single string.  Something like
>
> [2,] "y_{01}(k-001)" ""  -> [2,] "y_{01}(k-001)"
>
> [11,] "y_{01}(k-001)" "u_{01}(k-001)" -> [11,] "y_{01}(k-001)*u_{01} 
> (k-001)"
>
> Is there a way to do it without going through every column?
>
> apply(out$desc, 1, paste, collapse="")
>
> It is ambiguous what you want for a delimiter. In one case you used  
> "", and another you used "*". I used "".
>
>
> -- 
>
> David Winsemius, MD
> West Hartford, CT
>
>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list