为什么mapply重复相同的列表多次?

时间:2022-05-18 08:46:18

Please let me start by providing synthetic data set that shows the issues:

请允许我首先提供显示问题的综合数据集:

Do <- rep(c(0,2,4,6,8,10,15,20,30,40,45,50,55,60,65,70,80,85,90,92,94,96,98,100), each=16,times=16)
Cl <- rep(c("K", "Y","M","C"), each= 384, times=4)
In <- rep(c("A", "S"), each=3072)
Sa <- rep(c(1,2), each=1536)
Data <- rnorm(6144)
DataFrame <- cbind.data.frame(Do,Cl,In,Sa,Data); head(DataFrame)
rm(Do,Cl,In,Sa,Data)
attach(DataFrame)

Next, I split the 'DataFrame' object into multiple lists to avoid unpredictable recycling. Basically, I am placing each data subset in a separate list so that cycling is predictable and that produced the correct output in my simulator.

接下来,我将“DataFrame”对象拆分为多个列表,以避免不可预知的回收。基本上,我将每个数据子集放在一个单独的列表中,以便循环是可预测的,并在模拟器中生成正确的输出。

DFSplit <- split(DataFrame[ , "Data"], list(Do, Cl, In, Sa))

The 'DFSplit' object has 384 lists

“DFSplit”对象有384个列表

length(names(DFSplit))

Then I created the function 'ids' to identify the lists names

然后我创建了“id”函数来识别列表名

ids <- function(Do, Cl, In, Sa){
    grep( paste( "^" , Do, "\\.",
                Cl, "\\.",
                In,
                "\\.", Sa,sep=""),
         names(DFSplit), value = TRUE)}

mapply(ids, Do, Cl, In, Sa, SIMPLIFY = FALSE)

I understand that each of 'ids' arguments' length is 6144. mapply produces 384 lists each repeated 16 times. How can I change the ids function so that mapply doesn't repeat the same name 16 times. As an ugly and highly costly solution I used unique; i need a better fundamental solution.

我知道每个'id '参数的长度是6144。mapply生成384个列表,每个列表重复16次。如何更改ids函数,使mapply不会重复相同的名称16次。作为一种丑陋而昂贵的解决方案,我使用了unique;我需要一个更好的基本解。

unique(mapply(ids, Do, Cl, In, Sa, SIMPLIFY = FALSE))

I also created a function to operate on the 'DFSplit' lists. It has the same issue as the previous function. The thing is, it accepts the previous function as an input.

我还创建了一个对“DFSplit”列表进行操作的函数。它的问题与前面的函数相同。它接受前面的函数作为输入。

dG <- function(Do,Cl, In, Sa){
    dg <- 100*
                (1-10^-( DFSplit[[ids(Do,  Cl, In, Sa)]] - DFSplit[[ids(0, Cl, In, Sa)]])) /
                (1-10^-( DFSplit[[ids(100, Cl, In, Sa)]] - DFSplit[[ids(0, Cl, In, Sa)]])) - Do
    dg}

mapply(dG, Do, Cl, In, Sa, SIMPLIFY = FALSE)

What I am trying to do, unsuccessfully if I may say, is to apply the dG function inside each of the 384 lists. I acknowledge that dG function also needs to be modified and I don't know how. I want the input to the dG function to be the names of 384 lists each containing 16 numbers. I want the output to be 384 list with the dG applied.

我想要做的,如果我可以说,是在384个列表中应用dG函数,但没有成功。我承认dG函数也需要修改,我不知道怎么修改。我希望dG函数的输入是包含16个数字的384个列表的名称。我希望输出为384列表,并应用dG。

Please feel free to suggest a different solution all together. The important thing is I need to apply the 'dG' function to the data set.

请随意提出不同的解决方案。重要的是我需要将“dG”函数应用到数据集。

1 个解决方案

#1


4  

Please take a closer look at what you are giving mapply Each object is of length 6144.

