本文介绍了如何将函数调用的结果作为dplyr :: mutate的一部分展平?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有以下简单用例,其中定义了两个包含名称和位置的示例: if(!require( tidyverse))install.packages( tidyverse,repos = http://cran.us.r-project.org) if(!require(ggmap))devtools :: install_github( dkahle / ggmap ) #,您可能还需要 #register_google(key =<注册您自己的密钥>)) x<-tibble(name = c('a','b'),loc = c('china','switzerland'))x #小标题:2 x 2 name loc < ; chr> < chr> 1 a中国 2 b瑞士 现在我想丰富我的经纬度信息。我是这样运行的: x%>% mutate(lon = geocode(loc)$ lon, lat = geocode(loc)$ lat) 但这很昂贵,因为我需要致电地理编码函数每个样本两次,并且这不是免费的。有没有办法使 geocode 函数的返回变为平缓?这是一次失败的尝试,并且是我正在尝试实现的目标的证明: x%&>;% mutate(xx = geocode(loc),lon = xx $ lon,lat = xx $ lat)>错误:列xx的类数据不受支持。frame 解决方案对于添加地理编码坐标的特定情况, ggmap 实际上有一个函数 mutate_geocode 确实可以做到这一点: 库(dplyr)库(ggmap) mutate_geocode(x,location = loc)#> #小动作:2 x 4 #>名称loc lon lat #> < chr> < chr> < dbl> < dbl> #> 1 a china 104. 35.9 #> 2 b瑞士8.23 46.8 对于更一般的用途, purrr :: map_ * 功能运行良好。您可以映射位置名称,应用地理编码,然后取消该列表的嵌套: mutate(x,coords = purrr :: map(loc,geocode))%>% tidyr :: unnest(coords)#与上述$相同的输出b $ b 您还可以使用 purrr :: map_dbl 。如果返回的数据框不仅包含lon和lat列,那么这可能会有所帮助,例如,如果您在 output 值c>地理编码: mutate(x,coords = purrr: :map(loc,geocode), lon = purrr :: map_dbl(coords, lon), lat = purrr :: map_dbl(coords, lat))#相同输出以上 或按列位置: mutate(x,coords = purrr :: map(loc,geocode), lon = purrr :: map_dbl(coords,1), lat = purrr :: map_dbl(coords,2))#相同的输出 I have the following simple use-case where I define two samples containing name and location:if(!require(tidyverse)) install.packages("tidyverse", repos = "http://cran.us.r-project.org")if(!require(ggmap)) devtools::install_github("dkahle/ggmap")# you may also need to #register_google(key="<register your own key>")x <- tibble(name=c('a', 'b'), loc=c('china', 'switzerland'))x# A tibble: 2 x 2 name loc <chr> <chr> 1 a china 2 b switzerlandNow I'd like to enrich my tibble with longitude and latitude information. I do that by running:x %>% mutate(lon=geocode(loc)$lon, lat=geocode(loc)$lat)but this is expensive because I'd need to call the geocode function two times per sample and that [s] ain't free. Is there a way to flatten the return of the geocode function into the tibble? This is a failed attempt and a demonstration of what I'm trying to achieve:x %>% mutate(xx=geocode(loc), lon=xx$lon, lat=xx$lat)>Error: Column `xx` is of unsupported class data.frame 解决方案 For the specific case of adding geocoded coordinates, ggmap actually has a function mutate_geocode that does exactly this:library(dplyr)library(ggmap)mutate_geocode(x, location = loc)#> # A tibble: 2 x 4#> name loc lon lat#> <chr> <chr> <dbl> <dbl>#> 1 a china 104. 35.9#> 2 b switzerland 8.23 46.8For more general uses, purrr::map_* functions work well. You can map over location names, apply geocode, and unnest that list:mutate(x, coords = purrr::map(loc, geocode)) %>% tidyr::unnest(coords)# same output as aboveYou could also extract each column you need with purrr::map_dbl. This might be helpful if you got a data frame back with more than just lon and lat columns, such as if you'd set a different value of output in geocode:mutate(x, coords = purrr::map(loc, geocode), lon = purrr::map_dbl(coords, "lon"), lat = purrr::map_dbl(coords, "lat"))# same output as aboveOr by column position:mutate(x, coords = purrr::map(loc, geocode), lon = purrr::map_dbl(coords, 1), lat = purrr::map_dbl(coords, 2))# same output 这篇关于如何将函数调用的结果作为dplyr :: mutate的一部分展平?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-21 07:24