'Column Values Depend on Non-NA entries in Other Columns

I am in need of a way to devise a column (data_sequence) that uses the following logic as based off values in other columns (col_1:col_5).

  1. The column elements in col_1:col_5 contain both 1) values and 2) NA entries.

  2. The flow of construction of the 'data_sequence' column moves from right to left within col_1:col_5 .

  3. Initially, 'data_sequence' assumes the values of the rightmost column (col_5) until the first instance of NA is hit in that column.

  4. col_4 then becomes the relevant column upon which to harvest data. col_4 values are then assumed for 'data_sequence' in corresponding rows until its first instance of NA appears.

The process continues through col_1, at which point 'data_sequence' is fully populated with the appropriate values.

The values present in col_1:col_5 stair-step downward from right to left as this sample data indicates. That is, values in adjacent columns may begin in the same row, but values never begin at a lower row value in the left column of any adjacent pair of columns.

Once 'data_sequence' is populated, I also need a column (column_offset) that provides the column offset relative to the first column (row).

Any solutions, elegant or otherwise, are greatly appreciated.

enter image description here

sixteen_tons <- tibble(row = 1:12,
               col_1 = c( rep(NA, 5),  1:7), 
               col_2 = c( rep(NA, 3) , 15:21, rep(NA, 2) ),
               col_3 = c( rep(NA, 2) , 33:39, rep(NA, 3) ),
               col_4 = c( rep(NA, 2) , 55:59, rep(NA, 5) ),
               col_5 = c( 91:93, rep(NA, 9) ),
               data_sequence = c(91:93, 56:59, 38:39, 21, 6:7),
               column_offset = c(rep(5,3), rep(4,4), rep(3,2), rep(2,1), rep(1,2)   )
)



Solution 1:[1]

<?php if(!empty($row["Favourite"])) { ?>
    <img alt="Favourite" title="Favourite" src="http://localhost/Test/Images/Icons/<?php echo $row["Favourite"]; ?>" width="12.5%" height="12.5%"/>
<?php } ?>

More on empty function: https://www.php.net/manual/en/function.empty.php

Solution 2:[2]

This code will work, if favorite will not empty

<?php
            include_once("conn.php");
            $sql = "SELECT * FROM table ORDER BY id ASC";
            $result = mysqli_query($conn, $sql);
            if(mysqli_num_rows($result) > 0)
            {
                while($row = mysqli_fetch_array($result))
                {
                  $favourite = $row["Favourite"];
                }
           }

 if(!empty($favourite)) { 
    echo '<img alt="Favourite" title="Favourite" src="http://localhost/Test/Images/Icons/"'.$favourite.'width="12.5%" height="12.5%"/>';
 } 
else{
//rest of code
}
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2