从函数返回多个向量并添加到数据框

时间:2017-01-13 20:31:44

标签: r

一个例子:

library(tidyverse)
library(ggmap)

latLong <- data.frame(long= c(78.0422, -0.1246, 131.035904, 122.3493), 
                     lat = c(27.1750, 51.5007, -25.344646, 47.6205), 
                     name = c("Taj Mahal", "Big Ben", "Uluru", "Space Needle"))

通过反向地理编码查找找到lat / longs的地址详细信息。 该函数返回一个数据框。

results <- revgeocode(c(78.0422, 27.1750), output = "more")

我们希望返回任意大量的结果 通过将函数应用于包含的向量 经纬度,写出结果 回到数据框架。

如果我们想要带回单个值,我们可以使用mapply。

getLocality <- function(long, lat) {
  locality <- revgeocode(c(long, lat), output = "more")
  locality <- locality$locality
  return(locality)
}

localities <- mapply(getLocality, latLong$long, latLong$lat)

我们可以使用该函数将值返回到数据框 作为新专栏。

latLong$locality <- mapply(getLocality, latLong$long, latLong$lat)

我们也可以使用dplyr。

latLong <- latLong %>%
  rowwise() %>%
  mutate(dplyrLocality = getLocality(long, lat))

到目前为止的输出:

|long    |lat      |name        |locality      |dplyrLocality |
|--------|---------|------------|--------------|--------------|
|78.0422 |27.17500 |Taj Mahal   |Agra          |Agra          |
|-0.1246 |51.50070 |Big Ben     |London        |London        |
|131.0359|-25.34465|Uluru       |Petermann     |Petermann     |
|122.3493|47.62050 |Space Needle|Hulunbeier Shi|Hulunbeier Shi|

如果我们想要回写所有结果集呢?

getAddress <- function(long, lat) {
  address <- revgeocode(c(long, lat), output = "more")
  return(address)
}

将整个数据帧写回的最干净/最有效/最容易理解的方法是什么? latLong数据帧作为多个新列?你能提供一些替代方法吗,也许一个使用apply系列,一个使用dplyr?

1 个答案:

答案 0 :(得分:2)

我认为您可以使用purrr包轻松完成。 map2_df()接受两个参数并返回data.frame

library(purrr)

df <- data.frame(latLong,map2_df(latLong$long,latLong$lat,getAddress))

str(df)
'data.frame':   4 obs. of  15 variables:
 $ long                       : num  78.042 -0.125 131.036 122.349
 $ lat                        : num  27.2 51.5 -25.3 47.6
 $ name                       : Factor w/ 4 levels "Big Ben","Space Needle",..: 3 1 4 2
 $ address                    : chr  "Dashehara Ghat Rd, Dharmapuri, Forest Colony, Tajganj, Agra, Uttar Pradesh 282006, India" "Palace of Westminster, Westminster, London SW1A 2PW, UK" "Uluru Rd, Petermann NT 0872, Australia" "Unnamed Road, Zhalantun Shi, Hulunbeier Shi, Neimenggu Zizhiqu, China"
 $ route                      : chr  "Dashehara Ghat Road" NA "Uluru Road" "Unnamed Road"
 $ neighborhood               : chr  "Dharmapuri" "Westminster" NA NA
 $ political                  : chr  "Tajganj" NA NA "Zhalantun Shi"
 $ locality                   : chr  "Agra" "London" "Petermann" "Hulunbeier Shi"
 $ administrative_area_level_2: chr  "Agra" "Greater London" "Macdonnell Shire" NA
 $ administrative_area_level_1: chr  "Uttar Pradesh" "England" "Northern Territory" "Neimenggu Zizhiqu"
 $ country                    : chr  "India" "United Kingdom" "Australia" "China"
 $ postal_code                : chr  "282006" "SW1A 2PW" "0872" NA
 $ establishment              : Factor w/ 1 level "Palace of Westminster": NA 1 NA NA
 $ postal_town                : Factor w/ 1 level "London": NA 1 NA NA