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_Dataframe - Fatal编程技术网

R 为数据帧中的特定点创建指针

R 为数据帧中的特定点创建指针,r,loops,dataframe,R,Loops,Dataframe,在我开始讨论细节之前,先了解一下这个项目的背景。我正在研究一份约50个国家的名单,每个国家的数据大约在40到60年之间。我已经能够为一个国家建立一个循环,它尝试变量的各种值(代码中称为DELTA)并记录结果 我首先引入数据并清理它,使其没有空值,然后使用以下代码创建一个向量,其中包含用于表示每个国家的所有3个字母代码 Clean <- na.omit(Data) Clean <- Clean[order(country.isocode),] Codes <- levels(Cl

在我开始讨论细节之前,先了解一下这个项目的背景。我正在研究一份约50个国家的名单,每个国家的数据大约在40到60年之间。我已经能够为一个国家建立一个循环,它尝试变量的各种值(代码中称为DELTA)并记录结果

我首先引入数据并清理它,使其没有空值,然后使用以下代码创建一个向量,其中包含用于表示每个国家的所有3个字母代码

Clean <- na.omit(Data)
Clean <- Clean[order(country.isocode),]
Codes <- levels(Clean[,2])
现在,我所有的50个国家都在他们自己的数据框架中,以3个字母的ISO代码命名。以下是我运行的代码,用于创建我想要的安哥拉结果(AGO)


首先,这是一个问题

Clean <- Clean[order(country.isocode),]
你可以

xyz <- split(Clean, list(country.isocode))  # or, probably Clean$country.isocode
现在,您可以定义您想对每个国家做什么(我重写了您的代码,但没有试图理解它的作用,但只注意到一个明显的问题):


应该是这样。您可能需要一些修改、尝试和错误,但在我看来,这是一种比使用
assign
创建大量变量更明智的方法

为什么要将其分解为50个独立的数据帧?那可能没有必要。您可能也不需要循环。你能发布一些样本数据吗?我已经添加了样本数据。由于每个国家的年数不同,而且我想应用的函数是递归的,我认为很难让它在每个国家的正确位置开始和停止。你不需要在正确的位置开始和停止,也不需要考虑正确的位置。使用split<代码>?拆分当我最初使用拆分功能时,我无法设置液滴液位。好的,但您确定需要液滴液位吗?可以将相关变量转换为字符(使用as.character)。是的,您可以使用液滴液位-不直接在列表中,但您可以将其应用到列表中。类似于
xxx嗨,我一次接受了两条建议,但写两条命令似乎有些过头了。我已经试过了你说的,但它似乎不适合我正在尝试的。非常感谢,我已经对函数做了一些调整,它很棒。我在0.002的早期初始化了DELTA,并连接了Clean。不熟悉编写自定义函数,也不熟悉它如何与lapply交互,我对此完全不熟悉。非常感谢您和用户3207835
for (k in 1:length(Codes)) {


# Initialize Delta and Create Storage Matrix and Row Count
DELTA <- .01
assign(paste(Codes[k],"_Results", sep=""), matrix(numeric(0), 100,2))
assign(paste(Codes[k],"ROW",sep=""), nrow(eval(as.name(Codes[k]))))
country country.isocode year     POP   rgdpl    ki rgdpl2wok        rgdp investment workers L.P
21  Angola             AGO 1970 5605.63 2366.51 23.27   5904.14 13265745651 3087431388 2246856 0.4 
22  Angola             AGO 1971 5752.96 2445.13 23.25   6127.95 14066747655 3270057880 2295508 0.4
Clean <- Clean[order(country.isocode),]
for (i in 1:length(Codes)) {
assign((Codes[i]),droplevels(subset(Clean,country.isocode==Codes[i])))
}
xyz <- split(Clean, list(country.isocode))  # or, probably Clean$country.isocode
xyz <- split(Clean, list(Clean$country.isocode)) 
xyz <- lapply(xyz, droplevels)  # whatever that's for
doit <- function(x){
    # where does the DELTA come from? do you initialize it to zero?
    # anyway, you need to define it here or pass it as argument
    Results <- matrix(numeric(0), 100,2)  # I'd use 0 or NA instead of numeric(0)
    NROWs<-nrow(x)

    for (j in 1:100) {
      x[1,12]<-x[1,9]/DELTA

    for (i in 2:NROWs) {
      x[i,12] <- x[i-1,12]*(1-DELTA)+x[i,9]
    }

   x[,13] <- x[,12]/x[,8]

   Results[j,1] <- DELTA
   Results[j,2] <- sum(x[,13] > 1 & x[,13] < 3)

   DELTA=DELTA+.002
   }
   Results # returns results
   }
lapply(xyz, doit)