I've read through some of the if-else if question for for loop, but I cannot seem to solve what the problem is for my script.
type = numeric(length(r))
for (i in 1:10) {
if ( ((s_mov[i] < s_rot[i]) & (e_rot[i] < e_mov[i])) | ((s_rot[i] < s_mov[i]) & (s_mov[i] < e_rot[i])) == TRUE) {
type[i]=1
}
else if ( ((s_doc[i] < s_rot[i]) & (e_rot[i] < e_doc[i]) == TRUE) {
type[i]=2
}
else if ( ((s_rot[i] < e_mov[i]) & (s_doc[i] < e_rot[i])) | ((s_rot[i] < s_mov[i]) & (s_doc[i] < e_rot[i])) == TRUE) {
type[i]=3
}
}
or I've tried this way as well
for (i in 1:10) {
if ( ((s_mov[i] < s_rot[i]) & (e_rot[i] < e_mov[i])) | ((s_rot[i] < s_mov[i]) & (s_mov[i] < e_rot[i])) == TRUE) {
type[i]=1
}
else if ( ((s_doc[i] < s_rot[i]) & (e_rot[i] < e_doc[i]) == TRUE) {
type <- replace(type, type[i],2
}
else if ( ((s_rot[i] < e_mov[i]) & (s_doc[i] < e_rot[i])) | ((s_rot[i] < s_mov[i]) & (s_doc[i] < e_rot[i])) == TRUE) {
type <- replace(type, type[i],3
}
}
but I'm constantly getting
Error: unexpected '{' in:
What's wrong?
I'm not sure but I think depending on the data, when all 1:10 satisfy first if statement, it writes 1 for all 10 elements, and rather than stooping, it continues and cause error.
It looks like you're missing several closing brackets in different places. I highly recommend using a good IDE, such as RStudio, which has a side-benefit of highlighting the matching brackets.
Both the type <- replace... statements need a closing ).
Just by eyeballing, it looks like the first else if is missing a closing ) as well:
else if ( ((s_doc[i] < s_rot[i]) & (e_rot[i] < e_doc[i]) == TRUE) {
you have 4 opening and only 3 closing brackets in there.
I haven't checked the other statements, but you should.
In the first example, your first else if has an extra (.
In the second example, your first else if has an extra (, and your replace functions have no ).
Related
Let's suppose the next function:
demo_function <- function(x){
if(is.na(x)){
return(NA)
} else if(1 < x < 2){
return("something")
} else {
return("Nothing")
}
}
The idea is that when the argument x is between 1 and 2, say x=0.001, then the function returns something.
However when trying to run the above function, the next error arises:
Error: no function to go from, jumping to a higher level
How could I adjust the function in order to get "something" for the specified argument?
The issue is in the else if i.e. the syntax in R is different than the mathematical notation - multiple expressions are connected by logical operators
else if(1 < x && x < 2)
i.e.
demo_function <- function(x){
if(is.na(x)){
return(NA)
} else if(1 < x && x < 2){
return("something")
} else {
return("Nothing")
}
}
> demo_function(0.01)
[1] "Nothing"
> demo_function(1.5)
[1] "something"
> demo_function(NA)
[1] NA
I have a huge dataset with multiple columns representing the expression of a given gene in a specific tissue. The dataset is as follows with 74,440 genes:
I am trying to look for genes that have a very specific expression pattern and hence I am using multiple && operators in the if command in the code below. So essentially I am trying to find the genes that match the exact values I put in the if command. Any genes that match this criteria will be appended into a new file which is called Ovaries_only.
The data sample provided is exactly how the excel file looks and there are a lot more rows than showed. I am trying to see if the value in the 18th column exceeds 100 while the rest of the columns are lower than a set value. Based on this I am trying to append the entire row that fits the criteria into a new file.
As I run the code, I keep getting the error ""Error in if missing value where TRUE/FALSE needed" and also multiple warnings "In if (col.names) d[[2L]] else NULL :
the condition has length > 1 and only the first element will be used"
Could you tell me what I am doing wrong.
counter = TRUE
i = 1
while (counter == TRUE) {
if( (data[i,3] < 10) && (data[i,4] < 10) && (data[i,5] < 10) && (data[i,6] < 5) && (data[i,7] < 5) &&
(data[i,8] < 5) && (data[i,9] < 5) && (data[i,10] < 5) && (data[i,11] < 5) && (data[i,12] < 5) &&
(data[i,13] < 5) && (data[i,14] < 5) && (data[i,15] < 5) && (data[i,16] < 5) && (data[i,17] < 5) && (data[i,18] > 100) && (data[i,19] < 50) ){
df = data[i, ]
write.table(df, "ovaries_only.csv", sep = ",", row.names = FALSE, col.names = !file.exists("ovaries_only.csv", append = TRUE))
i = i + 1
if (i == 74440){
counter == FALSE
}
}
else{
i = i+1
if (i == 74440){
counter == FALSE
}
}
}
As the new user in R, I met few problems when I tried to evaluate a.
Code:
// time2 are numbers //
a = d3$Time2
b = c(...)
for (i in 1:65){
for (j in 1:1762){
if( (a[j]>=1474161415+900*(i-1))&(a[j]<1474161415+900*i) ){ a[j] = b[j] }
}
}
Results:
Error in if ((a[j] >= 1474161415 + 900 * (i - 1)) & (a[j] < 1474161415 + :
missing value where TRUE/FALSE needed
Also I have tried:
ifelse( ((a[j]>=1474161415+900*(i-1)) & (a[j]<1474161415+900*i)) , a[j]=b[j])
Results:
-unexpected '=' in -ifelse( ((a[j]-=1474161415+900-(i-1)) &
(a[j]-1474161415+900-i)) , a[j]=--
I have read in a csv file about weather on days. One of the variables is called winddirection (windrichting) but it is given in degrees and 0 and 990 have special meaning.
I want to change the numbers to North/South/West/East but when I used the replace function it changed the datatype of the whole vector to char which prevented my next replace with a range to work. So now I ended up with an if/else if/else statement but I can't get it to work. Also I get an error condition has length > 1. Can anyone help me get this to work so I have the ranges 45-135 to east, 135-225 south, etc.
here is my code where mydata$DD is the variable for wind direction with over 2000 values.
windrichting <- mydata$DD
windrichting <- if (windrichting == 990 ){
windrichting == "Veranderlijk"
} else if (windrichting>45 && windrichting<=135){
windrichting == "Oost"
} else if (windrichting>135 && windrichting<=225){
windrichting == "Zuid"
} else if (windrichting>225 && windrichting <=315){
windrichting == "West"
} else if (windrichting ==0){
windrichting == "Windstil"
} else windrichting == "Noord"
Perhaps something along the lines of this?
tmp <- round((windrichting + 45) / 90) %% 8
tmp[windrichting == 990] <- 8
tmp <- factor(tmp, levels = 0:9, labels = c("Noord", "Oost", "Zuid", "West", "Veranderlijk", "Windstil"))
tmp[windrichting == 0] <- "Windstil"
windrichting <- tmp
You can do this using an *apply function.
windrichting_text <- unlist(lapply(windrichting, function (x) {
if (x == 990) {
return("Veranderlijk")
} else if {
....
} else {
return ("Noord")
})
This applies the function defined to all elements in the list.
I've been writing an R script that would simulate outbound phone calls from a call center to demonstrate the rules ( if the call counts exceeds 5 OR if the call is a success OR a call back request is set then the code should exit). If I use break statement, I'm not getting the desired result, as a workaround I'm using the stop function. Is there a way to print a message and stop executing a function without throwing an Error ?
This is my code:
dial <- function(callcount = 1)
{
maxcalls <- 5
# Possible Outcomes
outcomes <- c("RPCON","WPCON","CBLTR")
# Probaility Vector for results:
pvector <- c(1,1,1)
repeat{
if(callcount == 5){
stop("5 attempts reached, closing record for the day")
}
res <- sample(outcomes, 1, prob=pvector, rep = TRUE)
print(paste0("Attempt ",callcount))
if(res == "RPCON" & callcount <= 5){
print("Call Successful")
stop(simpleError("Ended"))
}else if(res == "WPCON" & callcount <= 5){
print("Wrong Party!, Trying alternate number...")
callcount <- callcount + 1
dial(callcount)
}else if(res == "CBLTR" & callcount <= 5){
print("Call back request set by agent")
stop("Ended")
}
}# End of REPEAT loop
}# End of function
Any help is appreciated.
I would suggest to use a While loop with a boolean to check if you want to continue to loop :
dial <- function(callcount = 1)
{
maxcalls <- 5
# Possible Outcomes
outcomes <- c("RPCON","WPCON","CBLTR")
# Probaility Vector for results:
pvector <- c(1,1,1)
endLoop <- FALSE
while(callcount <=5 & endLoop == FALSE ){
if(callcount == 5){
stop("5 attempts reached, closing record for the day")
}
res <- sample(outcomes, 1, prob=pvector, rep = TRUE)
print(paste0("Attempt ",callcount))
if(res == "RPCON" & callcount <= 5){
print("Call Successful")
endLoop <- TRUE
}else if(res == "WPCON" & callcount <= 5){
print("Wrong Party!, Trying alternate number...")
callcount <- callcount + 1
dial(callcount)
}else if(res == "CBLTR" & callcount <= 5){
print("Call back request set by agent")
endLoop <- TRUE
}
}# End of REPEAT loop
}# End of function
Hope this helps.