繁体   English   中英

R-如何在循环中保存循环结果?

[英]R - How to save the results of a loop within a loop?

好的,所以目标是通过一个循环读取包含特定于气象站的气象信息的csv文件,该循环提供开始日期,结束日期以及干球温度和湿度的百分数以及站号。 到目前为止,我已经能够制作一个循环,为我提供一个csv文件的信息以及6-20个电台的数据,因此效果很好。 现在,我试图在第一个循环中设置第一个循环,该循环从一个文件夹运行多头csv文件,但是我只能得到工作站ID,而不能得到其他四个结果。 我想知道如何以列表,单独的向量或数据框的形式保存所有这些数据。

这是代码。

files <- list.files("R_Test_Nicholas", pattern="*.csv", full.names=TRUE)
l <- length(files)
r <- NULL
first <- NULL
for(k in 1:l) {
  first <- read.csv(files[k])
  library(openair)
  station <-unique(first$STN_ID)
  n = length(station)
  first$date <- paste(first$LOCAL_YEAR, first$LOCAL_MONTH, first$LOCAL_DAY, first$LOCAL_HOUR, sep=" ")
  first$date <- as.POSIXct(first$date, format="%Y %m %d %H", "UTC")

  result7 <- NULL
  for (i in 1:n) {
    curr_station <- subset(first, STN_ID==station[i], select=c(date, DRY_BULB_TEMP, HUMIDEX, LOCAL_YEAR))
    test <- cutData(curr_station, type = "season")
    timers <- selectByDate(test, start = "YYYY-mm-dd", year = 1971:2000)
    trial <- subset(timers, season == "summer (JJA)")
    Ptile <- quantile(trial$DRY_BULB_TEMP, c(0.95), na.rm=T)
    Htile <- quantile(trial$HUMIDEX, c(0.95), na.rm=T)
    date_start <- min(trial$LOCAL_YEAR, na.rm=T)
    date_last <- max(trial$LOCAL_YEAR, na.rm=T)
    result7$stn[i] <- station[i]
    result7$Ptile[i] <- Ptile
    result7$Htile[i] <- Htile
    result7$date_start[i] <- date_start
    result7$date_last[i] <- date_last
  }
  r$STN_ID[k] <- result7
  r$Ptile[k] <- result7$Ptile[i]
  r$Htile[k] <- result7$Htile[i]
  r$date_start[k] <- result7$date_start[i]
  r$date_last[k] <-result7$date_last[i]
}
r
$STN_ID
$STN_ID[[1]]
[1] 6130 6137 6140 6157 6205 6207 6250 6256 6915 6916 6918 6919 7026 7558 9821

$STN_ID[[2]]
[1] 10808 10981 26823 26968 27746 29493 30309 30598 42103 43323 43383 45090 48568 48768 50309 50310 51537

$STN_ID[[3]]
[1] 6312 6330 6339 6345 6354 6356 6358 6369 6399 6442 6454 6465 6468 6486 6491 6501 6516 6923 7103 7162 7169 7173 8990 9033 9833

$STN_ID[[4]]
[1] 10078 10661 10792 10848 10859 10914 10936 10945 10969 10970 26824 26864 27141 27223 27592 27600 27868 30326 30668 31829 41575 42083 42243 43123 43124 43183 43403 43404 43405 43406 44363
[32] 44503 46007 47187 48668 49748 50133 50408 50620

$STN_ID[[5]]
[1] 6526 6547 7177

$STN_ID[[6]]
[1] 10800 10814 27846 30308 31029 41903 50621


$Ptile
[1] 23.9   NA 19.5   NA 21.2   NA

$Htile
[1] 29.35    NA 24.34    NA 26.96    NA

$date_start
[1] 1986  Inf 1986  Inf 1978  Inf

$date_last
[1] 2000 -Inf 1996 -Inf 2000 -Inf

您可以使用[[运算符来创建和访问list元素。 由于缺少可复制的数据,因此将mtcars数据集用于此演示。

data(mtcars)
NCol = ncol(mtcars)
ObjList=list()

for(i in 1:NCol){

ObjList[[i]]=list()
ObjList[[i]]['max']=max(mtcars[,i])
ObjList[[i]]['min']=min(mtcars[,i])
ObjList[[i]]['mean']=mean(mtcars[,i])
ObjList[[i]]['sd']=sd(mtcars[,i])
ObjList[[i]][["head"]]=head(mtcars)
ObjList[[i]][["tail"]]=tail(mtcars)


}

max_vec=as.vector(do.call(cbind,lapply(ObjList,function(x) x[["max"]])))
#[1]  33.900   8.000 472.000 335.000   4.930   5.424  22.900   1.000   1.000   5.000   8.000


max_orig=as.vector(apply(mtcars,2,max))
#[1]  33.900   8.000 472.000 335.000   4.930   5.424  22.900   1.000   1.000   5.000   8.000

setdiff(max_vec,max_orig)
#numeric(0)

没有简短的数据集很难重现代码并理解您想要做什么,但是我看到您应该使用矩阵来收集数据

result7 <-matrix(ncol =?,nrow =?))#而不是NULL

for(i in 1:n){...(其余的代码,然后得到结果时,将它们收集:) result7 [i,1] <-结果的计算结果将在第i行,第一列result7 [i,2] <-计算结果B将位于i行和第二列}

您可以将其转换为数据框,然后为各列命名:

result7 <-data.frame(result7)
colnames(georef.data)<-c(“此处的列名)

当您完成所有数据帧时,只需使用rbind按行连接数据帧即可,这些行可以由ID定义。

r$STN_ID[k] <- result7
  r$Ptile[k] <- result7[2]
  r$Htile[k] <- result7[3]
  r$date_start[k] <- result7[4]
  r$date_last[k] <-result7[5]

这就是我最终所做的,这使我在列表中得到了一堆列表,然后将其更改为数据帧。 感谢那些花时间回答的人。

这是一个关于在循环中将新数据连接到现有数据帧的常见问题的非常具体的示例。 为了完整起见,我想分享一种通用的方法。

这个想法是从一个空的数据帧开始,在一个循环中进行计算,然后使用rbind()添加数据。 所以...

df <- NULL
for (n in seq(1,10)){

df <- rbind(df,
            data.frame(n = n,
                       n.squared = n)
}

在此示例中,您将获得一个10行数据帧,其中包含列nn.squared

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM