jeudi 1 octobre 2020

C# comma usage in a ternary operator

Is there any analogy like in C that the comma operator can be used in C# as well as following.

 templateCode = myBoolFlag ? ((smsNotification.NewNumber = request.NewNumber), "NEWCARDSTR")
                                       : request.NewCardStatus == "L" || request.NewCardStatus == "S" 
                                           ? "STRING1" 
                                           : "STRING2";

Group by + New Column + Grab value former row based on conditionals

I have this set

df=pd.DataFrame({'user':[1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,4],
                  'date':['1995-09-01','1995-09-02','1995-10-03','1995-10-04','1995-10-05','1995-11-07','1995-11-08','1995-11-09','1995-11-10','1995-11-15','1995-12-18','1995-12-19','1995-12-20','1995-12-23','1995-12-26','1995-12-27'],
                  'dc':['1995-09-02','1995-09-02','1995-10-02','1995-10-05','1995-10-05','1995-11-05','1995-11-05','1995-11-10','1995-11-10','1995-11-10','1995-12-10','1995-12-23','1995-12-23','1995-12-23','1995-12-23','1995-12-23'],
                  'tp':['s','c','f','s','c','c','f','s','c','s','f','s','s','c','s','f'],
                  'vt':['0','1','0','0','1','0','0','0','1','0','0','0','0','1','0','0'],
                  'c1':['1','5','0','2','3','9','3','2','0','5','5','6','4','0','6','0'],
                  'c2':['3','4','0','2','5','3','8','4','0','6','2','7','0','0','8','0'],
                  'c3':['5','5','2','5','6','4','2','4','4','6','3','4','3','8','2','7']})
df2

which gives:

user    date        dc     tp   vt  c1   c2  c3
 1  1995-09-01  1995-09-02  s   0    1   3   5
 1  1995-09-02  1995-09-02  c   1    5   4   5
 1  1995-10-03  1995-10-02  f   0    0   0   2
 2  1995-10-04  1995-10-05  s   0    2   2   5
 2  1995-10-05  1995-10-05  c   1    3   5   6
 2  1995-11-07  1995-11-05  c   0    9   3   4
 2  1995-11-08  1995-11-05  f   0    3   8   2
 3  1995-11-09  1995-11-10  s   0    2   4   4
 3  1995-11-10  1995-11-10  c   1    0   0   4
 3  1995-11-15  1995-11-10  s   0    5   6   6
 3  1995-12-18  1995-12-10  f   0    5   2   3
 4  1995-12-19  1995-12-23  s   0    6   7   4
 4  1995-12-20  1995-12-23  s   0    4   0   3
 4  1995-12-23  1995-12-23  c   1    0   0   8
 4  1995-12-26  1995-12-23  s   0    6   8   2
 4  1995-12-27  1995-12-23  f   0    0   0   7

I want to create new column create new column df['dc2'], where groupby user, column df['dc2']= df['dc']. However if df['dc'] meets the conditions 'tp'='c' & 'vt'=1 & 'c1'=0 & 'c2'=0, then grab the date of the former entry (raw of the user)

#ie. for user 3, on column df['dc'] if we look at entry 'tp'='c' & 'vtb'=1, we can see it has 'c1'=0 and 'c2'=0, #thus the value of df['dc2'] will be (for user 3) '1995-11-09' instead of '1995-11-10'

#ie. for user 4, on column df['dc'] if we look at entry 'tp'= 'c' & 'vtb'=1, we can see it has 'c1'=0 and 'c2'=0, in this case df['dc2'] should be (for user 4) '1995-12-20' instead of '1995-12-23'

Here is the desired result:

user    date       dc           dc2     tp   vt c1  c2  c3
1   1995-09-01  1995-09-02  1995-09-02   s   0   1   3   5
1   1995-09-02  1995-09-02  1995-09-02   c   1   5   4   5
1   1995-10-03  1995-10-02  1995-10-02   f   0   0   0   2
2   1995-10-04  1995-10-05  1995-10-05   s   0   2   2   5
2   1995-10-05  1995-10-05  1995-10-05   c   1   3   5   6
2   1995-11-07  1995-11-05  1995-11-05   c   0   9   3   4
2   1995-11-08  1995-11-05  1995-11-05   f   0   3   8   2
3   1995-11-09  1995-11-10  1995-11-09   s   0   2   4   4
3   1995-11-10  1995-11-10  1995-11-09   c   1   0   0   4
3   1995-11-15  1995-11-10  1995-11-09   s   0   5   6   6
3   1995-12-18  1995-12-10  1995-12-09   f   0   5   2   3
4   1995-12-19  1995-12-23  1995-12-20   s   0   6   7   4
4   1995-12-20  1995-12-23  1995-12-20   s   0   4   0   3
4   1995-12-23  1995-12-23  1995-12-20   c   1   0   0   8
4   1995-12-26  1995-12-23  1995-12-20   s   0   6   8   2
4   1995-12-27  1995-12-23  1995-12-20   f   0   0   0   7

Creating Calculated column using IF statement needs to use same Source column value for multiple list options in the Calculated column

I have a client requirement where i need to create filter bins for existing column filter.

Since i cannot put the actual file here i am recreating the scenario in a dummy dxp file.

I used Tableau version 10.4

Existing column contains values A,B,C,D,E

In the calculated column i create i need to display two list items AB & AX

Basically AB - A,B and AX - A,C,D,E

I tried using if Statement and in the result, A is not displayed in the second list item of the Calculated column .

Below is the code i used:

If [Col 1]='A' OR [Col 1]='B'

THEN 'AB'

ELSEIF [Col 1]='A' OR [Col 1]='C' OR [Col 1]='D'OR [Col 1]='E'

