Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中重复包含一个列表组件并排除所有其他组件_R_Loops_Dplyr_Tidyverse - Fatal编程技术网

在R中重复包含一个列表组件并排除所有其他组件

在R中重复包含一个列表组件并排除所有其他组件,r,loops,dplyr,tidyverse,R,Loops,Dplyr,Tidyverse,我有一个价值清单。我想在函数的一部分使用第一个值,在函数的另一部分使用其余的值。然后,我想在函数的一部分中使用列表中的第二个值,并在函数的另一部分中使用剩余的值,包括第一个值 以下不是真正的代码,因为我排除了一些函数输入,但它显示了我正在尝试执行的操作: group <- c(1, 3, 4, 5, 9) #the complete group #It is important to note that these values are not continuous. They are

我有一个价值清单。我想在函数的一部分使用第一个值,在函数的另一部分使用其余的值。然后,我想在函数的一部分中使用列表中的第二个值,并在函数的另一部分中使用剩余的值,包括第一个值

以下不是真正的代码,因为我排除了一些函数输入,但它显示了我正在尝试执行的操作:

group <- c(1, 3, 4, 5, 9) #the complete group 
#It is important to note that these values are not continuous. They are distinct values. 


#include 1st unit of the list group as treatment and then use
#the remaining values as controls 

controls_not2 <- c(3, 4, 5, 9)
df1 <- dataprep(treatment = 1, controls = controls_not1) 
gaps.plot(dataprep = df1) 


#use the 2nd unit from the list as the treatment and use
#the remaining values as controls 

controls_not2 <- c(1, 4, 5, 9)
df2 <- dataprep(treatment = 3, controls = controls_not3) 
gaps.plot(dataprep = df3) 
下面是创建单个图形的完整代码的示例

identifier相当于我在上面的简单示例中的处理。controls.identifier相当于上面示例中的控件

dataprep.out1 <- dataprep(foo = as.data.frame(df), 
                         predictors = predictors1, 
                         predictors.op = "mean", 
                         special.predictors = NULL, 
                         dependent = "logfatalitiespercapita", 
                         unit.variable = "state", 
                         time.variable = "year", 
                     treatment.identifier = "99", 
                     controls.identifier = controlstates, 
                     time.predictors.prior = c(1981:1985), 
                     time.optimize.ssr = c(1981:1985), #check on these years
                     time.plot = 1981:2003, 
                     unit.names.variable = "statenames") 

#identifying weights that create the best possible synthetic control unit for the treated 
synth.out1 <- synth(dataprep.out1)

#graph
spec1 <- gaps.plot(dataprep.res = dataprep.out1, synth.res = synth.out1, Xlab = c("Year"), Ylab = c("Gap"))

首先是注释,您可能不想使用“list”作为向量名称,因为list是R中的一种数据类型。您可以通过引用索引号来子集向量。下面的例子


list1要将结果存储在列表中,这将忽略值,并使用1:50作为索引

library(purrr)
result <- map(1:50, ~{ #replace 50 with the length of your list
df <- dataprep(treatment = group[[.x]], controls = group[[-.x]])
 gaps.plot(dataprep = df)})
这是受上述解决方案启发的类似解决方案

newfunction <- function(x) {
    df <- dataprep(treatment = group[x], controls = group[-x])
     gaps.plot(dataprep = df)
}

graphs <- lapply(group, newfunction) 

@akrun我已经添加了它们。是否应该有另一个括号来关闭初始括号?x是什么意思?你在哪里定义过?当我尝试自己运行它时,它不起作用。当使用purr包时。x是一个代词,可以与map和{}一起使用,请将其想象为function.x{}。您得到的错误是什么?我试着运行你的代码,但是我没有对象df,我没有使用map,而是通过创建一个函数来实现这种方法。list[.x]]不起作用,但list[x]和list[-x]在函数中起作用。然后我使用了lappy。如果您将myVec=1:6更改为myVec=as.vectorgroup,这更接近我要问的问题。然后,它不必按顺序从1到6,并且可以包含组对象中的任何数字。
library(purrr)
result <- map(1:50, ~{ #replace 50 with the length of your list
df <- dataprep(treatment = group[[.x]], controls = group[[-.x]])
 gaps.plot(dataprep = df)})
newfunction <- function(x) {
    df <- dataprep(treatment = group[x], controls = group[-x])
     gaps.plot(dataprep = df)
}

graphs <- lapply(group, newfunction) 
library(tidyverse)

myVec = 1:6

doSomething <- function(treatment, myVec){
  controls = myVec[! myVec == treatment]
  paste("Treatment:", treatment, "Controls:", paste(controls, collapse = "-"))
}
map_chr(myVec, ~ doSomething(.x, myVec))

[1] "Treatment: 1 Controls: 2-3-4-5-6" "Treatment: 2 Controls: 1-3-4-5-6" "Treatment: 3 Controls: 1-2-4-5-6"
[4] "Treatment: 4 Controls: 1-2-3-5-6" "Treatment: 5 Controls: 1-2-3-4-6" "Treatment: 6 Controls: 1-2-3-4-5"