0

I'm trying to run a loop in R script on a dataframe called input. and update the column name Valid.

function myFunction(x){
    Result <- x * 2 
return(Result)
}

for (row in nrow(input)){
input$Valid[row] == myFunction(2)
}



output <- input 

But not getting the dataframe updated. And Yes I want to do this in a loop.

2 Answers 2

2

There are several issues in your for loop:

  1. nrow(input) is just a number, so you need to create a range from 1 to nrow(input), i.e., seq_len(nrow(input))
  2. == is to judge the equality, so, use <- or = for value assignment

Example

for (row in seq_len(nrow(input))){
  input$Valid[row] <- myFunction(2)
}
Sign up to request clarification or add additional context in comments.

2 Comments

1:nrow(...) is fragile, what if this is done with a 0-row frame (it breaks, try 1:0). Instead, I suggest seq_len(NROW(input)), as it deals correctly with the 0-row frame.
Yes, except ... seq(0) still returns [1] 1 0, which means the loop will still try to do something to two non-existent rows. Contrast with seq_len(0), which returns integer(0). (And seq_along(nrow(input)) is too similar to the OP, though here it will always operate on the first row, c.f., seq_along(100).)
0

I was able to get it working with your suggestions:

Here is the full script I'm using : Which is using a SOAP request to validate EU vat numbers with 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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.