Eduardo Mendes
2011-Aug-18 18:35 UTC
[R] Concatenate two strings in one in a string matrix
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? Many thanks Ed [[alternative HTML version deleted]]
R. Michael Weylandt
2011-Aug-18 18:40 UTC
[R] Concatenate two strings in one in a string matrix
Assuming that the "*" in your [11,] example is a typo, would this work? apply(out$desc,1,paste,collapse="") Hope this helps, Michael Weylandt On Thu, Aug 18, 2011 at 2:35 PM, Eduardo Mendes <emammendes@gmail.com>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? > > Many thanks > > Ed > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
David Winsemius
2011-Aug-18 18:40 UTC
[R] Concatenate two strings in one in a string matrix
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
Seemingly Similar Threads
- lme model specification
- readMat - how to retrieve the variables
- How to convert number (matlab) to date
- Greetings. I have a question with mixed beta regression model in nlme.
- Greetings. I have a question with mixed beta regression model in nlme (corrected version).