程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在 R 中使用循环对文件进行子集化和保存大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在 R 中使用循环对文件进行子集化和保存?

开发过程中遇到在 R 中使用循环对文件进行子集化和保存的问题如何解决?下面主要结合日常开发的经验,给出你关于在 R 中使用循环对文件进行子集化和保存的解决方法建议,希望对你解决在 R 中使用循环对文件进行子集化和保存有所启发或帮助;

我有从 2005 年到 2015 年的面板数据,我想运行一些循环并分别保存每年的输出。这是我的代码:

for (i in 2005:2015){

  ntm_data <-subset(ntm_data_wip,StartDate <="i" & EndDate >"i")

*"inner loops"*

regulatory_distance_matrix$year <-i
write.dta(regulatory_distance_matrix,"C:/Users/Utente/Desktop/Master's thesis/Thesis analysis/- RD construction/Binary RD/regulatory_distance_matrix_",i,".dta")
} 

如果我手动设置子集并选择一年,则内部循环会起作用。但是,当我按上述方式运行它时,出现以下错误:

 `summarise()` has grouped output by 'reporter','ntmcode'. You can overrIDe using the `.groups` argument.
Error in if (!is.na(regulatory_distance_matrix[k,avail_iso3s[g]])) { : 
  l'argomento ha lunghezza zero
Inoltre: Warning messages:
1: In max(ntm_data$startDate,na.rm = falSE) :
  no non-missing arguments to max; returning -Inf
2: In min(ntm_data$EndDate,na.rm = falSE) :
  no non-missing arguments to min; returning Inf

有人知道怎么解决吗? 提前致谢

解决方法

非常感谢您的及时回复,非常感谢!我在此附上整个代码并附上一些简短的评论:

for (i in 2005:2007){

  ntm_data <-subset(ntm_data_wip,StartDate <=i & EndDate >i)

# Once the data is loaded,I exclude NTM codes which are missing. 
# I only need the reporter,NTM code and product codes (HS 6-digit codes).
ntm_data <- ntm_data[!is.na(ntm_data$ntmcodE)&ntm_data$ntmcode!="",]
ntm_data <- ntm_data[,c("reporter","ntmcode","hs6")]

# I group the data by reporter,NTM and product code (hs6) and count the number of combinations in a new variable called count.
ntm_data <- ntm_data %>% group_by(reporter,ntmcode,hs6) %>%
  summarise(count = n())
head(ntm_data)

# I prepare the regulatory matrix by creaTing a list of countries for which I want the regulatory distance.  The 
# regulatory matrix shows the distance between two countries and has as column and row names the ISO3 codes of the countries.
# As specified above,I am interested in having the analysis for all available countries. 
avail_iso3s <- unique(ntm_data$reporter)

# I create an empty regulatory distance matrix. For column size I use the length of avail_iso3s and add 1 for the reporter column.
# I populate the column names with reporter and the ISO3 codes with the option dimnames.
regulatory_distance_matrix <- data.frame(matrix(vector(),length(avail_iso3s)+1,dimnames = list(c(),avail_iso3s )
                                                )),StringsAsFactors=F)


#' Now I can move on to calculaTing the regulatory distance formula in page 3 of "DEEP REGIONAL INTEGRATION AND NON-TARIFF MEASURES:A METHODOLOGY FOR DATA ANALYSIS (2015)" . 
#' As N is a constant,I start with calculaTing it outside of the loop
N <- ntm_data %>% group_by(ntmcode,hs6) %>% count()
N <- nrow(N)

# I now fill in the regulatory distance matrix with values

for (g in 1:length(avail_iso3s)){
  country_a <- ntm_data[ntm_data$reporter==avail_iso3s[g],c("ntmcode","hs6")]
  country_a$country_a <- 1
  regulatory_distance_matrix[g,"reporter"] <- avail_iso3s[g]
  
  for (k in 1:length(avail_iso3s)){
    
    if (!is.na(regulatory_distance_matrix[k,avail_iso3s[g]])){next }
    
    country_b <- ntm_data[ntm_data$reporter==avail_iso3s[k],"hs6")]
    country_b$country_b <- 1
    merged <- merge(country_a,country_b,by=c("ntmcode","hs6"),all = TRUE)
    merged[is.na(merged)] <- 0
    merged$abs_diff <- abs(merged$country_a-merged$country_b)
    rd <- sum(merged$abs_diff)/N
    regulatory_distance_matrix[g,avail_iso3s[k]] <- rd
    
  }
}

# Now I fill in the missing values and create a Stata dta.file.
for (g in 1:length(avail_iso3s)){
  for (k in 1:length(avail_iso3s)){
    if (is.na(regulatory_distance_matrix[k,avail_iso3s[g]])){
      regulatory_distance_matrix[k,avail_iso3s[g]] <- regulatory_distance_matrix[g,avail_iso3s[k]]
    }
  }
}


regulatory_distance_matrix$year <-i
write.dta(regulatory_distance_matrix,"C:/Users/Utente/Desktop/Master's thesis/Thesis analysis/- RD construction/Binary RD/new_regulatory_distance_matrix_",i,".dta")
} 

我希望这有用。此外,在遵循您的建议后,我收到以下错误:

`summarise()` has grouped output by 'reporter','ntmcode'. You can override using the `.groups` argument.
Error in if (convert.dates) { : 
  l'argomento non può esserE interpretato come logico
Inoltre: Warning message:
In write.dta(regulatory_distance_matrix,:
  Version must be 6-12: using 7

对于这个麻烦的问题,我深表歉意,但我对 R 很陌生,其他帖子似乎没有多大帮助。

大佬总结

以上是大佬教程为你收集整理的在 R 中使用循环对文件进行子集化和保存全部内容,希望文章能够帮你解决在 R 中使用循环对文件进行子集化和保存所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。