请仔细看看你给mapply的对象的长度是6144。

  > length(Do)
  [1] 6144
  > length(Cl)
  [1] 6144
  > length(In)
  [1] 6144
  > length(Sa)
  [1] 6144
  > 

You are giving mapply 6144 tuples and asking it to iterate over each.
It is giving you back a list of 6144 elements.

您将为mapply提供6144个元组,并要求它对每个元组进行迭代。它返回一个包含6144个元素的列表。

It is exactly what you are telling it to do

这正是你要它做的


Also, just copying and pasting your code yields a list 6144 long, each element containing 16 elements.

另外,只需复制和粘贴代码,就会生成一个6144长的列表,每个元素包含16个元素。

  .
  .
  [[6141]]
   [1]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  1.421085e-14
  [12]  0.000000e+00  0.000000e+00  0.000000e+00 -1.421085e-14  0.000000e+00

  [[6142]]
   [1]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  1.421085e-14
  [12]  0.000000e+00  0.000000e+00  0.000000e+00 -1.421085e-14  0.000000e+00

  [[6143]]
   [1]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  1.421085e-14
  [12]  0.000000e+00  0.000000e+00  0.000000e+00 -1.421085e-14  0.000000e+00

  [[6144]]
   [1]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  1.421085e-14
  [12]  0.000000e+00  0.000000e+00  0.000000e+00 -1.421085e-14  0.000000e+00

Therefore, not 6144 of 1 element as you describe.

因此,不是你描述的1个元素的6144。

You received two very good pieces of advice, one form @Arun and one from @Paul Hiemstra.

你收到了两个非常好的建议,一个是@Arun,一个是@Paul Hiemstra。

Perhaps you can try describing what it is you are attempting to accomplish and folks here can better assist you. Also, please dont forget to look back on your previous questions and up vote and thank those who have given you helpful answers.

也许你可以试着描述你想要完成的事情,这里的人可以更好地帮助你。同时,请不要忘记回顾你之前的问题并进行投票,并感谢那些给了你有用答案的人。

#1


4  

Please take a closer look at what you are giving mapply Each object is of length 6144.

请仔细看看你给mapply的对象的长度是6144。

  > length(Do)
  [1] 6144
  > length(Cl)
  [1] 6144
  > length(In)
  [1] 6144
  > length(Sa)
  [1] 6144
  > 

You are giving mapply 6144 tuples and asking it to iterate over each.
It is giving you back a list of 6144 elements.

您将为mapply提供6144个元组,并要求它对每个元组进行迭代。它返回一个包含6144个元素的列表。

It is exactly what you are telling it to do

这正是你要它做的


Also, just copying and pasting your code yields a list 6144 long, each element containing 16 elements.

另外,只需复制和粘贴代码,就会生成一个6144长的列表,每个元素包含16个元素。

  .
  .
  [[6141]]
   [1]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  1.421085e-14
  [12]  0.000000e+00  0.000000e+00  0.000000e+00 -1.421085e-14  0.000000e+00

  [[6142]]
   [1]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  1.421085e-14
  [12]  0.000000e+00  0.000000e+00  0.000000e+00 -1.421085e-14  0.000000e+00

  [[6143]]
   [1]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  1.421085e-14
  [12]  0.000000e+00  0.000000e+00  0.000000e+00 -1.421085e-14  0.000000e+00

  [[6144]]
   [1]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  1.421085e-14
  [12]  0.000000e+00  0.000000e+00  0.000000e+00 -1.421085e-14  0.000000e+00

Therefore, not 6144 of 1 element as you describe.

因此,不是你描述的1个元素的6144。

You received two very good pieces of advice, one form @Arun and one from @Paul Hiemstra.

你收到了两个非常好的建议,一个是@Arun,一个是@Paul Hiemstra。

Perhaps you can try describing what it is you are attempting to accomplish and folks here can better assist you. Also, please dont forget to look back on your previous questions and up vote and thank those who have given you helpful answers.

也许你可以试着描述你想要完成的事情,这里的人可以更好地帮助你。同时,请不要忘记回顾你之前的问题并进行投票,并感谢那些给了你有用答案的人。