R 脚本 - 在循环期间更新 DataFrame 的值
R Scripting - Update Values of DataFrame during Loop
我正在尝试 运行 在名为输入的数据帧上的 R 脚本中循环。并更新列名称有效。
function myFunction(x){
Result <- x * 2
return(Result)
}
for (row in nrow(input)){
input$Valid[row] == myFunction(2)
}
output <- input
但没有更新数据框。是的,我想循环执行此操作。
您的 for
循环中存在几个问题:
nrow(input)
只是一个数字,因此您需要创建一个从 1
到 nrow(input)
的范围,即 seq_len(nrow(input))
==
是判断是否相等,所以,用<-
或=
赋值
例子
for (row in seq_len(nrow(input))){
input$Valid[row] <- myFunction(2)
}
我能够根据你的建议让它工作:
这是我正在使用的完整脚本:它使用 SOAP 请求通过 http://ec.europa.eu/taxation_customs/vies/checkVatService.wsd
验证欧盟增值税号
VatCheck <- function(x,z)
{
get_config_proxy <- function(url, user = NULL, pwd = NULL, verbose = FALSE, auth = "basic") {
# curl::ie_get_proxy_for_url wants a scheme - either way it don't work
if(is.null(httr::parse_url(url)$scheme)) {
if (verbose) message("No scheme provided. assume HTTP")
url <- modify_url(url, scheme = "http")
}
# get the proxy url if needed
proxy_url <- curl::ie_get_proxy_for_url(url)
# return blank config if no proxy required
if(is.null(proxy_url)) {
if(verbose) message("No proxy")
return(httr::config())
}
if(verbose) message("Proxy found")
# Otherwise configure the proxy / user and password are needed
# return a config object for use with httr
proxy_config <- httr::use_proxy(url = proxy_url, auth = auth)
}
body = sprintf("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='urn:ec.europa.eu:taxud:vies:services:checkVat:types'>
<soapenv:Body>
<urn:checkVat xmlns='urn:ec.europa.eu:taxud:vies:services:checkVat:types'>
<urn:countryCode>%s</urn:countryCode>
<urn:vatNumber>%s</urn:vatNumber>
</urn:checkVat>
</soapenv:Body>
</soapenv:Envelope>",x,z)
url <- "http://ec.europa.eu/taxation_customs/vies/services/checkVatService"
# No proxy configured so we get a timeout from curl fetch
# req <- GET(url)
# we configure the proxy giving interactively user and password
config_proxy <- get_config_proxy(url)
# we make a GET using the config
x <- with_config(config_proxy, POST(url, body=body))
req<-toString(content(x)) # we get 200. it works!
req
}
for(i in 1:nrow(input)) {
input[i, "Valid"] <- VatCheck(x = toString(input[i, "Column1"]) , z= toString(input[i, "Colum2"]))
}
output <- input
我正在尝试 运行 在名为输入的数据帧上的 R 脚本中循环。并更新列名称有效。
function myFunction(x){
Result <- x * 2
return(Result)
}
for (row in nrow(input)){
input$Valid[row] == myFunction(2)
}
output <- input
但没有更新数据框。是的,我想循环执行此操作。
您的 for
循环中存在几个问题:
nrow(input)
只是一个数字,因此您需要创建一个从1
到nrow(input)
的范围,即seq_len(nrow(input))
==
是判断是否相等,所以,用<-
或=
赋值
例子
for (row in seq_len(nrow(input))){
input$Valid[row] <- myFunction(2)
}
我能够根据你的建议让它工作:
这是我正在使用的完整脚本:它使用 SOAP 请求通过 http://ec.europa.eu/taxation_customs/vies/checkVatService.wsd
验证欧盟增值税号VatCheck <- function(x,z)
{
get_config_proxy <- function(url, user = NULL, pwd = NULL, verbose = FALSE, auth = "basic") {
# curl::ie_get_proxy_for_url wants a scheme - either way it don't work
if(is.null(httr::parse_url(url)$scheme)) {
if (verbose) message("No scheme provided. assume HTTP")
url <- modify_url(url, scheme = "http")
}
# get the proxy url if needed
proxy_url <- curl::ie_get_proxy_for_url(url)
# return blank config if no proxy required
if(is.null(proxy_url)) {
if(verbose) message("No proxy")
return(httr::config())
}
if(verbose) message("Proxy found")
# Otherwise configure the proxy / user and password are needed
# return a config object for use with httr
proxy_config <- httr::use_proxy(url = proxy_url, auth = auth)
}
body = sprintf("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='urn:ec.europa.eu:taxud:vies:services:checkVat:types'>
<soapenv:Body>
<urn:checkVat xmlns='urn:ec.europa.eu:taxud:vies:services:checkVat:types'>
<urn:countryCode>%s</urn:countryCode>
<urn:vatNumber>%s</urn:vatNumber>
</urn:checkVat>
</soapenv:Body>
</soapenv:Envelope>",x,z)
url <- "http://ec.europa.eu/taxation_customs/vies/services/checkVatService"
# No proxy configured so we get a timeout from curl fetch
# req <- GET(url)
# we configure the proxy giving interactively user and password
config_proxy <- get_config_proxy(url)
# we make a GET using the config
x <- with_config(config_proxy, POST(url, body=body))
req<-toString(content(x)) # we get 200. it works!
req
}
for(i in 1:nrow(input)) {
input[i, "Valid"] <- VatCheck(x = toString(input[i, "Column1"]) , z= toString(input[i, "Colum2"]))
}
output <- input