0
我一直在使用啓發式安德魯博客和這StackOverflow Question這真棒代碼。修改R中的函數以添加ID信息?
我想做一個簡單的修改,以便我的最終輸出也有來自原始數據框的ID信息。這是我現在所擁有的
test1 <- structure(list(ID = c("A123", "A123", "A125", "A126"),
IP = c("173.74.6.149", "109.189.227.94",
"50.27.115.146", "1.64.170.178")),
.Names = c("ID", "IP"), class = "data.frame",
row.names = c(NA, 4L))
freegeoip <- function(ip, format = ifelse(length(ip)==1,'list','dataframe'))
{
if (1 == length(ip))
{
# a single IP address
require(rjson)
url <- paste(c("http://freegeoip.net/json/", ip), collapse='')
ret <- fromJSON(readLines(url, warn=FALSE))
if (format == 'dataframe')
ret <- data.frame(t(unlist(ret)))
return(ret)
} else {
ret <- data.frame()
for (i in 1:length(ip))
{
r <- freegeoip(ip[i], format="dataframe")
ret <- rbind(ret, r)
}
return(ret)
}
}
try.ip <- function(ip) suppressWarnings(try(freegeoip(ip), silent = TRUE))
outcomes <- lapply(test1$IP, try.ip)
is.ok <- function(x) !inherits(x, "try-error")
outcomes <- outcomes[sapply(outcomes, is.ok)]
outcomes <- do.call("rbind", outcomes)
,我認爲這樣做會修改freegeoip
功能是正確的代碼,但我不知道怎麼樣。
有人可以幫我嗎?
感謝@Troy。但是,如果我有多個具有相同IP的用戶,會發生什麼? – Ignacio
嗨@Ignacio,剛剛更新了答案,以便它可以重複使用... – Troy