Hi Suppose we have an object with strings: A<-c("a","b","c","d") Now I do: B<-matrix(A,4,4, byrow=F) and I get a a a a b b b b c c c c d d d d But what I really want is: a b c d b c d a c d a b d a b c How can I do this? thank you A. Dias -- View this message in context: http://r.789695.n4.nabble.com/Creating-a-Matrix-from-a-vector-with-some-conditions-tp3178219p3178219.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2011-Jan-07 04:53 UTC
[R] Creating a Matrix from a vector with some conditions
On Jan 6, 2011, at 4:34 PM, ADias wrote:> > Hi > > Suppose we have an object with strings: > > A<-c("a","b","c","d") > > Now I do: > > B<-matrix(A,4,4, byrow=F) > > and I get > > a a a a > b b b b > c c c c > d d d d > > But what I really want is: > > a b c d > b c d a > c d a b > d a b c > > How can I do this?How else? B<-matrix(A,4,4, byrow=TRUE)> > thank you > > A. Dias > -- > View this message in context: http://r.789695.n4.nabble.com/Creating-a-Matrix-from-a-vector-with-some-conditions-tp3178219p3178219.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Petr Savicky
2011-Jan-07 07:59 UTC
[R] Creating a Matrix from a vector with some conditions
On Thu, Jan 06, 2011 at 01:34:31PM -0800, ADias wrote:> > Hi > > Suppose we have an object with strings: > > A<-c("a","b","c","d") > > Now I do: > > B<-matrix(A,4,4, byrow=F) > > and I get > > a a a a > b b b b > c c c c > d d d d > > But what I really want is: > > a b c d > b c d a > c d a b > d a b c > > How can I do this?Try the following A <- c("a","b","c","d") B <- matrix(A, 5, 4)[1:4, ] # [,1] [,2] [,3] [,4] #[1,] "a" "b" "c" "d" #[2,] "b" "c" "d" "a" #[3,] "c" "d" "a" "b" #[4,] "d" "a" "b" "c" Petr Savicky.
Petr Savicky
2011-Jan-08 22:52 UTC
[R] Creating a Matrix from a vector with some conditions
On Thu, Jan 06, 2011 at 01:34:31PM -0800, ADias wrote:> > Hi > > Suppose we have an object with strings: > > A<-c("a","b","c","d") > > Now I do: > > B<-matrix(A,4,4, byrow=F) > > and I get > > a a a a > b b b b > c c c c > d d d d > > But what I really want is: > > a b c d > b c d a > c d a b > d a b c > > How can I do this?Try the following v <- rep(c("a", "b", "c", "d"), times=2) a <- matrix(nrow=4, ncol=4) a[, ] <- v[row(a) + col(a) - 1] a [,1] [,2] [,3] [,4] [1,] "a" "b" "c" "d" [2,] "b" "c" "d" "a" [3,] "c" "d" "a" "b" [4,] "d" "a" "b" "c" Petr Savicky.
baptiste auguie
2011-Jan-09 09:21 UTC
[R] Creating a Matrix from a vector with some conditions
Hi, embed() seemed well-suited, but I couldn't figure out an elegant way to use it embed(c(A,A), 4)[1:4, 4:1] HTH, baptiste On 6 January 2011 22:34, ADias <diasandre at gmail.com> wrote:> > Hi > > Suppose we have an object with strings: > > A<-c("a","b","c","d") > > Now I do: > > B<-matrix(A,4,4, byrow=F) > > and I get > > a a a a > b b b b > c c c c > d d d d > > But what I really want is: > > a b c d > b c d a > c d a b > d a b c > > How can I do this? > > thank you > > A. Dias > -- > View this message in context: http://r.789695.n4.nabble.com/Creating-a-Matrix-from-a-vector-with-some-conditions-tp3178219p3178219.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >