using 'load' for the sample below results in Boolean output instead of the stored data
> --> a=rand(3,3)
> a =
> 0.8833888 0.9329616 0.3616361
> 0.6525135 0.2146008 0.2922267
> 0.3076091 0.312642 0.5664249
>
>
> --> save ('rand_matrix.dat','a')
>
> --> ls ans =
>
> rand_matrix.dat
>
>
> --> load("rand_matrix.dat",'a')
> ans =
> T
first of all it would be great if you could spend some time learning MarkDown to be able to have readable posts here on StackOverflow. Your original code block was distorted. Please take a look at how I have edited it so next time you can use the same format. Secondly try to look into the original page of the functions you are trying to use, in advance to posting a question. In this case the page have implicitly explained that this is the expected behavior. Lrt's go through your codes:
--> a=rand(3,3)
a =
0.2113249 0.3303271 0.8497452
0.7560439 0.6653811 0.685731
0.0002211 0.6283918 0.8782165
--> save ('rand_matrix.dat','a')
This will create rand_matrix.dat on your current working directory and stores the value of the variable a there in binary format. Now if you remove a from your workspace:
--> clear a
--> a
Undefined variable:a
and use the load the variable and its data again from the file
--> load("rand_matrix.dat",'a')
ans =
T
--> a
a =
0.2113249 0.3303271 0.8497452
0.7560439 0.6653811 0.685731
0.0002211 0.6283918 0.8782165
It will be loaded to the workspace again. The T the load function returns means that it has successfully loaded the file and the variable.
Related
I am trying to save a .maf file as a table, but I always get the error below:
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ‘structure("MAF", package = "maftools")’ to a data.frame
This is the code I am using:
library(maftools)
laml.maf <- "/Users/PC/mc3.v0.2.8.PUBLIC.maf"
laml = read.maf(maf = laml.maf)
write.table(laml, file="/Users/PC/tp53atm.txt")
I understand that the .maf file has several fields, but I am not sure how to isolate them to save as a table. Any help would be much appreciated!
The problem is, that the write.table function doesn't know how to deal with an object of class MAF.
However, you can access the underlying data like this:
write.table(laml#data, file="/Users/PC/tp53atm.txt")
But note that this way you will only export the raw data, whereas the MAF object contains various other meta data:
> slotNames(laml)
[1] "data" "variants.per.sample" "variant.type.summary" "variant.classification.summary"
[5] "gene.summary" "summary"
"maf.silent" "clinical.data"
>
I've used below code which successfully compares two text files and logs the difference in a log file using for loop. The file names are in series, for example, File_1, File_2 etc.. but when there is a missing file in the series, the code stops the execution with error - No such file or directory.
Then I've used if condition to check the file existence but I am getting a below-mentioned error.
Please help me to skip comparison for a nonexisting file.
Code:
for(i in 1:length){
prod_file_res_name <- sprintf("path/Query_Prod_%s.txt", i)
beta_file_res_name <- sprintf("path/Query_Beta_%s.txt", i)
if (exists('prod_file_res_name' && 'beta_file_res_name')){
res <- tools::Rdiff(prod_file_res_name, beta_file_res_name, Log = TRUE)
if(res[2] != "character(0)"){
write(toString(res[2]), file = "LogFile.txt",append=TRUE)
}
else{
elsevar <- sprintf("No difference found between prod and beta responses for query %s", i)
print(elsevar)
}
}
}
Error:
Error in "prod_file_res_name" && "beta_file_res_name" :
invalid 'x' type in 'x && y'
exists checks if the given R objects (only takes R objects as input --> this caused your first error) exist in your environment. You initiate the R objects prod_file_res_name and beta_file_res_name before you check if they exist, so the exists call will always return TRUE. What you are looking for is the file.exists function which checks if the file does exist in your working directory:
file.exists(prod_file_res_name) && file.exists(beta_file_res_name)
The second error was caused by the R objects existing but not the files you want to check.
Since exists() looks up single objects (from Documentation):
Is an Object Defined?
Description
Look for an R object of the given name and possibly return it
'prod_file_res_name' && 'beta_file_res_name' doesn't work.
Rewrite
exists("prod_file_res_name" && "beta_file_res_name")
to:
exists("prod_file_res_name") && exists("beta_file_res_name")
I am trying to create a data frame of various error messages based on Data to be cross checked between two dataframes and storing the message in a vector in an iterative manner . I am using the following snippet for this purpose :
> for(j in 1:nrow(MySQL_Data)){ date_mysql=
> paste("MySQL_Data[",j,",1]") date_red= paste("RED_Data[",j,",1]")
> body= c() if(!date_mysql == date_red) {
> body<- append(body,paste("'There is data missing for date",date_mysql,"in",table2)) }else {
> NULL }}
My table2 variable prints as MYSQL_Data[2,1] instead of the actual value of the variable which is a date
Following is the Output :
"'There is data missing for date MySQL_Data[ 2 ,1] in Dream11_UserRegistration"
Can someone help me with the error that I am committing here..
Thanks in Advance !
Your use of paste in the definitions of data_mysql and data_red makes no sense. I’m assuming that what you actually want is this:
data_mysql = MySQL_Data[j, 1]
data_red = RED_Data[j, i]
Furthermore, you’re resetting body in every loop iteration so it will only ever hold a single element.
I'm trying to use dump() to save the settings of my analysis so I can examine them in a text editor or reload them at a later date.
In my code I'm using the command
dump(ls(), settingsOutput, append=TRUE)
The file defined by `settingsOutput' gets created, but the larger objects and locally defined functions are truncated. Here's an excerpt from such a file. Note these files are generally on the order of a few kb.
createFilePrefix <-
function (runDesc, runID, restartNumber)
{
...
createRunDesc <-
function (genomeName, nGenes, nMix, mixDef, phiFlag)
{
...
datasetID <-
"02"
descriptionPartsList <-
c("genomeNameTest", "nGenesTest", "numMixTest", "mixDefTest",
"phiFlagTest", "runDescTest", "runIDTest", "restartNumberTest"
...
diffTime <-
structure(0.531, units = "hours", class = "difftime")
dissectObjectFileName <-
function (objectFileName)
{
...
divergence <-
0
Just for reference, here's one of the functions defined above
createFilePrefix <- function(runDesc, runID, restartNumber){
paste(runDesc, "_run-", runID, "_restartNumber-", restartNumber, sep="")
}
Right now I'm going back and removing the problematic lines and then loading the files, but I'd prefer to actually have code that works as intended.
Can anyone explain to me why I'm getting this behavior and what to do to fix it?
How can I extend the exists function to work with the following:
Any ideas how I would extend to this to looking at seeing whether a nested dictionary would also exist. I.e. for example: if(exists("mylists[[index]]['TSI']")), where the mylists object is a dictionary look up that also wants to contain a nested dictionary.
Now mylists will look like:
[[index]]["TSI"]=c(0="a",1="b")
How should I check this exists so that I may append it so I have:
[[index]]["TSI"]=c(0="a",1="b",2="c")
Here is more code that illustrates things better:
index is an ID
if(!is.null(listsar[[index]]["TSI"])) {
print("extending existing")
listsar[[index]][["TSI"]] <- c(listsar[[index]][["TSI"]], risktype=myTSI)
}else
{
print("creating new")
listsar[[index]][["TSI"]] <- c(risktype=myTSI)
}
However this does not seem to work. I get the "extending existing" and I never seem to get the "creating new". If I change the evaluation line to:
if(!is.null(listsar[[index]][["TSI"]]))
I get different statement:
"creating new"
You can test for NULL in most cases. Sample data (which is something you should have given us along with working code - wtf is c(0="a",1="b",2="c") supposed to be?)
> mylists=list()
> mylists[["foo"]]=list()
> mylists[["foo"]][["TSI"]]=c(a=0,b=1)
Does a "foo" exist at the top level?
> !is.null(mylists[["foo"]])
[1] TRUE
Yes.
Does a "fnord" exist at the top level?
> !is.null(mylists[["fnord"]])
[1] FALSE
No.
Does a "TSI" exist within "foo"?
> !is.null(mylists[["foo"]][["TSI"]])
[1] TRUE
Yes.
Does a "FNORD" exist within "foo"?
> !is.null(mylists[["foo"]][["FNORD"]])
[1] FALSE
No.
Does a "FNORD" exist within a top-level (and nonexistent) "fnord":
> !is.null(mylists[["fnord"]][["FNORD"]])
[1] FALSE
No.