Using an if & statement - r

I am trying to do an if & statment in R:
I want to do something like this:
if (x > 1) & (y = "Yes) {"replace")
I've also tried
if (x > 1) && (y = "Yes") {"replace")
Which I read on StackOverflow.
How do I convert the excel formula
=IF(AND(cell > 1, cell = "Yes"),100,0)

Try this. Does it work?
if (x > 1 & y == "Yes") {"replace"}

Related

How can you check whether a sequence is an 'almost increasing sequence' in R?

A sequence (e.g. c(1,2,3,4)) is almost increasing when we can remove exactly one element from the sequence and get a strictly increasing sequence (i.e. a0 < a1 < ... < an). I'm trying to find a way to check whether a sequence is almost increasing. If it is, I want to return TRUE; if it isn't I want to output FALSE. I've got this far:
solution <- function(sequence) {
sequence1 <- unlist(sequence)
if (length(sequence1) == 1) {
next
}
count <- 0
for (i in (length(sequence1) - 1)) {
if (sequence1[i + 1] > sequence1[i]) {
next
} else if (((sequence1[i + 2] > sequence1[i]) & count == 0) & i !=
length(sequence1)-1) {
sequence1 <- sequence1[- (i + 1)]
count <- count + 1
} else if ((sequence1[i + 1] > sequence1[i - 1]) & count == 0 & i != 1) {
sequence1 <- sequence1[-i]
count <- count + 1
} else {
return(FALSE)
}
}
return(TRUE)
}
I've used unlist() because codesignal, for some reason, doesn't accept you to refer to the function argument within the function. This works for some sequences: solution(c(4,1,5)) correctly returns TRUE. It doesn't work for others: solution(c(1, 1, 1, 2, 3)) incorrectly returns TRUE. solution(c(2,1,2,1)) correctly returns FALSE and yet solution(c(1,2,1,2)) incorrectly returns TRUE. I've lost my grip on what's going on. I wonder if anyone can spot anything?
Clarification: the basic idea of my code is to iterate through the sequence and for each element check whether its right neighbour is a bigger number. If it isn't, then we have two options: get rid of i or get rid of i+1, so I check those in turn. Since we can only make one change, i've added the condition that if count is 1, then we skip to finish. Also, if the index is 1 then we can't check i-1, and if the index is length(sequence)-1, then we can't check i+2, so i've added those conditions in to make sure my code skips to the other option if appropriate.
Here is a solution which works for me. The idea is that diff(x) has negative elements for every downwards step in x. For example, min(diff(x)) is positive, if x is strictly increasing. If diff(x)[i] <= 0 for exactly one index i, we have to check whether either removing x[i] or removing x[i+1] makes the sequence strictly increasing. The following function passed all tests I tried:
check_almost <- function(x) {
if (length(x) < 2) {
return(TRUE)
}
d <- diff(x)
i <- which(d <= 0)
if (length(i) == 0) {
return(TRUE) # strictly increasing
} else if (length(i) > 1) {
return(FALSE)
}
return(i == 1 || # we can remove x[1]
i == length(d) || # we can remove x[length(x)]
d[i-1]+d[i] > 0 || # we can remove x[i]
d[i] + d[i+1] > 0) # we can remove x[i+1]
}

I am trying to use an if else loop to check multiple columns for data ranges, how do I make it so that it ignores NAs in my data

I am trying to check numbers in three columns in a data frame and if they're within a certain range, I want a certain output. I have this part of the code, but one of my tests wants to know if all three are negative, then I get a certain output. My issue is that some of the data in some of the columns are NA. I want to ignore the NAs in my logic. Is there a way to do this? A sample of my code is below.
if((DataWSGR$RouteType == 7 | DataWSGR$RouteType == 9) & (DataWSGR$SGR > 5 ) & (0 < DataWSGR$`30_Year_SGR` < 5) & (0 < DataWSGR$`20_Year_SGR` < 5) & (0 < DataWSGR$`10_Year_SGR` < 5)) {}
The 10, 20, and 30 year SGRs are the columns that will have NAs in them.
After fixing the range condition, I think you can just add | is.na(var) to the last three conditions:
if ((DataWSGR$RouteType == 7 |
DataWSGR$RouteType == 9)) &
(DataWSGR$SGR > 5) &
(DataWSGR$`30_Year_SGR` > 0 & DataWSGR$`30_Year_SGR` < 5 | is.na(DataWSGR$`30_Year_SGR`)) &
(DataWSGR$`20_Year_SGR` > 0 & DataWSGR$`20_Year_SGR` < 5 | is.na(DataWSGR$`20_Year_SGR`)) &
(DataWSGR$`10_Year_SGR` > 0 & DataWSGR$`10_Year_SGR` < 5 | is.na(DataWSGR$`10_Year_SGR`))) {
}
If DataWSGR has more than one row, the above will throw an error.
Here is a reproducible example for doing this in a for loop:
df <- data.frame(
route_type = c(7, 6, 9),
sgr = c(6, 3, 6),
sgr_30 = c(3, 1, NA),
sgr_20 = c(1, 1, NA),
sgr_10 = c(2, 1, NA)
)
for (i in 1:nrow(df)) {
if (
(df$route_type[i] == 7 | df$route_type[i] == 9) &
(df$sgr[i] > 5) &
(df$sgr_30[1] > 0 & df$sgr_30[i] < 5 | is.na(df$sgr_30[i])) &
(df$sgr_20[1] > 0 & df$sgr_20[i] < 5 | is.na(df$sgr_20[i])) &
(df$sgr_10[1] > 0 & df$sgr_10[i] < 5 | is.na(df$sgr_10[i]))
) {
print(paste("In range in row", i))
}
}

