This question already has an answer here:
if {...} else {...} : Does the line break between "}" and "else" really matters?
(1 answer)
Closed 1 year ago.
It works, but I wonder why tis is correct
if (one_time_cost_year_zero != 0) { EIW_TPI_flag = 1
} else {EIW_TPI_flag = 0}
while this results in an error
if (one_time_cost_year_zero != 0) { EIW_TPI_flag = 1 }
else {EIW_TPI_flag = 0}
What's the logic behind that?
Because R would not know that your if -else statement is not finished yet (since only the if () line is also valid R...) compare
1 + 2
+ 3
vs
1 + 2 +
3
In R if we want to split a command over multiple lines we need to either leave a bracket open (as in the if -else example) or have a "hanging" operator at the end of the line (there are also "multiline" strings, but they are not really commands per se)....
Then, the error you are seeing results from the fact that we can't beginn a command with else (like we can't start a command with in, | etc.)
Accordingly, we could also write:
if (one_time_cost_year_zero != 0) { EIW_TPI_flag = 1 } else
{EIW_TPI_flag = 0}
Related
This question already has answers here:
Function not returning the output in R
(3 answers)
Closed 1 year ago.
I was going through an R Function exercise, in this exercise we are required to create a function called bar_count that returns the least amount of aluminum bars required to fulfill an order, there are only two types of bars, 5kg bars and 1kg bars. The function should return F if the order has decimal it can only be exact numbers.
Here is what I came up with:
bar_count <- function(kg){
num.of.five <- 0
num.of.one <- 0
five.kg <- kg/5
if (kg%%5 == 0){
num.of.five <-num.of.five + five.kg
} else if(kg%%5 != trunc(kg%%5)){
return(F)
}else if (kg%%5 >= 1) {
num.of.five <- num.of.five + trunc(five.kg)
num.of.one <- num.of.one + ((kg%%5))
}
num.of.bars <- num.of.five + num.of.one
return (num.of.bars)
return(paste('qty of 5kg bars is:', num.of.five))
return(paste('qty of 1kg bars is:',num.of.one))
}
The problem I am having is with the two last commands where I ask R to return(paste()) the quantity of aluminum bars for the 5kg lot and the 1 kg lot, however it does not return any of those two,it only returns the total amount, I have tried with print() but it hasn't work either.
Thank you for taking the time to answer
Once you return from an R function, no code beyond the return statement will execute. Presumably you want to print before you return, so try using this version:
bar_count <- function(kg) {
// ...
print(paste('qty of 5kg bars is:', num.of.five))
print(paste('qty of 1kg bars is:', num.of.one))
return(num.of.bars)
}
I'm trying to get the product of 2 fields in the 'ELSE' portion of a CASE statement of a Formula(Text) row in a Netsuite saved search, but I keep getting 'ERROR: Invalid Expression' when I run the search. Current formula is:
CASE WHEN {binonhandcount} = 0 THEN '0' ELSE NVL({binonhandcount}, 0) * NVL({locationaveragecost}, 0) END
I've tried to simplify it by doing something like this:
CASE WHEN {binonhandcount} = 0 THEN '0' ELSE 1 + 1 END
But it still fails with an invalid expression error. All of the Googling I've done leads me to believe that this should work, but I can't seem to find my mistake. I'm hoping the extra eyes here can give me a kick in the right direction. Thank you.
The data type returned from the formula needs to match the Formula type selected. You can correct your formula by setting it to a Formula (Numeric) type and simply removing the quotes around the '0' after the first THEN:
CASE WHEN {binonhandcount} = 0 THEN 0 ELSE NVL({binonhandcount}, 0) * NVL({locationaveragecost}, 0) END
Or if you really want a text formula for some reason, you can wrap the ELSE statement in a TO_CHAR function:
CASE WHEN {binonhandcount} = 0 THEN '0' ELSE TO_CHAR(NVL({binonhandcount}, 0) * NVL({locationaveragecost}, 0)) END
In your NetSuite saved search replace Formula(text) to Formula(Numeric).
And your formula would be:
CASE WHEN {binonhandcount} = 0 THEN 0 ELSE NVL({binonhandcount}, 0) * NVL({locationaveragecost}, 0) END
Please remove the string from THEN '0'. You should be fine.
I am trying to cut down a list of gene names that I have been given. I'm trying to eliminate any repetitive names that may be present but I keep getting an error when running my code:
counter=0
i=0
j=0
geneNamesRevised=array(dim=length(geneNames))
for (i in 0:length(geneNamesRevised))
geneNamesRevised[i]=""
geneNamesRevised
for (i in 1:length(geneNames))
for (j in 1:length(geneNamesRevised))
if (geneNames[i]==geneNamesRevised[j])
{
break
}
else if ((j==length(geneNamesRevised)-1) &&
(geneNames[i]!=geneNamesRevised[j]))
{
geneNamesRevised[counter]=geneNames[i]
counter++
}
The error message is a repetitive string of :
the condition has length > 1 and only the first element will be usedthe condition has length > 1 and only the first element will be usedthe condition has length > 1 and only the first element will be used
and this error message is for the last "else if" statement that has the '&&'.
Thank you!
Why not just
geneNamesRevised <- unique( geneNames )
... which returns a shortened list. There is also a duplicated function that can be used to remove duplicates when negated.
There are a few problems in your code.
1) The else is incorrectly specified - or not :) thanks #Mohsen_Fatemi
2) & is usually what you need rather than &&
3) counter++ isn't R
Copy the code below and see if it runs
for (i in 1:length(geneNames)){
for (j in 1:length(geneNamesRevised)){
if (geneNames[i]==geneNamesRevised[j])
{
break
} else {
if ((j==length(geneNamesRevised)-1) & (geneNames[i]!=geneNamesRevised[j]))
{
geneNamesRevised[counter]=geneNames[i]
counter <- counter + 1
}
}
}
}
Edit
4) also you were missing braces for your fors
use & instead of && ,
else if ((j==length(geneNamesRevised)-1) & (geneNames[i]!=geneNamesRevised[j]))
This question already has answers here:
R: Break for loop
(2 answers)
Closed 8 years ago.
How can I end a for loop if a condition is met inside it?
I want to break a for loop once t = 1 for example, but as of now I am only breaking out of an if statement inside the for loop.
t = 0
x = matrix()
for (i in 1:10){
if (t == 1){
break }
t = t + 0.01
x[i] = t
}
Assuming that you mean, for (i in 1:101), you won't get a value equal to 1 in the loop because .01 cannot be exactly represented in binary.
With the modified for statement, the 100'th value is not exactly equal to 1:
x[100] - 1
## [1] 6.661338e-16
To break when something "exceeds or equals" another value, you would use >=. That is, modify the test to read if (t >= 1) { break }. In general, you should not use == to compare floating-point numbers.
I have an element in a data frame tmp that may contain either a number, 0, or NA. If that element is neither 0 or NA, I would like something to happen. Otherwise, nothing happens. I imagine it'd look like this:
if ( tmp[2, 19] != (0 || NA) ){
do something
}
I get this error: Error in if (tmp[2, 19] == (0 || NA)) { : missing value where TRUE/FALSE needed. I don't know if it's not possible in R to compare something to both an int and a string or if I'm just using the OR operator wrong. I've tried different variations in different cases but haven't been able to determine the problem. Please help!
As #GSee said in a comment, you need to use is.na:
if(tmp[2, 19] != 0 || is.na(tmp[2, 19])) {
# do stuff
}
You could have discovered this yourself by reading ?"if" and ?NA.