代码之家  ›  专栏  ›  技术社区  ›  firmo23

使用for循环根据与其他数据帧的匹配来填充数据帧的空值

  •  0
  • firmo23  · 技术社区  · 1 年前

    我有这个几乎是空的数据帧

    new_df<-structure(list(id = c("R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", 
    "R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", 
    "R_88j7lG37gLfxk22"), choice = c(0, 1, 0, 0, 0, 1), low_env = c(NA, 
    NA, NA, NA, NA, NA), mid_env = c(NA, NA, NA, NA, NA, NA), high_env = c(NA, 
    NA, NA, NA, NA, NA), low_eth = c(NA, NA, NA, NA, NA, NA), mid_eth = c(NA, 
    NA, NA, NA, NA, NA), high_eth = c(NA, NA, NA, NA, NA, NA), `low_pri($25)` = c(NA, 
    NA, NA, NA, NA, NA), `mid_pri($75)` = c(NA, NA, NA, NA, NA, NA
    ), `high_pri($125)` = c(NA, NA, NA, NA, NA, NA)), row.names = c(NA, 
    6L), class = "data.frame")
    

    我想填写的内容

    enter image description here

    我使用下面的数据和方法,它适用于前3行,但不适用于下3行(省略选项,它有效)

        long1<-structure(list(id = "R_88j7lG37gLfxk22", t1_choice = "2", t2_choice = "1", 
        t3_choice = "1", t4_choice = "2", t1_p1_env = "high_env", 
        t1_p1_eth = "low_eth", t1_p1_pri = "$125", t1_p2_env = "mid_env", 
        t1_p2_eth = "high_eth", t1_p2_pri = "$25", t1_p3_env = "low_env", 
        t1_p3_eth = "mid_eth", t1_p3_pri = "$75", t2_p1_env = "high_env", 
        t2_p1_eth = "low_eth", t2_p1_pri = "$75", t2_p2_env = "mid_env", 
        t2_p2_eth = "mid_eth", t2_p2_pri = "$125", t2_p3_env = "mid_env", 
        t2_p3_eth = "mid_eth", t2_p3_pri = "$75", t3_p1_env = "high_env", 
        t3_p1_eth = "high_eth", t3_p1_pri = "$125", t3_p2_env = "mid_env", 
        t3_p2_eth = "low_eth", t3_p2_pri = "$25", t3_p3_env = "low_env", 
        t3_p3_eth = "high_eth", t3_p3_pri = "$25", t4_p1_env = "low_env", 
        t4_p1_eth = "low_eth", t4_p1_pri = "$75", t4_p2_env = "high_env", 
        t4_p2_eth = "mid_eth", t4_p2_pri = "$125", t4_p3_env = "low_env", 
        t4_p3_eth = "high_eth", t4_p3_pri = "$25"), row.names = c(NA, 
    -1L), class = c("tbl_df", "tbl", "data.frame"))
    
    #working
    # Loop through the first three rows of new_df
    for (i in 1:3) {
      # Extracting the required values from long1 for each row
      env <- long1[paste0("t1_p", i, "_env")][1]
      eth <- long1[paste0("t1_p", i, "_eth")][1]
      pri <- long1[paste0("t1_p", i, "_pri")][1]
     
       # Matching values from long1 to new_df columns in the corresponding row
       new_df[i, "low_env"] <- as.numeric(env == "low_env")
       new_df[i, "mid_env"] <- as.numeric(env == "mid_env")
       new_df[i, "high_env"] <- as.numeric(env == "high_env")
       new_df[i, "low_eth"] <- as.numeric(eth == "low_eth")
       new_df[i, "mid_eth"] <- as.numeric(eth == "mid_eth")
       new_df[i, "high_eth"] <- as.numeric(eth == "high_eth")
       new_df[i, "low_pri($25)"] <- as.numeric(pri == "$25")
       new_df[i, "mid_pri($75)"] <- as.numeric(pri == "$75")
       new_df[i, "high_pri($125)"] <- as.numeric(pri == "$125")
     
    }
    #not working
    # Loop through the second three rows of new_df
    for (i in 1:3) {
      # Extracting the required values from long1 for each row
      env <- long1[paste0("t2_p", i, "_env")][1]
      eth <- long1[paste0("t2_p", i, "_eth")][1]
      pri <- long1[paste0("t2_p", i, "_pri")][1]
      for(j in 4:6){
        # Matching values from long1 to new_df columns in the corresponding row
        new_df[j, "low_env"] <- as.numeric(env == "low_env")
        new_df[j, "mid_env"] <- as.numeric(env == "mid_env")
        new_df[j, "high_env"] <- as.numeric(env == "high_env")
        new_df[j, "low_eth"] <- as.numeric(eth == "low_eth")
        new_df[j, "mid_eth"] <- as.numeric(eth == "mid_eth")
        new_df[j, "high_eth"] <- as.numeric(eth == "high_eth")
        new_df[j, "low_pri($25)"] <- as.numeric(pri == "$25")
        new_df[j, "mid_pri($75)"] <- as.numeric(pri == "$75")
        new_df[j, "high_pri($125)"] <- as.numeric(pri == "$125")
      }
      
    }
    
    1 回复  |  直到 1 年前
        1
  •  1
  •   Umar    1 年前
    # Create new_df
    new_df <- structure(list(id = c("R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", 
                                    "R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", 
                                    "R_88j7lG37gLfxk22"), choice = c(0, 1, 0, 0, 0, 1), low_env = c(NA, 
                                                                                                    NA, NA, NA, NA, NA), mid_env = c(NA, NA, NA, NA, NA, NA), high_env = c(NA, 
                                                                                                                                                                           NA, NA, NA, NA, NA), low_eth = c(NA, NA, NA, NA, NA, NA), mid_eth = c(NA, 
                                                                                                                                                                                                                                                 NA, NA, NA, NA, NA), high_eth = c(NA, NA, NA, NA, NA, NA), `low_pri($25)` = c(NA, 
                                                                                                                                                                                                                                                                                                                               NA, NA, NA, NA, NA), `mid_pri($75)` = c(NA, NA, NA, NA, NA, NA
                                                                                                                                                                                                                                                                                                                               ), `high_pri($125)` = c(NA, NA, NA, NA, NA, NA)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                               6L), class = "data.frame")
    
    # Create long1
    long1 <- structure(list(id = "R_88j7lG37gLfxk22", t1_choice = "2", t2_choice = "1", 
                            t3_choice = "1", t4_choice = "2", t1_p1_env = "high_env", 
                            t1_p1_eth = "low_eth", t1_p1_pri = "$125", t1_p2_env = "mid_env", 
                            t1_p2_eth = "high_eth", t1_p2_pri = "$25", t1_p3_env = "low_env", 
                            t1_p3_eth = "mid_eth", t1_p3_pri = "$75", t2_p1_env = "high_env", 
                            t2_p1_eth = "low_eth", t2_p1_pri = "$75", t2_p2_env = "mid_env", 
                            t2_p2_eth = "mid_eth", t2_p2_pri = "$125", t2_p3_env = "mid_env", 
                            t2_p3_eth = "mid_eth", t2_p3_pri = "$75", t3_p1_env = "high_env", 
                            t3_p1_eth = "high_eth", t3_p1_pri = "$125", t3_p2_env = "mid_env", 
                            t3_p2_eth = "low_eth", t3_p2_pri = "$25", t3_p3_env = "low_env", 
                            t3_p3_eth = "high_eth", t3_p3_pri = "$25", t4_p1_env = "low_env", 
                            t4_p1_eth = "low_eth", t4_p1_pri = "$75", t4_p2_env = "high_env", 
                            t4_p2_eth = "mid_eth", t4_p2_pri = "$125", t4_p3_env = "low_env", 
                            t4_p3_eth = "high_eth", t4_p3_pri = "$25"), row.names = c(NA, 
                                                                                      -1L), class = c("tbl_df", "tbl", "data.frame"))
    
    # Loop through the first three rows of new_df
    for (i in 1:3) {
      # Extracting the required values from long1 for each row
      env <- long1[paste0("t1_p", i, "_env")][1]
      eth <- long1[paste0("t1_p", i, "_eth")][1]
      pri <- long1[paste0("t1_p", i, "_pri")][1]
      
      # Matching values from long1 to new_df columns in the corresponding row
      new_df[i, "low_env"] <- as.numeric(env == "low_env")
      new_df[i, "mid_env"] <- as.numeric(env == "mid_env")
      new_df[i, "high_env"] <- as.numeric(env == "high_env")
      new_df[i, "low_eth"] <- as.numeric(eth == "low_eth")
      new_df[i, "mid_eth"] <- as.numeric(eth == "mid_eth")
      new_df[i, "high_eth"] <- as.numeric(eth == "high_eth")
      new_df[i, "low_pri($25)"] <- as.numeric(pri == "$25")
      new_df[i, "mid_pri($75)"] <- as.numeric(pri == "$75")
      new_df[i, "high_pri($125)"] <- as.numeric(pri == "$125")
    }
    
    # Loop through the second three rows of new_df
    for (i in 1:3) {
      # Extracting the required values from long1 for each row
      env <- long1[paste0("t2_p", i, "_env")][1]
      eth <- long1[paste0("t2_p", i, "_eth")][1]
      pri <- long1[paste0("t2_p", i, "_pri")][1]
      
      # Matching values from long1 to new_df columns in the corresponding row
      new_df[i + 3, "low_env"] <- as.numeric(env == "low_env")
      new_df[i + 3, "mid_env"] <- as.numeric(env == "mid_env")
      new_df[i + 3, "high_env"] <- as.numeric(env == "high_env")
      new_df[i + 3, "low_eth"] <- as.numeric(eth == "low_eth")
      new_df[i + 3, "mid_eth"] <- as.numeric(eth == "mid_eth")
      new_df[i + 3, "high_eth"] <- as.numeric(eth == "high_eth")
      new_df[i + 3, "low_pri($25)"] <- as.numeric(pri == "$25")
      new_df[i + 3, "mid_pri($75)"] <- as.numeric(pri == "$75")
      new_df[i + 3, "high_pri($125)"] <- as.numeric(pri == "$125")
      
      # Adjusting the choice column
      new_df[i + 3, "choice"] <- as.numeric(long1[paste0("t2_choice")][1] == i)
    }
    
    
    new_df
                     id choice low_env mid_env high_env low_eth mid_eth high_eth low_pri($25) mid_pri($75)
    1 R_88j7lG37gLfxk22      0       0       0        1       1       0        0            0            0
    2 R_88j7lG37gLfxk22      1       0       1        0       0       0        1            1            0
    3 R_88j7lG37gLfxk22      0       1       0        0       0       1        0            0            1
    4 R_88j7lG37gLfxk22      1       0       0        1       1       0        0            0            1
    5 R_88j7lG37gLfxk22      0       0       1        0       0       1        0            0            0
    6 R_88j7lG37gLfxk22      0       0       1        0       0       1        0            0            1
      high_pri($125)
    1              1
    2              0
    3              0
    4              0
    5              1
    6              0