使用for循环合并DataFrame实现数据预测:代码检查与完善建议
问题分析与代码完善方案
我有两个包含字符串、日期和数值的DataFrame:df1是2020年的基础数据,df2存储了2021-2025年的增长率(对应H列)。需要将df1的D、E、F列数值分别与df2中对应年份的H列增长率相乘,生成2021-2025年的预测数据。以下是我写的部分代码,需要检查问题并获取完善思路。
原始代码及数据
df1 <- read.csv("df1.csv", check.names=FALSE) df2 <- read.csv("df2.csv", check.names=FALSE) # df1 数据结构 # A B year D E F # abc ab 2020 0 1 2 # def cd 2020 3 4 0 # ghi ef 2020 0 5 6 # jkl gh 2020 7 8 0 # mno ij 2020 0 9 10 # df2 数据结构 # year H # 2021 1.1 # 2022 1.2 # 2023 1.3 # 2024 1.4 # 2025 1.5 df3 <- data.frame() for (i in 1:length(df2)){ df3 = rbind(df1, df2 %>% mutate(df1$all_columns_with_numbers = all_columns_with_numbers[i,] * df2$H[i,] )) } df3 # 期望输出示例 # A B year D E F # abc ab 2021 0 1.1 2.2 # abc ab 2022 0 1.2 2.4 # abc ab 2023 0 1.3 2.6 # abc ab 2024 0 1.4 2.8 # abc ab 2025 0 1.5 3.0 # def cd 2021 3.3 4.4 0 # ...
现有代码的问题
- 循环范围错误:
length(df2)返回的是df2的列数(这里是2),不是需要遍历的年份行数(5),应该用nrow(df2) - 语法逻辑错误:
mutate里直接修改df1的列、使用未定义的all_columns_with_numbers对象,都是无效操作 - 数据绑定错误:每次循环把原始df1和新生成的行绑定,会导致数据重复混乱
- 年份未替换:生成的预测数据没有替换为df2对应的目标年份
完善方案与代码
方案一:用tidyverse工具包(推荐)
利用dplyr和tidyr的函数实现,避免低效循环,逻辑更清晰:
library(tidyverse) # 读取数据 df1 <- read.csv("df1.csv", check.names=FALSE) df2 <- read.csv("df2.csv", check.names=FALSE) # 生成预测数据 df3 <- df1 %>% # 保留标识列和数值列,移除原始2020年份列 select(A, B, D, E, F) %>% # 笛卡尔积连接,让每个原始行对应所有预测年份 crossing(df2) %>% # 计算各列预测值 mutate( D = D * H, E = E * H, F = F * H ) %>% # 调整列顺序,移除中间用的H列 select(A, B, year, D, E, F) %>% # 按标识和年份排序(可选) arrange(A, B, year) # 查看结果 print(df3)
方案二:基础R实现
如果不想加载额外包,可以用基础R循环实现:
# 读取数据 df1 <- read.csv("df1.csv", check.names=FALSE) df2 <- read.csv("df2.csv", check.names=FALSE) # 初始化空结果框 df3 <- data.frame() # 遍历每个预测年份 for (i in 1:nrow(df2)) { current_year <- df2$year[i] current_rate <- df2$H[i] # 复制原始数据,替换年份并计算数值列 temp_df <- df1 temp_df$year <- current_year temp_df[, c("D", "E", "F")] <- temp_df[, c("D", "E", "F")] * current_rate # 绑定到结果 df3 <- rbind(df3, temp_df) } # 排序并重置行名 df3 <- df3[order(df3$A, df3$B, df3$year), ] rownames(df3) <- NULL # 查看结果 print(df3)
内容的提问来源于stack exchange,提问作者dmoyaec
相关产品推荐
相关产品推荐

