【问题标题】:Tmap Error - replacement has [x] rows, data has [y]Tmap 错误 - 替换有 [x] 行,数据有 [y]
【发布时间】:2018-02-28 02:22:24
【问题描述】:

短版: 执行以下命令时qtm(countries, "freq") 我收到以下错误消息:

$<-.data.frame(*tmp*, "SHAPE_AREAS", value = c(652270.070308042, : 替换有 177 行,数据有 210

免责声明:我已经检查过其他答案,例如 this onethis one 以及 this explanation,它们指出通常此错误来自拼写错误的对象,但找不到答案我的问题。

可重现的代码:

library(rgdal)
library(dplyr)
library(tmap)

# Load JSON file with countries.
countries = readOGR(dsn = "https://gist.githubusercontent.com/ccamara/fc26d8bb7e777488b446fbaad1e6ea63/raw/a6f69b6c3b4a75b02858e966b9d36c85982cbd32/countries.geojson")

# Load dataframe.
df = read.csv("https://gist.githubusercontent.com/ccamara/fc26d8bb7e777488b446fbaad1e6ea63/raw/754ea37e4aba1b7ed88eaebd2c75fd4afcc54c51/sample-dataframe.csv")


countries@data = left_join(countries@data, df, by = c("iso_a2" = "country_code"))

qtm(countries, "freq")

【问题讨论】:

    标签: r tmap


    【解决方案1】:

    您的错误在数据中 - 代码工作正常。

    你现在正在做的是:

    1) 尝试 1:1 比赛

    2) 意识到您的 .csv 数据包含多个要匹配的 id

    3) 左连接,然后将左侧与右侧的所有匹配项相乘

    为避免此问题,您必须再聚合一次数据,例如:

    library(dplyr)
    
    df_unique = df %>%
        group_by(country_code, country_name) %>%
        summarize(total = sum(total), freq = sum(freq))
    
    
    #after that you should be fine - as long as just adding up the data is okay.
    countries@data = left_join(countries@data, df, by = c("iso_a2" = 
    "country_code"))
    
    qtm(countries, "freq")
    

    【讨论】:

    • 非常感谢您的解释和回答。它现在正在工作!
    • 我担心,尽管我认为我已经理解我的问题是什么(左侧数据框中的一个变量与右侧的多个变量匹配,因此,我需要对变量进行分组正确的数据框)我再次面临同样的消息,但我很确定问题是另一个问题。如果你好心回答,我在这里发布了另一个问题:stackoverflow.com/questions/46430561/…。对不起,如果我要求太多@Christian
    猜你喜欢
    • 2015-07-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多