vendredi 26 avril 2019

if-statement in R: "missing value" error despite existing value

An if-statement returns an "missing Value"-error when there is a perfectly healthy value.

I wanted to write a simple script to delete rows in a dataset if one of their entries contains a certain tag. I assign an indicator variable in a new column (containsMR) and then iterate over the rows using a for-loop. If the indicator is TRUE, the row should be removed.

The indicators get assigned correctly, so far, so good. The interesting part: In the loop's if-statement seems to have trouble reading the values, because it returns "Error in if (data$containsMR[i]) { : missing value where TRUE/FALSE needed".

Given the correct (and complete) assignment of indicator variables, this surprises me. What is even more weird: Some, but not all the rows with a positive indicator are removed (checked with printouts and table(data$containsMR) ).

And now the really weird stuff: if I run the same loop one more time, it removes the rest of the columns (as it should), but returns the same error. So, theoretically, I could just run the loop twice, ignore the errors and walk away with the result I wanted. That's just really not the point of what I'm doing.

Bugfixes tried: - changed for- to while-loop - changed indicators (and if-statement) to integer (0,1) - ran the script in RStudio and R console - changed variable names, included/excluded definitions (e.g. adding the proxy variable row_number instead of calling it in one line.

# Script to delete all rows containing "MR" in column "EXAM_CODE"

# import file
data <- read.csv("C:\\ScriptingTest\\ablations 0114.csv")

# add indicator column
for (i in 1:nrow(data)){
    data$containsMR[i] <- ifelse(grepl("MR", toString(data$EXAM_CODE[i])), TRUE, FALSE)
}

# remove rows with positive indicator
row_number <- nrow(data)
for (i in 1:row_number){
    if (data$containsMR[i]){
        data <- data[-c(i),]
    }
}

# export csv
write.csv(data, "C:\\ScriptingTest\\export.csv")

A simple counter gives a different outcome in javascript compared to python

I'm trying to learn javascript. I'm having problems with a simple counter that gives a wrong outcome.

var x = 0

while (x < 10) {
  x++
  console.log("x:", x)

  if (5 < x < 10) {
    console.log("x is between 5 and 10")
  } else {
    console.log("getting out of the loop")
    break
  }
}

outcome

'x:'1
'x is between 5 and 10'
'x:'2
'x is between 5 and 10'
'x:'3
'x is between 5 and 10'
'x:'4
'x is between 5 and 10'
'x:'5

etc.

Whereas if I do the same with python, the result is correct

x = 0

while (x < 18):
    x+=1
    print("x:", x)

    if 5 < x < 10:
        print("x is between 5 and 10")

    else:
        print("getting out of the loop")
        break

outcome

x: 1
getting out of the loop

Process finished with exit code 0

How to make function for tag with if statements work? (function name is never read)

I'm working on a navigation through i tags acting as buttons, sliding content around.

My function to perform the slide, through an onclick, does not work due to the function name not responding/being found.

HTML

<div onclick="prevPage()">
     <i class="fas fa-step-backward"></i>
</div>

JavaScript

function prevPage(){
     if (document.getElementsById("pageAbout").classList.contains('centered'))
}

Condition to check if if statement fails just once?

How can I check that my if statement as below fails just once? Is there a way to check how many times it fails?

Right now I am using any but that means it could be failing more than once?

for j in range (0,n):
     for k in range (0,n):
            while True:
                 if any(X[i][j]!=X[k])
....


How can I make my confirm statement post a status if my function evaluates as true, or cancel if false?

I want a confirm box to pop-up when the user clicks submit, but only if their post contains a string such as 'sale' and '£'. Unfortunately the code forwards to the action page regardless of whether OK or Cancel was clicked.

I also tried creating another 'if else' containing the confirm statement, to return true for Ok or False for Cancel, but to no avail.

Sorry if some of that is hard to understand, I'm a noob and trying to wrap my head around javascript.

function check() {

 var post = document.forms["myForm"]["newPost"].value;
    if (post.indexOf('sale') > -1 || post.indexOf('£') > -1) {
     confirm("If this is a 'for sale' post, please post to the marketplace instead. Press OK to post as a general status."); 
 }
}
</script>
</head>
<body>

<form name="myForm" action="/post-page.php" onSubmit="return check()" method="post">
Post: <input name="newPost" id="newPost">
  <input type="submit" value="Post Now">
</form>

Expected: Pressing OK posts the status.

Results: Both options post the status.

Google sheet SUM based on criteria of criteria in multiple sheets

On the same Googlesheet document, meant to manage multiple License keys, I've got three different sheets.

First sheet is the list of the Keys. As soon as a key is used, I set its user.

Second sheet is the list of the Users. As soon as I have a new user, I set its name and its role.

Third sheet is the list of the Roles. This is a list of the different roles that needs a key.

Problem : On the third sheet, I'd like to count how many keys are used for each role.

Example : I'm Paul, I'm UX Designer, and I use one key.

On the first sheet, one key is set to my name. On the second sheet, my name is set to the role UX Designer.

I'm basically the only UX set with one key. So, on the third sheet, it should count 1 as the number of key used by the role "UX Designer".

Many thanks in advance, I hope I was clear !

if runs with 9 >= 10? [duplicate]

This question already has an answer here:

I am making a mistake or something strange is going on but I am not seeing it.

localStorage value = 9

if (localStorage.getItem('TotalProducts') >= '10') {
  alert('total products'+localStorage.getItem('TotalProducts'));
}

Why am I receiving the alert?

Alert content is total products9