I have the following code to check if the dataset existed and if the dataset has data, and the problem is coming from the double quote around the macro variable.
check_data_ready <- defmacro(tracking_sheet,table_df,table_name,
expr={if (exists(table_df) && is.data.frame(get(table_df)) && dim(table_df)==NULL) {`tracking_sheet$DataReady[tracking_sheet$Table==table_name]<-'Ready'
} else {tracking_sheet$DataReady[tracking_sheet$Table==table_name]<-'Check!'}
})
check_data_ready(tracking_sheet,"df","Table")
the error msg is
Error in if (exists("df") && is.data.frame(get("df")) && dim("df") ==
So apparently dim("df") is not working, which should be dim(df). I try to substr the double quote from 2 to 3, but looks like it recognize d as 1 and f as 2, it doesn't count the double quote. I am lost here, how do I make the code working like this
if (exists("df") && is.data.frame(get("df")) && dim(df) ==
Related
I am looking to create a couple of options with optparse in R. I want to feed the R script an argument from bash. This option will first check the length of the argument and then it will check if the argument contains all numbers or a combo of numbers and letters. This should be written as an if then statement.
So what I have so far:
if (opt$TYPE) != "RUNDATE" && str_length(opt$IDNO) != 8 && opt$IDNO != opt$IDNO[0-9]{8} {
print_help(opt_parser)
stop("Not a valid Identity Number.n", call. = FALSE)
}
if (opt$TYPE) != "LIMSID" && str_length(opt$IDNO) == 7 && opt$IDNO != opt$IDNO[A-Z]{3}[0-9]{4} {
print_help(opt_parser)
stop("Not a valid Identity Number.n", call. = FALSE)
}
Example: ABC1234 & ABCD123
needs to be checked so that the first three characters are letters, and that the last four characters are numbers. This would allow ABC1234 to proceed while ABCD123 would trigger the error message and stop the function.
I have an idea for the syntax in bash and am looking for it's equivalent in R:
[[A-Z]]{3}[0-9]{4}
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]))
The docs for Julia indicate that a valid short-cut to writing out an if statement is the syntax
<cond> && <statement>
I've used this a lot for error messages, e.g. length(x) < N && error("x is too short"), and it works as expected. However, the following does not work:
x = 3
x < 4 && x = 5
I get an error of the form syntax: invalid assignment location. What is going on here?
What I'm trying to do is check if x is less than 4, and if it is, then set x to 5. Should I do the following?
if x < 4
x = 5
end
Is there a valid short-circuit method for this situation?
Your error is caused because the && operator has higher precedence than the assignment operator = so your line of code is executed as if you would write (x < 4 && x) = 5.
For a solution you have to add parantheses.
x < 4 && (x = 5)
See my code running in the browser
I can't get this to work. I want to replace all two character occurences in the first field of a csv file with the occurence and an X appended, and whitespace removed. For example SA and SA should map to SAX in the new file. Below is what I tried with sed (from help through an earlier question)
system( paste("sed ","'" ,' s/^GG/GGX/g; s/^GG\\s/GGX/g; s/^GP/GPX/g;
s/^GP\\s/GPX/g; s/^FG/FGX/g; s/^FG\\s/FGX/g; s/^SA/SAX/g; s/^SA\\s/SAX/g;
s/^TP/TPX/g; s/^TP\\s/TPX/g ',"'",' ./data/concat_csv.2 >
./data/concatenated_csv.2 ',sep=''))
I tried using the sQuote() function, but this still doesn't help. The file has problems being handled by read.csv because there are errors within some fields based on too many and not enough separators on certain lines.
I could try reading in and editing the file in pieces, but I don't know how to do that as a streaming process.
I really just want to edit the first field of the file using a system() call. The file is about 30GB.
try the following on a file like so:
echo "fi,second,third" | awk '{len = split($0,array,","); str = ""; for (i = 1; i <= len; ++i) if (i == 1) { m = split(array[i],array2,""); if (m == 2) {str = array[i]"X";} else {str = array[i]};} else str = str","array[i]; print str;}'
so you would call it from R using the following as input to the paste() call
cat fileNameToBeRead | awk '{len = split($0,array,","); str = ""; for (i = 1; i <= len; ++i) if (i == 1) { m = split(array[i],array2,""); if (m == 2) {str = array[i]"X";} else {str = array[i]};} else str = str","array[i]; print str;}' > newFile
this code won't handle your whitespace requirement though. could you provide examples to demonstrate the sort of functionality you're looking at
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.