【问题标题】:twitteR search geocode argument in RR中的推特R搜索地理编码参数
【发布时间】:2017-04-18 15:31:01
【问题描述】:

我想使用 twitteR 运行一个简单的搜索,但只返回位于美国的推文很难。

对于只获取美国推文的论点,我会输入什么内容?

谢谢,

【问题讨论】:

    标签: r search twitter


    【解决方案1】:

    我做了一个简短的搜索,看起来 twitteR 没有内置的国家/地区参数。但是由于您有纬度/经度,因此对美国国家形状文件(即多边形中的点)进行空间连接非常简单。

    在这个例子中,我使用shapefile from Census.gov 和包spatialEco 来实现它的point.in.polygon() 功能。与其他软件包提供的相比,它是一个非常快速的空间连接功能,即使您有数十万个坐标和数十个多边形。如果您有数百万条推文——或者如果您稍后决定加入多个多边形,例如所有世界国家——那么它可能会慢很多。但对于大多数用途来说,它非常快。

    (另外,我没有设置 Twitter API,所以我将使用带有 tweet_ids 和 lat/long 的示例数据框。)

    library(maptools) # to 
    library(spatialEco)
    
    # First, use setwd() to set working directory to the folder called cb_2015_us_nation_20m
    us <- readShapePoly(fn = "cb_2015_us_nation_20m")
    # Alternatively, you can use file.choose() and choose the .shp file like so:
    us <- readShapePoly(file.choose())
    
    # Create data frame with sample tweets
    # Btw, tweet_id 1 is St. Louis, 2 is Toronto, 3 is ouston
    tweets <- data.frame(tweet_id = c(1, 2, 3), 
                     latitude = c(38.610543, 43.653226, 29.760427),
                     longitude = c(-90.337189, -79.383184, -95.369803))
    
    # Use point.in.poly to keep only tweets that are in the US
    coordinates(tweets) <- ~longitude+latitude
    tweets_in_us <- point.in.poly(tweets, us)
    tweets_in_us <- as.data.frame(tweets_in_us)
    

    现在,如果您查看 tweets_in_us,您应该只会看到经纬度在美国范围内的推文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      相关资源
      最近更新 更多