Is there any method to combine data frames properly?

后端 未结 3 472
野趣味
野趣味 2021-01-29 11:14

I have a problem with combining data frames which share the same rows. I imported rasters from different folders and converted them into data frames and tried to combine them to

3条回答
  •  醉梦人生
    2021-01-29 11:41

    You may adapt this solution. The order you can get by adding an id column before merging.

    res <- Reduce(function(...) merge(..., all=TRUE), 
           Map(`[<-`, All, "id", value=substring(names(All), 2)))
    res[order(res$id), -3]  # order here by `id`
    #         x        y  R2  R1
    # 1  696060 -3327450  NA  66
    # 3  696090 -3327450  NA  71
    # 5  696120 -3327450  NA  69
    # 7  696150 -3327450  NA  65
    # 9  696180 -3327450  NA  67
    # 11 696210 -3327450  NA  68
    # 13 696240 -3327450  NA  67
    # 15 696270 -3327450  NA  68
    # 17 696300 -3327450  NA  69
    # 19 696330 -3327450  NA   0
    # 2  696060 -3327450  66  NA
    # 4  696090 -3327450  71  NA
    # 6  696120 -3327450  69  NA
    # 8  696150 -3327450  65  NA
    # 10 696180 -3327450  67  NA
    # 12 696210 -3327450  68  NA
    # 14 696240 -3327450  67  NA
    # 16 696270 -3327450  68  NA
    # 18 696300 -3327450  69  NA
    # 20 696330 -3327450   0  NA
    # 21 753810 -3339930  NA 109
    # 23 753840 -3339930  NA 108
    # 25 753870 -3339930  NA 108
    # 27 753900 -3339930  NA 109
    # 29 753930 -3339930  NA 108
    # 31 753960 -3339930  NA 109
    # 33 753990 -3339930  NA 109
    # 35 754020 -3339930  NA 109
    # 37 754050 -3339930  NA 110
    # 39 754080 -3339930  NA 109
    # 22 753810 -3339930 109  NA
    # 24 753840 -3339930 108  NA
    # 26 753870 -3339930 108  NA
    # 28 753900 -3339930 109  NA
    # 30 753930 -3339930 108  NA
    # 32 753960 -3339930 109  NA
    # 34 753990 -3339930 109  NA
    # 36 754020 -3339930 109  NA
    # 38 754050 -3339930 110  NA
    # 40 754080 -3339930 109  NA
    

    Data

    All <- list(structure(list(x = c(696060L, 696090L, 696120L, 696150L, 
    696180L, 696210L, 696240L, 696270L, 696300L, 696330L), y = c(-3327450L, 
    -3327450L, -3327450L, -3327450L, -3327450L, -3327450L, -3327450L, 
    -3327450L, -3327450L, -3327450L), R1 = c(66L, 71L, 69L, 65L, 
    67L, 68L, 67L, 68L, 69L, 0L)), row.names = c(NA, -10L), class = "data.frame"), 
        structure(list(x = c(696060L, 696090L, 696120L, 696150L, 
        696180L, 696210L, 696240L, 696270L, 696300L, 696330L), y = c(-3327450L, 
        -3327450L, -3327450L, -3327450L, -3327450L, -3327450L, -3327450L, 
        -3327450L, -3327450L, -3327450L), R2 = c(66L, 71L, 69L, 65L, 
        67L, 68L, 67L, 68L, 69L, 0L)), row.names = c(NA, -10L), class = "data.frame"), 
        structure(list(x = c(753810L, 753840L, 753870L, 753900L, 
        753930L, 753960L, 753990L, 754020L, 754050L, 754080L), y = c(-3339930L, 
        -3339930L, -3339930L, -3339930L, -3339930L, -3339930L, -3339930L, 
        -3339930L, -3339930L, -3339930L), R1 = c(109L, 108L, 108L, 
        109L, 108L, 109L, 109L, 109L, 110L, 109L)), row.names = c(NA, 
        -10L), class = "data.frame"), structure(list(x = c(753810L, 
        753840L, 753870L, 753900L, 753930L, 753960L, 753990L, 754020L, 
        754050L, 754080L), y = c(-3339930L, -3339930L, -3339930L, 
        -3339930L, -3339930L, -3339930L, -3339930L, -3339930L, -3339930L, 
        -3339930L), R2 = c(109L, 108L, 108L, 109L, 108L, 109L, 109L, 
        109L, 110L, 109L)), row.names = c(NA, -10L), class = "data.frame"))
    

提交回复
热议问题