pytorch: RuntimeError: bool value of Tensor with more than one value is ambiguous

it works with
x[x >= 0.2] = 1
x[x < 0.2] = 0
x is a tensor here.
but when i am trying to use
x[x > 0 and x < 1] = 1
it reports: RuntimeError: bool value of Tensor with more than one value is ambiguous ?
dose anyone know why?
Just a syntax thing.
x = torch.randn((1,3,20,20))
x[(x > 0) & (x < 1)] = 1

Creating a function which returns certain value

I have this exercise and I cant figure it out.
Create a function ans(x, y, c) which returns the value c*x^2*y, if x^2 <= y <= 1, and the value 0 otherwise. When you are ready input c.
I have 2 different solutions but I can't quite understand how to organize the function correctly. Neither one is correct.
Solution 1)
ans <- function(x,y,c){
if (x^2 <= y && <= 1)
return(c*x^2*y)
}
else{
return(0)
}
Solution 2)
ans <- function(x,y,c){
if (x^2 <= y & y <= 1)
return(c*x^2*y)
else if(x^2 <= 1){
return(c*x^2*y)
}
else{}
return(0)
}
Just check the format of your function. Here may be something you want:
ans <- function(x,y,c){
if (x^2 <= y & y <= 1){
return(c*x^2*y)
}else{return(0)}}

Error when running R file from command line

I have an R file which imports a file, does some data manipulation, and performs a logistic regression model, and then saves those results to a txt file. However, when I run the file from the command line, I get the following error message and don't know what's going on.
anonymous#anonymous-Latitude-E6520:~/Downloads$ R --no-save < Auto_Model.r > out.txt
Warning message:
NAs introduced by coercion
Error in if (x == "\\N") NA else if (x > 1 & x < 6999) "1:6999" else if (x > :
missing value where TRUE/FALSE needed
Calls: bin.value -> do.call -> mapply -> .Call -> <Anonymous>
Execution halted
anonymous#anonymous-Latitude-E6520:~/Downloads$ R --no-save < Auto_Model.r
The R script which results in the error is below =
> ## IMPORT DATA:
> #setwd("~/Desktop")
> library(foreign)
> dat = read.csv("dat.csv", stringsAsFactors=FALSE)
>
> ## zipcode =
> dat$zipcode = as.character(dat$zipcode)
>
> bin.value = Vectorize(function(x) {
+ if (x == "\\N") NA
+ else if (x > 1 & x < 6999) "1:6999"
+ else if (x > 7000 & x < 9999) "7000:9999"
+ else if (x > 10000 & x < 14849) "10000:14849"
+ else if (x > 14850 & x < 19699) "14850:19699"
+ else if (x > 19700 & x < 29999) "19700:29999"
+ else if (x > 30000 & x < 31999) "30000:31999"
+ else if (x > 32000 & x < 34999) "32000:34999"
+ else if (x > 35000 & x < 42999) "35000:42999"
+ else if (x > 43000 & x < 49999) "43000:49999"
+ else if (x > 50000 & x < 59999) "50000:59999"
+ else if (x > 60000 & x < 69999) "60000:69999"
+ else if (x > 70000 & x < 79999) "70000:79999"
+ else if (x > 80000 & x < 89999) "80000:89999"
+ else if (x > 90000 & x < 96999) "90000:96999"
+ else if (x > 97000 & x < 99820) "97000:99820"
+ else NA
+ })
>
> dat$zipcode2 = as.character(bin.value(as.integer(dat$zipcode)))
Error in if (x == "\\N") NA else if (x > 1 & x < 6999) "1:6999" else if (x > :
missing value where TRUE/FALSE needed
Calls: bin.value -> do.call -> mapply -> .Call -> <Anonymous>
Execution halted
I assume some is wrong in how I am trying to manipulate the mode of the zipcode variable but nothing I've tried seems to fix the issue.
> str(dat$zipcode)
int [1:12635] 76148 33825 61832 11368 98290 92078 44104 62052 55106 20861 ...
>
It seems to me that what you're trying to do is already done by function cut:
bin.value <- function(x){
cut(as.integer(x),
breaks= c(1,6999,9999,14849,19699,29999,31999,34999,42999,49999,59999,69999,79999,89999,96999,99820),
labels= c("1:6999", "7000:9999", "10000:14849", "14850:19699", "19700:29999", "30000:31999", "32000:34999", "35000:42999", "43000:49999", "50000:59999", "60000:69999", "70000:79999", "80000:89999", "90000:96999", "97000:99820"))
}
Otherwise your specific problem is caused by as.integer:
a <- c("\\N",sample(seq(0,100000,by=1),10))
a
[1] "\\N" "38987" "50403" "75683" "66706" "27924" "17216" "77539" "80658" "2335" "53010"
as.integer(a)
[1] NA 38987 50403 75683 66706 27924 17216 77539 80658 2335 53010
\\N is therefore traited straight away as NA which your loop only handle at the end, meanwhile all ifstatements try to compare a missing value with some elements.
as.integer(a)[1]=="\\N"
[1] NA # Instead of TRUE or FALSE

Resources