Q
在函數中重複參數
0
A
回答
2
功能rep
似乎做你想做的。
n <- 3 #number of repetitions
x <- list(seq(1,5))
expand.grid(rep(x,n)) #gives a data.frame of 125 rows and 3 columns
x2 <- list(a = seq(1,5), b = seq(6, 10))
expand.grid(rep(x2,n)) #gives a data.frame of 15625 rows and 6 columns
0
0
我認爲,解決原來的問題最簡單的方法是使用rep
嵌套列表。
例如,要展開n次相同的列表,使用rep根據需要多次展開嵌套列表(n
),然後使用展開的列表作爲expand.grid
的唯一參數。
# Example list
l <- list(1, 2, 3)
# Times required
n <- 3
# Expand as many times as needed
m <- rep(list(l), n)
# Expand away
expand.grid(m)
0
如果該函數要在列表的元素(重複地)動作自如(即,列表成員是從所定義的列表本身未連接),以下將是有用的:
l <- list(1:5, "s") # A list with numerics and characters
n <- 3 # number of repetitions
expand.grid(unlist(rep(l, n))) # the result is:
Var1
1 1
2 2
3 3
4 4
5 5
6 s
7 1
8 2
9 3
10 4
11 5
12 s
13 1
14 2
15 3
16 4
17 5
18 s
您是否想要展開原始對象的列表?然後看我的答案,一個更復雜的對象列表可以用@ Qaswed的回答來解決 – NJBurgo