编写一个函数来分离R中的工作与误差值 [英] Writing a function to separate working vs. error values in R

查看:178
本文介绍了编写一个函数来分离R中的工作与误差值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用启发式安德鲁博客



您可以在其中定位IP地址。我希望能够通过IP地址的大型(24867 IP)向量来运行该功能。问题是大多数IP地址会返回以下错误:

 文件错误(con,r):不能打开连接
此外:警告消息:
在文件(con,r)中:无法打开:HTTP状态为'404未找到'

我假设发生这种情况是因为无法追踪IP地址。



我想创建一个IP地址向量的子集,它将给我的位置。我想我需要编写另一个函数将IP地址放入一个新的工作IP地址向量中,或放入不同的错误IP地址向量中。



我想我还需要使用 try tryCatch 函数?

我是编程新手,所以我不知道如何编写函数。我在网上查看了一些例子,但无法弄清楚。任何意见将不胜感激。



编辑:
感谢@Thomas指责我正确的道路......

我使用函数is.error来确定 try()是否会给我一个错误与否。返回一个逻辑向量:

  is.error<  -  function(x)inherits(x,try-error)

使用 lapply()

 > is.error(
+ try(
+ lapply(X = ls_IPaddresses,FUN = freegeoip)
+)
+)
文件错误(con,r ):无法打开连接
[1] TRUE

ls_IPaddresses的长度为24867 ,所以我想要一个具有相同长度的逻辑向量,这样我就可以将逻辑向量与子集ls_IPaddresses进行子集化。

解决方案



  ips<  -  c('184.26.100.110','555.22.333.111')

try.ip< - function(ip)suppressWarnings(try(freegeoip(ip),silent = TRUE))
results < - lapply(ips,try.ip)

是.ok < - function(x)!inherits(x,try-error)
sapply(results,is.ok)
#[1] TRUE FALSE


I've been using this awesome code from the Heuristic Andrew blog

where you can geolocate IP addresses. I want to be able to run the function over a large (24867 IPs) vector of IP addresses. The problem is most of the IP addresses will return the following error:

Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") : cannot open: HTTP status was '404 Not Found'

I'm assuming this occurs because the IP address can not be traced.

I want to create a subset of the vector of IP addresses that will give me the location. I think I would need to write another function puts the IP address into a new vector of working IP addresses, or into a different vector of error IP addresses.

I think I would also need to use the try or tryCatch function?

I'm new to programming so I don't know how to write a function. I've looked at examples online but can't quite figure it out. Any advice would be appreciated.

EDIT: Thanks to @Thomas for pointing me down the right road...

I used the function is.error to determine if try() will give me an error or not. Returns a logical vector:

is.error <- function(x) inherits(x, "try-error")

Tried using lapply()

> is.error(
+   try(
+     lapply(X  = ls_IPaddresses, FUN = freegeoip)
+     )
+   )
Error in file(con, "r") : cannot open the connection
[1] TRUE

The length of ls_IPaddresses is 24867, so I would want a logical vector with the same length so I can subset the logical vector against ls_IPaddresses.

解决方案

This should work:

ips <- c('184.26.100.110', '555.22.333.111')

try.ip   <- function(ip) suppressWarnings(try(freegeoip(ip), silent = TRUE))
outcomes <- lapply(ips, try.ip)

is.ok    <- function(x) !inherits(x, "try-error")
sapply(outcomes, is.ok)
# [1] TRUE  FALSE

这篇关于编写一个函数来分离R中的工作与误差值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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