使用dplyr / mutate修复R中的不兼容类型错误 [英] fixing incompatible types error in R using dplyr/mutate

查看:254
本文介绍了使用dplyr / mutate修复R中的不兼容类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R中的tidyverse / dplyr包来处理数据,包括向在线API(从Altmetric)的向量化调用,使用mutate添加行。



我可以创建的最小的代码重现错误是在下面。我收到错误错误:不兼容的类型,期望一个数字向量

 库(tidyverse)
库(jsonlite )

fromJSON_wrapper< - function(x,y){
fromJSON(x)[[c(y)]]
}

玩具< - tibble(
doi = c(10.1002 / anie.201500251,10.1080 / 19443994.2015.1005695,10.1007 / s13721-015-0095-0),
url = c https://api.altmetric.com/v1/doi/10.1002/anie.201500251,https://api.altmetric.com/v1/doi/10.1080/19443994.2015.1005695,https:// api $ alt $($)

提取< - 玩具%>%rowwise()%>%mutate(score = fromJSON_wrapper (url,score))

以下提取单个分数的代码可以工作,无论是使用包装器或一行琐事,我不知道为什么我的代码不工作。

  fromJSON_wrapper(https ://api.altmetric.com/v1/doi/10.1007/s13721-015-0095-0)
extract< - 玩具[1,]%>%rowwise()%>%mutate(score = fromJSON_wrapper(url,score))

任何建议将不胜感激。

解决方案

简单的迭代URL的向量并提取你需要的东西。 purrr :: map_dbl 使这个简单,虽然 sapply 也可以正常工作。

  library(tidyverse)

玩具< - tibble b $ b doi = c(10.1002 / anie.201500251,10.1080 / 19443994.2015.1005695,10.1007 / s13721-015-0095-0),
url = c(https:// api .altmetric.com / v1 / doi / 10.1002 / anie.201500251,https://api.altmetric.com/v1/doi/10.1080/19443994.2015.1005695,https://api.altmetric.com/v1 /doi/10.1080/19443994.2015.1005695)


提取< - 玩具%>%mutate(score = map_dbl(url,〜jsonlite :: fromJSON(.x)$分数))

提取%>%select(doi,score)
#> #A tibble:3×2
#> doi得分
#> < CHR> < DBL>
#> 1 10.1002 / anie.201500251 0.25
#> 2 10.1080 / 19443994.2015.1005695 1.00
#> 3 10.1007 / s13721-015-0095-0 1.00


I'm trying to use the tidyverse/dplyr package in R to work with data including vectorized calls to an online API (from Altmetric) to add rows using mutate.

The smallest code I can create that reproduces the error is that below. I get the error "Error: incompatible types, expecting a numeric vector"

library(tidyverse)
library(jsonlite)

fromJSON_wrapper <- function(x,y) {
  fromJSON(x)[[c(y)]]
}

toy <- tibble(
      doi = c("10.1002/anie.201500251", "10.1080/19443994.2015.1005695", "10.1007/s13721-015-0095-0"), 
      url = c("https://api.altmetric.com/v1/doi/10.1002/anie.201500251", "https://api.altmetric.com/v1/doi/10.1080/19443994.2015.1005695", "https://api.altmetric.com/v1/doi/10.1080/19443994.2015.1005695")
      )

extracted <- toy %>% rowwise() %>% mutate(score = fromJSON_wrapper(url,"score"))

The code for extracting a single score below works, whether just using the wrapper or on a one row tibble and I'm not sure why my code isn't working.

fromJSON_wrapper("https://api.altmetric.com/v1/doi/10.1007/s13721-015-0095-0")
extracted <- toy[1,] %>% rowwise() %>% mutate(score = fromJSON_wrapper(url, "score"))

Any suggestions would be appreciated.

解决方案

It's simpler to just iterate over the vector of URLs and extract what you need. purrr::map_dbl makes this simple, though sapply would work fine, too.

library(tidyverse)

toy <- tibble(
    doi = c("10.1002/anie.201500251", "10.1080/19443994.2015.1005695", "10.1007/s13721-015-0095-0"), 
    url = c("https://api.altmetric.com/v1/doi/10.1002/anie.201500251", "https://api.altmetric.com/v1/doi/10.1080/19443994.2015.1005695", "https://api.altmetric.com/v1/doi/10.1080/19443994.2015.1005695")
)

extracted <- toy %>% mutate(score = map_dbl(url, ~jsonlite::fromJSON(.x)$score))

extracted %>% select(doi, score)
#> # A tibble: 3 × 2
#>                             doi score
#>                           <chr> <dbl>
#> 1        10.1002/anie.201500251  0.25
#> 2 10.1080/19443994.2015.1005695  1.00
#> 3     10.1007/s13721-015-0095-0  1.00

这篇关于使用dplyr / mutate修复R中的不兼容类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