roberto.muller at doctor.com
2011-Mar-11 15:12 UTC
[R] Fwd: How to get all combinations between two character vectors?
Hi Rainer
Or maybe you are referring to the outer function.
I'm a newbie in R, but I recently read something about it in the pdf book
named below (pages 91-2). I send to you an excerpt:
An introduction to R
An introduction to R
Longhow Lam
6.2.5 The outer function
outer function
The function outer performs an outer-product given two arrays (vectors). This
can
be especially useful for evaluating a function on a grid without explicit
looping. The
function has at least three input-arguments: two vectors x and y and the name of
a
function that needs two or more arguments for input. For every combination of
the
vector elements of x and y this function is evaluated. Some examples are given
by the
code below.
outer performs an outer-product given two arrays (vectors). This can
be especially useful for evaluating a function on a grid without explicit
looping. The
function has at least three input-arguments: two vectors x and y and the name of
a
function that needs two or more arguments for input. For every combination of
the
vector elements of x and y this function is evaluated. Some examples are given
by the
code below.
x and y and the name of a
function that needs two or more arguments for input. For every combination of
the
vector elements of x and y this function is evaluated. Some examples are given
by the
code below.
x and y this function is evaluated. Some examples are given by the
code below.
x <- 1:3
y <- 1:3
z <- outer(x,y,FUN="-")
z
[,1] [,2] [,3]
[1,] 0 -1 -2
[2,] 1 0 -1
[3,] 2 1 0
x <- c("A", "B", "C", "D")
y <- 1:9
z <- outer(x, y, paste, sep = "")
z
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] "A1" "A2" "A3" "A4" "A5"
"A6" "A7" "A8" "A9"
[2,] "B1" "B2" "B3" "B4" "B5"
"B6" "B7" "B8" "B9"
[3,] "C1" "C2" "C3" "C4" "C5"
"C6" "C7" "C8" "C9"
[4,] "D1" "D2" "D3" "D4" "D5"
"D6" "D7" "D8" "D9"
z
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] "A1" "A2" "A3" "A4" "A5"
"A6" "A7" "A8" "A9"
[2,] "B1" "B2" "B3" "B4" "B5"
"B6" "B7" "B8" "B9"
[3,] "C1" "C2" "C3" "C4" "C5"
"C6" "C7" "C8" "C9"
[4,] "D1" "D2" "D3" "D4" "D5"
"D6" "D7" "D8" "D9"
z <- outer(x, y, paste, sep = "")
z
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] "A1" "A2" "A3" "A4" "A5"
"A6" "A7" "A8" "A9"
[2,] "B1" "B2" "B3" "B4" "B5"
"B6" "B7" "B8" "B9"
[3,] "C1" "C2" "C3" "C4" "C5"
"C6" "C7" "C8" "C9"
[4,] "D1" "D2" "D3" "D4" "D5"
"D6" "D7" "D8" "D9"
-----Original Message-----
From: David L Lorenz <lorenz@usgs.gov>
To: Rainer M Krug <r.m.krug@gmail.com>
Cc: r-help@r-project.org
Sent: Fri, Mar 11, 2011 9:31 am
Subject: Re: [R] How to get all combinations between two character vectors?
Rainer,
The are probably lots of ways, I'd use
levels(interaction(c("a", "b"), c('x', 'y'),
sep=''))
Dave>Hi
I know there is a function - I have used it before - but I always forget
what it is called...
I need the combination of two character vectors, i.e:
x <- c("a", "b")
y <- c("x", "y")
z <- THEFUNCTION(x, y)
z == c("ax", "ay", "bx", "by")
I promise I will write the name of the function on my desk!
Thanks,
Rainer
[[alternative HTML version deleted]]
______________________________________________
-help@r-project.org mailing list
ttps://stat.ethz.ch/mailman/listinfo/r-help
LEASE do read the posting guide http://www.R-project.org/posting-guide.html
nd provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
Reasonably Related Threads
- Server Report [Incident: 040126-000715]
- µÚËĽ챱¾©³¯Ñô¹ú¼ÊÉÌÎñ½Ú
- Unable to browse Samba share by hostname from certain clients...
- Need help in addressing this error - ERROR OpenSSL::SSL::SSLError: SSL_accept returned=1 errno=0 state=SSLv3 read client certificate A: tlsv1 alert unknown ca
- retrieve machine password in current Samba?
