我有一个数据框,并希望使用组X和组Y的转换组合从它生成一组新的数据集:
#Group X
df1 <- df+1
df2 <- df-2
df3 <- df*3
#Group Y
df4 <- df*4
df5 <- df^5
df6 <- df/6我真正想做的是使用expand.grid,然后使用组X和组Y转换的每个组合生成一个新的数据集。(组Y始终在组X之后应用。)这些数据集应该存储在全局环境中。
因此,输出结果将与
df14 <- (df+1)*4
df24 <- (df-2)*4
df34 <- (df*3)*4
df15 <- (df+1)^5
df25 <- (df-2)^5
df35 <- (df*3)^5
df16 <- (df+1)/6
df26 <- (df-2)/6
df36 <- (df*3)/6我该怎么做呢?(例如,您可以从字面上获取任何数据帧的数据,例如iris。)
如果有帮助,您可以将组X和Y转换重写为函数:
#Group X
Fun1 <- function(x){return(x+1)}
Fun2 <- function(x){return(x-2)}
Fun3 <- function(x){return(x*3)}
#Group Y
Fun4 <- function(x){return(x*4)}
Fun5 <- function(x){return(x^5)}
Fun6 <- function(x){return(x/6)}我猜对于数据集的名称,像df.Fun1.Fun4这样的名称会更好。(请注意,根据我提供的数据框的名称,df当然应该更改,因此这里将其设置为iris.Fun1.Fun4)
发布于 2020-05-14 00:53:20
编写一个能做你想做的事情的函数:
foo = function(add, mult) {
(df + add) * mult
}然后对add和mult的所需值使用expand.grid,并对其进行迭代。在base中,Map是迭代多个值的一种很好的方式。
params = expand.grid(add = 1:3, mult = 4:6)
df = iris[1:6, 1:3] # numeric sample from iris
result = with(params, Map(foo, add = add, mult = mult))
names(result) = with(params, paste0("add ", add, ", mult ", mult))
result
# $`add 1, mult 4`
# Sepal.Length Sepal.Width Petal.Length
# 1 24.4 18.0 9.6
# 2 23.6 16.0 9.6
# 3 22.8 16.8 9.2
# 4 22.4 16.4 10.0
# 5 24.0 18.4 9.6
# 6 25.6 19.6 10.8
#
# $`add 2, mult 4`
# Sepal.Length Sepal.Width Petal.Length
# 1 28.4 22.0 13.6
# 2 27.6 20.0 13.6
# 3 26.8 20.8 13.2
# 4 26.4 20.4 14.0
# 5 28.0 22.4 13.6
# 6 29.6 23.6 14.8
#
# $`add 3, mult 4`
# Sepal.Length Sepal.Width Petal.Length
# 1 32.4 26.0 17.6
# 2 31.6 24.0 17.6
# ...适配函数而非特定参数:
#Group X
Fun1 <- function(x){return(x+1)}
Fun2 <- function(x){return(x-2)}
Fun3 <- function(x){return(x*3)}
#Group Y
Fun4 <- function(x){return(x*4)}
Fun5 <- function(x){return(x^5)}
Fun6 <- function(x){return(x/6)}
# Put the functions in a list
funs_x = mget(ls(pattern = "Fun[1-3]"))
funs_y = mget(ls(pattern = "Fun[4-6]"))
# iterate over list indices
indices = expand.grid(ind_x = seq_along(funs_x), ind_y = seq_along(funs_y))
result = with(indices, Map(function(ind_x, ind_y) funs_y[[ind_y]](funs_x[[ind_x]](df)), ind_x, ind_y))
names(result) = with(indices, paste("df", names(funs_x)[ind_x], names(funs_y)[ind_y], sep = "."))
result
# $df.Fun1.Fun4
# Sepal.Length Sepal.Width Petal.Length
# 1 24.4 18.0 9.6
# 2 23.6 16.0 9.6
# 3 22.8 16.8 9.2
# 4 22.4 16.4 10.0
# 5 24.0 18.4 9.6
# 6 25.6 19.6 10.8
#
# $df.Fun2.Fun4
# Sepal.Length Sepal.Width Petal.Length
# 1 12.4 6.0 -2.4
# 2 11.6 4.0 -2.4
# 3 10.8 4.8 -2.8
# 4 10.4 4.4 -2.0
# 5 12.0 6.4 -2.4
# 6 13.6 7.6 -1.2
#
# $df.Fun3.Fun4
# Sepal.Length Sepal.Width Petal.Length
# 1 61.2 42.0 16.8
# 2 58.8 36.0 16.8
# ...https://stackoverflow.com/questions/61780192
复制相似问题