二维应用于R?
Two-dimensional apply in R?
我有两个数组
chemArr = c("water","oil",...(lots of values omitted)...,"hydrogen")
timeArr = c("0", "8hr", "12hr", "24hr", "36hr", "48hr", "72hr")
我想构建一个数据框
chem 0 8hr ... 72hr
water f("water", "0") f("water", "8hr") ...
...
其中f
是我写的一个函数。在 R 中有没有好的方法来做到这一点?
在我的特定情况下,使一个函数接受 chem
并每次返回一列会更有效,因为这些共享计算中的每一个。但由于所需的总时间很小,如果更方便的话,我可以用其他方式来做。
为了说明评论中讨论的解决方案:
one <- letters[1:3]
two <- letters[4:6]
one
[1] "a" "b" "c"
two
[1] "d" "e" "f"
mapply(paste0, one, two)
a b c
"ad" "be" "cf"
mapply(paste0, sort(rep(one, length(two))), two)
a a a b b b c c c
"ad" "ae" "af" "bd" "be" "bf" "cd" "ce" "cf"
mapply(paste0, one, list(two)) # courtesy of @thelatemail
a b c
[1,] "ad" "bd" "cd"
[2,] "ae" "be" "ce"
[3,] "af" "bf" "cf"
outer(one, two, paste0) # courtesy of @akrun
[,1] [,2] [,3]
[1,] "ad" "ae" "af"
[2,] "bd" "be" "bf"
[3,] "cd" "ce" "cf"
我建议使用 expand.grid
,它创建所有 2 路组合的 "long form",然后使用 mapply 从函数创建值:
chemArr = c("water","oil","hydrogen")
timeArr = c("0", "8hr", "12hr", "24hr", "36hr", "48hr", "72hr")
mygrid <- expand.grid(chemArr, timeArr)
mygrid <- expand.grid(chems = chemArr, times = timeArr)
str(mygrid)
#'data.frame': 21 obs. of 2 variables:
# $ chems: Factor w/ 3 levels "water","oil",..: 1 2 3 1 2 3 1 2 3 1 ...
# $ times: Factor w/ 7 levels "0","8hr","12hr",..: 1 1 1 2 2 2 3 3 3 4 ...
# - attr(*, "out.attrs")=List of 2
# ..$ dim : Named int 3 7
# .. ..- attr(*, "names")= chr "chems" "times"
# ..$ dimnames:List of 2
# .. ..$ chems: chr "chems=water" "chems=oil" "chems=hydrogen"
# .. ..$ times: chr "times=0" "times=8hr" "times=12hr" "times=24hr" ...
mygrid$f_value <- mapply(f, mygrid$chems, mygrid$times)
我有两个数组
chemArr = c("water","oil",...(lots of values omitted)...,"hydrogen")
timeArr = c("0", "8hr", "12hr", "24hr", "36hr", "48hr", "72hr")
我想构建一个数据框
chem 0 8hr ... 72hr
water f("water", "0") f("water", "8hr") ...
...
其中f
是我写的一个函数。在 R 中有没有好的方法来做到这一点?
在我的特定情况下,使一个函数接受 chem
并每次返回一列会更有效,因为这些共享计算中的每一个。但由于所需的总时间很小,如果更方便的话,我可以用其他方式来做。
为了说明评论中讨论的解决方案:
one <- letters[1:3]
two <- letters[4:6]
one
[1] "a" "b" "c"
two
[1] "d" "e" "f"
mapply(paste0, one, two)
a b c
"ad" "be" "cf"
mapply(paste0, sort(rep(one, length(two))), two)
a a a b b b c c c
"ad" "ae" "af" "bd" "be" "bf" "cd" "ce" "cf"
mapply(paste0, one, list(two)) # courtesy of @thelatemail
a b c
[1,] "ad" "bd" "cd"
[2,] "ae" "be" "ce"
[3,] "af" "bf" "cf"
outer(one, two, paste0) # courtesy of @akrun
[,1] [,2] [,3]
[1,] "ad" "ae" "af"
[2,] "bd" "be" "bf"
[3,] "cd" "ce" "cf"
我建议使用 expand.grid
,它创建所有 2 路组合的 "long form",然后使用 mapply 从函数创建值:
chemArr = c("water","oil","hydrogen")
timeArr = c("0", "8hr", "12hr", "24hr", "36hr", "48hr", "72hr")
mygrid <- expand.grid(chemArr, timeArr)
mygrid <- expand.grid(chems = chemArr, times = timeArr)
str(mygrid)
#'data.frame': 21 obs. of 2 variables:
# $ chems: Factor w/ 3 levels "water","oil",..: 1 2 3 1 2 3 1 2 3 1 ...
# $ times: Factor w/ 7 levels "0","8hr","12hr",..: 1 1 1 2 2 2 3 3 3 4 ...
# - attr(*, "out.attrs")=List of 2
# ..$ dim : Named int 3 7
# .. ..- attr(*, "names")= chr "chems" "times"
# ..$ dimnames:List of 2
# .. ..$ chems: chr "chems=water" "chems=oil" "chems=hydrogen"
# .. ..$ times: chr "times=0" "times=8hr" "times=12hr" "times=24hr" ...
mygrid$f_value <- mapply(f, mygrid$chems, mygrid$times)