mardi 25 juin 2019

Is there a more compact way of writing these identical conditionals?

I want to check that the type of four variables is a particular type, and I have to do:

if (type(a) is not SomeType or
    type(b) is not SomeType or
    type(c) is not SomeType or
    type(d) is not SomeType):
    raise Exception("Not the correct types")

This feels repetitive. It seems there could be a better, less redundant way of doing this?

Ranking in Google Sheet

I have some data about people on my Google Sheet and they are given some points based on their activities. What I want in the next column is that their rank based on their points. I would change the points manually on a regular basis and accordingly, their rank should be changed automatically with a formula.

At present the data ranging from A1 to J50 where row 1 is the header. I need the rank in K.

I would like to know to rank in two ways. One in numerical. Like, Rank 1, 2, 3, etc. Other in text, 'Outstanding', 'Good', or any text.

How to use a for-loop to create a new variable based on differences between posixct for log files

I am trying to loop through a log file dataset I have, to add a variable in which a server session number is stored for every observation. For the first line, I want to create a new variable 'session number' with value 1. After that, I want a different session number for the following line if the 'ResearchNumber' differs from the line before. If it is the same 'ResearchNumber', I want to check whether the difference in the Posixct variable is larger than 18000 seconds (or 30 minutes). If that's the case, I want to create a different session number (by increasing this with 1). In all the other cases, I want the session number to be the same as the previous line. To summarize, I want to create session numbers based on inactivity for longer than 30 minutes per participant.

I have tried several things, but my code doesn't seem to loop over all the lines, and with other solutions the time difference doesn't calculate in the right way.

I hope someone can help me fix this problem. All help is appreciated!


# create example data

ResearchNumber <- c("AL001","AL002","AL003")

DateTimeTag <- c(
  as.POSIXct('2014-09-29 10:35:40', tz='GMT'),
  as.POSIXct('2014-09-29 10:35:42', tz='GMT'),
  as.POSIXct('2014-09-29 10:38:18', tz='GMT')
)

logdata <- data.frame(ResearchNumber, DateTimeTag)


# loop through logdata to add variable to every observation with a server session number

linecount <- 1
for (lines in logdata) {
  if (linecount == 1) {
    session_number <- 1
    logdata$session_number <- session_number
    datetime <- logdata$DateTimeTag
    participantbefore <- logdata$ResearchNumber
    linecount <- (linecount + 1)
  } 
  else if (linecount > 1) {
    difference <- (logdata$DateTimeTag - datetime)
    if (logdata$ResearchNumber != participantbefore) {
      logdata$session_number <- (session_number + 1)
      participantbefore <- logdata$ResearchNumber
      session_number <- (session_number + 1)
      datetime <- logdata$DateTimeTag
    }
    else if (difference > 18000) {
      logdata$session_number <- (session_number + 1)
      participantbefore <- logdata$ResearchNumber
      session_number <- (session_number + 1)
      datetime <- logdata$DateTimeTag
    }
    else {
      logdata$session_number <- (session_number)
      participantbefore <- logdata$ResearchNumber
      datetime <- logdata$DateTimeTag
    }
  }
}

Fn::If condition based on environment for Cloudformation Parameters

Based on the environment, I am trying to set the URL for a variable: It staging my URL should be https://staging.DNHostedZoneName , if prod - it should just be https://DNSHostedZoneName:

Here's my condition:

Conditions:
  IsEnvProd: Fn::Equals [ !Ref Env, 'prod']
  IsEnvStage: Fn::Equals [ !Ref Env, 'stage']

Here's where its been evaluated:

          Environment:
            - Name: NODE_ENV
              Value: !Ref NodeEnv
            - Fn::If:
              - IsEnvStage
              - Name: CORE_URL
                Value:
                  Fn::Join:
                    - ""
                    - - "https://"
                      - "staging"
                      - "."
                      - !Ref DnsHostedZoneName
              - Name: NCVCORE_URL
                Value:
                  Fn::Join:
                    - ""
                    - - "https://"

                      - !Ref DnsHostedZoneName

I am getting the following error:

 Template format error: Conditions can only be boolean operations on parameters and other conditions

how to paste together a vector element with the previuos according to a condition

I'm getting data from a web source with rvest and then I got a vector, say x, which has a length different from my other vectors (so I can't combine them into a table). Getting to the point: The reason is that everytime I see the element 'nuovo' (position x[11] ) in the vec, I know It should be pasted with exactly the previous, then i should also cancel out the element 'nuovo' because I need a 25 length vector.

x  = c("Vetrina" ,"Vetrina" ,"Vetrina",
"Vetrina" ,"Vetrina", "Vetrina",
"Vetrina" ,"Vetrina" ,"Vetrina",
"Vetrina" ,"nuovo" ,"Vetrina",
"Vetrina" ,"Vetrina" ,"Vetrina",
"Vetrina" ,"Vetrina" ,"Vetrina",
"Vetrina" ,"Vetrina", "Vetrina",
"Vetrina" ,"Vetrina", "Vetrina",
"Vetrina" ,"Vetrina")

length(x) = 26

and then I need x to be like:

x  = c("Vetrina" ,"Vetrina" ,"Vetrina",
"Vetrina" ,"Vetrina", "Vetrina",
"Vetrina" ,"Vetrina" ,"Vetrina",
"Vetrina nuovo","Vetrina",
"Vetrina" ,"Vetrina" ,"Vetrina",
"Vetrina" ,"Vetrina" ,"Vetrina",
"Vetrina" ,"Vetrina", "Vetrina",
"Vetrina" ,"Vetrina", "Vetrina",
"Vetrina" ,"Vetrina")

length(x) = 25

Alternative for huge if else statement

I have a huge data, and i need to filter them to get certain value according to Choices, i tried to use if else but it was so hard because it is really huge and nested.

    if(Type == "H" && Tech =="2A")
    {
       if(Level == "ground")
       {
        return "2AA";
       }
       else if (Level == "UP"){
        return "2AB";
    }

}
else if(Type == "H" && Tech =="3A"){
 if(Level == "ground"){
    return "3AA";
    }
    else if (Level == "UP"){
    return "3AB";

}

 else if(Type == "E" && Tech =="2B"){
 if(Level == "ground"){
    return "3BB";
    }
    else if (Level == "UP"){
    return "3BC";

}

How can I repeat if else innerHTML

How can I repeat this after click.

var maxResults = 0;
maxResults += 1;

if (maxResults < 7) {

  localStorage.setItem("example", id.innerHTML = "1" + id.innerHTML);

} else if (maxResults > 1 && maxResults < 8) {

  localStorage.setItem("example", id.innerHTML = "0");

}

Illustration the results as below:

111111
0
111111
0
111111
0

Thanks!