THEN 'AC'

ELSE 'NA'

END

Expected Solution : Basically the solution i want is when i select Col 1 value 'A' both values of Calculated Column 'AB' & 'AX' should be shown and vice versa when any of the calculated column values are selected 'A' of Col 1 should be displayed as its common for both

Link with Question and Tableau File in Tableau Community : https://community.tableau.com/s/question/0D54T00000KfccNSAR/creating-calculated-column-using-if-statement-needs-to-use-same-source-column-value-for-multiple-list-options-in-the-calculated-column

if statements with several conditions and several variations

I have the following data table in R:

       industry fyear change.in.employeegrowth change.in.netincome.to.sales change.in.ROA change.in.Assetturnover change.in.RandD change.in.CAPEX.byassets
1     Agriculture  1999               0.08766928                  0.036667693   0.021561014              0.08213972    1.893469e-03              0.014274404
2     Agriculture  2000               0.13963964                  0.066484354   0.027813095              0.15047066    4.116929e-03              0.028307019
3     Agriculture  2001               0.13636364                  0.041775993   0.030575742              0.05965700    8.678983e-03              0.014702146
4     Agriculture  2002               0.05484111                  0.092764170   0.058518761              0.04699455    2.078513e-03              0.010364144
5     Agriculture  2003               0.08757912                  0.084572235   0.041094305              0.10765641    4.061465e-03              0.008522656
6     Agriculture  2004               0.04970685                  0.058833426   0.028568214              0.02540688    1.275619e-02              0.005628402
7     Agriculture  2005               0.17954545                  0.040047709   0.041380006              0.08353320    3.917954e-02              0.009383056
8     Agriculture  2006               0.19047619                  0.063405763   0.047080200              0.07955826    2.465875e-02              0.005213245
9     Agriculture  2007               0.09165972                  0.098566476   0.064550850              0.09336734    9.296165e-03              0.008958315
10    Agriculture  2008               0.04227658                  0.076702017   0.056679238              0.12820537    2.575690e-03              0.010149566

and 47 more industries.

Now I want to create a dummy variable for certain industries and years, but I am struggling with the formulation of my if statement. The dummy should print 1 if true and nothing if not(alternatively 0). However, I do not want to override previous statements by repeated code.

compustat.medians$industry is of class factor compustat.medians$fyear is of class integer

I have tried the following syntax:

compustat.medians$pre.wave.year <-if( (compustat.medians$industry == "Food Products") & (compustat.medians$fyear == 2012) )
                                 or ( (compustat.medians$industry == "Candy and Soda") & (compustat.medians$fyear == 2012) )
                                 or ( (compustat.medians$industry == "Recreation") & (compustat.medians$fyear == 2005) )
{
  print(1)
}

I get the error: the condition has length > 1 and only the first element will be used for all variations, I try.

Is there a way to use the if statement for several conditions, and with several possible combinations at once?

thanks,

how to capture only digits in array using if statement in bash

    if [[ "${newarray[$i]}" =~ ^[[:digit:]]+$ ]]; then 

        echo "${newarray[$i]}"     
    fi


 

this is my code I need to get only digits from the array list and their count as well. but there is an issue that shows alphabetical values only.

given getdata.txtcontains -

   cat  
   dog
   1234
   pl345
   567ab
   12234
     

mercredi 30 septembre 2020

How to take values initiated in an if else statement to be used outside the statement?

hello I'm a newbie and I am having a hard time doing this program. I was wondering if there is any work around to get the value in the else statement be used outside the statement itself or any other way for this to work. The program is suppose to stop when there are 10 odd numbers in the array which i tried to do by putting it in a do while loop and assigning variable k to add 1 value if the the else statement detects an odd number but the value of k is only usable in the else statement.

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num[] = new int[100];

        System.out.println("This program stops when 10 odd numbers are entered.");
        do {
            int j = 0;
            int k = 0;
            for (int i = 0; i <= num.length; i++) {
                System.out.println("Please enter element " + i);
                num[j] = sc.nextInt();

                if (j % 2 == 0)
                    System.out.println("Entered number is even");
                else
                    k = k + 1;
            }
        } while (k != 11);
    }
    
}

How to use switch statement to assign a string value to a variable

I hope that I put a title that properly relates to my problem.

The purpose of my program is to calculate the expenses for a trip between various cities. The program will give a list of source cities, followed by a list of destination cities. The user will be allowed to choose their cities by inputting the first letter of each city.

I want the program to be able to take the input from the user and assign it to one of the given cities.

//Declarations
    char src_city[15];
    char dest_city[15];
system("cls");
    puts("\n");
    printf("ENTER THE SOURCE CITY: \n\n");
    printf("B for Baltimore\n\n");
    printf("C for Chattanooga\n\n");
    printf("N for Nashville\n\n");
    printf("P for Pasadena\n\n");
    puts("");
    scanf("%c", &src_city);
    switch(src_city)
    {
    case 'B': case 'b': ("Baltimore");
    break;
    case 'C': case 'c': ("Chattanooga");
    break;
    case 'N': case 'n': ("Nashville");
    break;
    case 'P': case 'p': ("Pasadena");
    break;
    }
    getchar();

The 'B', 'C', 'N', and 'P' are the letters I want the user to input, and when they do to assign it to the corresponding city.

However, when I use this method I get the error: "assignment to expression with array type". I looked up this error and I was then told to use if/else if statements which in turn failed similarly. The switch statement method makes the most sense to me.

I need to assign the string value to the corresponding variable (src_city/dest_city) because I will need to call those variables later in a printf(); statement.

Any help would be very much appreciated!