【问题标题】:merging outputs from a loop合并循环的输出
【发布时间】:2020-12-18 21:22:27
【问题描述】:

我有两个数据集,分别命名为 E 和 eF。

E<- structure(list(Inception_Date = structure(c(962323200, 962323200, 
810950400, 988675200, 1042502400, 1536624000), tzone = "UTC", class =  
c("POSIXct","POSIXt")), Name = c("Calvert Social Index B", "Calvert US   
Large Cap Core Rspnb Idx A", "Green Century Equity Individual 
Investor", "Praxis Value Index A", "Vanguard FTSE Social Index I", 
"Amundi IS Amundi MSCI USA SRI ETF DR")), row.names = c(NA, -6L), 
class = c("tbl_df", "tbl", "data.frame"))

eF <- structure(list(Inception_Date = structure(c(760233600, 519868800, 
1380067200, 1101772800, 1325203200, 628473600, 1325203200, 1123804800
), tzone = "UTC", class = c("POSIXct", "POSIXt")), Name = c("Amana     
Growth Investor", "Amana Income Investor", "Amana Income   
Institutional", "American Century Sustainable Equity A", 
"Ariel Appreciation Institutional", "Ariel Appreciation Investor", 
"Ariel Focus Institutional", "Baywood Socially Responsible Invs"
)), row.names = c(NA, -8L), class = c("tbl_df", "tbl", "data.frame"
))

我将以下代码应用于数据 E 和 eF。

for (k in 1:nrow(E)) {
F_temp <- eF;
G_temp <- F_temp %>% filter(abs(F_temp$Inception_Date-    
E$Inception_Date[k]) <= 1500);
print(G_temp)}

由于“全球环境”下的“G_temp”显示为0 obs。仅包含 2 个变量(必须是循环列表中的最后一个组件),如何制作一个 .csv 文件,显示所有“G_temp”组件合并在一起删除重复项?

谢谢

【问题讨论】:

    标签: r loops merge


    【解决方案1】:

    使用您的确切过滤条件可以做到这一点吗?

    G_temp <- data.frame(Inception_Date = as.POSIXct(character()),
                         Name = character())
    for (k in 1:nrow(E)) {
      G_temp_int <- eF %>%
        filter(abs(eF$Inception_Date - E$Inception_Date[k]) <= 1500)
      G_temp <- bind_rows(G_temp, G_temp_int)
    }
    
    G_temp <- G_temp %>%
      distinct(Inception_Date, Name)
    write.csv(G_temp, "G_temp.csv")
    

    【讨论】:

      猜你喜欢
      • 2022-12-18
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 2015-11-18
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      相关资源
      最近更新 更多