mercredi 28 novembre 2018

When Ifelse statement is true, ifelse only returns one value instead of all

I have this data frame:

> new


     median
1    3.957587 
2    3.957587 
3    3.957587 
4    3.957587 
5    3.957587 
6    3.957587 
7    3.249960 
8    3.249960 
9    3.249960 
10   3.249960 
11   3.249960 
12   3.249960 
13   2.962515 
14   2.962515
15   2.962515 
16   2.962515 
17   2.962515 
18   2.962515

Now I compare these median values with this command:

# if difference of median values is bigger than 50%, get index of where this is happens
# returns numeric(0) when condition not met

results <- which(c(0, abs(diff(new$median))) > 0.5 * new$median) - 1

This works fine.

What I want to do now, is when the difference of median values is less than 50% (condition not met), just get the index of where the median values change. I could do it this way:

> testing <-  which(new$median[-1] != new$median[-length(new$median)])
> testing
[1]  6 12

I put these two ideas in an ifelse statement:

> results <- ifelse(length(results) == 0, testing, results)
> results
[1] 6

But this just gives me the first number, not also the second. Why?

php If statement inside another php if statement [duplicate]

This question already has an answer here:

I want to display whole code if $ws_user_status is 1, but inside this i want to separate the code on 2 another if statements... if $ws_total_sales == 0 to display info message, else ($ws_total_sales > 0) to display the table with all content items

But i have something made wrong... the table is visible all the time...

<?php
$ws_total_sales == 3;
$ws_user_status == 1;
?>

<?php if ($ws_user_status == 1) : ?>
<?php if ($ws_total_sales == 0 ) : ?>
<div class="woocommerce-info">
    <?php _e( 'You do not have any items to sell.', 'woocommerce' ); ?>
</div>
<?php else : ?>
<div>
<div class="alert alert-info">
    <?php _e( 'Your sales.', 'woocommerce' ); ?>
</div>      
      <table class="table table-hover">
        <thead>
          <tr>
            <th>Item</th>
            <th>Total sale</th>
            <th>Your profit</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Item 1 name</td>
            <td>100</td>
            <td>75000</td>
          </tr>
          <tr>
            <td>Item 2 name</td>
            <td>100</td>
            <td>60000</td>
          </tr>
          <tr>
        </tbody>
      </table>
</div>  
<?php endif; ?>
<?php endif; ?>

Using a return statement inside of an if statement [duplicate]

I am writing some JavaScript but I can't seem to make it work. I have the following code:

function() {

  var number = ;
  var level = " ";

  if(number = 198115) {
  var level = "HBO Bachelor";
}
  else if(number = 198118) {
  var level = "HBO Master";
}
  else if(number = 198121) {
  var level = "WO Bachelor";
}
  else if(number = 198124) {
  var level = "WO Master";
}
  else {
  var level = "Undefined";
}
  return level;
}

The variable that 'number' will be is one of the options stated in the if statement. What happens is that I only get 'undefined' and I don't get why.

Do you guys have any tips?

Trying to get property' of non-object, update existing users with new forms

I have extended an existing form, with another form with address information. New clients i can edit, since they got the values which are given with them.

Existing users, who dont have address information yet, i can't edit.

Trying to get property 'region' of non-object

I think this is because, the edit page returns a Null value.

    $regions = DB::table("regions")->pluck("name","id");
    $address = Address::where('id', $request->address_id)->with('region')->first();
    $data = $this->data->getEditClient($id);
    $admins = $this->data->getAdmin();

 if (is_null($address) && is_null($regions))
    {
        return view('client.edit', [

    //code for succesfully go to the edit page without address information.

        ]);
    } 


    return view('client.edit', [
        'client'        => $data, 
        'admins'        => $admins,
        'regions'       => $regions,
        'address'       => $address
        ]);
    }
}

Anyone knows the right solution for this error?

Greetings,

How to check if the variable contains the same string as a file?

My variables name is A and my files name is dogs.txt . I need to check if the variables text is the same as dogs.txt (contains the same string). What is the best way to do it?

I tried this

if [ cat dogs.txt == "$A" ] ; then  echo "equal" ; fi

How can i apply a function with an ifelse stricture

I make a new function:

  newFunction <- function (varX) {

  lst_a <- c("aa", "bb", "cc")
  lst_b <- c("dd", "ee", "ff")
  lst_c <- c("gg", "hh", "ii")

  ifelse(varX %in% lst_a , x.out <- "List A" , x.out <- "Other List" )
  ifelse(varX %in% lst_b , x.out <- "List B" , x.out )
  ifelse(varX %in%  lst_c , x.out <- "List C" , x.out)

  return(x.out)

 }

When I test the function it works well. But when I apply it to a dataframe it does not work:

     df <- as.data.frame(c("aa","cc","kk", "nn"))
    df$new <- newFunction((df[,1]))

I expected to get "List A", "List A", "Other list", "Other List" - but no such luck have I, and left with me is "Other list", "Other List", "Other list", "Other List"

r - Ordering problems when using mutate with ifelse conditional to date

I'm trying to use mutate to create a column that takes the value of one column up to a point and then uses cumprod to fill the rest of the observations based on the values of another column.

I tried combining mutate with ifelse but the order of the statements is not correct and I can't figure out why

Below I reproduce a more basic example that replicates my problem:

foo1 <- data.frame(date=seq(2005,2018,1))
foo1 %>% mutate(h=ifelse(date>2008, seq(1,11,1), 99))

The output is:

   date  h
1  2005 99
2  2006 99
3  2007 99
4  2008 99
5  2009  5
6  2010  6
7  2011  7
8  2012  8
9  2013  9
10 2014 10
11 2015  1
12 2016  2
13 2017  3
14 2018  4

And I'd like it to be:

   date  h
1  2005 99
2  2006 99
3  2007 99
4  2008 99
5  2009  1
6  2010  2
7  2011  3
8  2012  4
9  2013  5
10 2014  6
11 2015  7
12 2016  8
13 2017  9
14 2018 10

Thanks for your help!