R Write a dbf file - r

I want to create a dbf file to export a data frame to, I already tried:
write.dbf(MyDF,MyDF.dbf,factor2char = F)
but get the error code:
Error in write.dbf(MyDF, MyDF.dbf, factor2char = F) :
object 'MyDF.dbf' not found
I can tell why but I just can't find a solution.

The 2nd parameter should be a string. Try this:
write.dbf(MyDF,"MyDF.dbf",factor2char = F)
The error you are getting is that MyDF.dbf is being treated as a variable name and you haven't defined a variable with that name.

Related

Saving .maf file as a table

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"
>

Concatenate variables in R

I want to create an object in R, which will contain one string, with a few variables (in my case it is file path). When I try to use paste to concatenate the file paths I can see only one last variable instead of all variables in one string. I use next code:
for(i in seq_len(nrow(samples))) {
lib = samples$conditions[i]
txtFile = file.path(lib, "hits.txt")
testfiles = paste(txtFile, sep = ',')
}
print(testfiles)
and get something like
cond/hits.txt,
instead of
cond/hits.txt,cond1/hits.txt,cond2/hits.txt and so on
Thank you very much for help

Save a data frame to a file addressing by name

I have a data frame and a text variable containing the name of this data frame:
adsl = data.frame(a=2, b=7, w=17)
ds_name = "adsl"
I want to save my data frame from the workspace to the file named "dest_file". The code should be wrapped into a function get_r()
with the data frame name as an argument:
get_r(ds_name="adsl")
So I need to avoid using the explicit name "adsl" inside the code.
The following works almost correctly but the resulting data frame is called "temp_dataset", not "adsl":
get_r = function(ds_name){
temp_dataset = eval(parse(text=ds_name))
save(temp_dataset, file = "dest_file")
}
Here is another option which works wrong (the text string is being saved, not the data frame):
get_r = function(ds_name){
save(ds_name, file = "dest_file")
}
What should I do to make R just execute
save(adsl, file="dest_file")
inside the function? Thank you for any help.
Try
save(list = ds_name, file = "dest_file")
The list argument in save() allows you to pass the name of the data as a character string. See help(save) for more.

Unexpected string constant in R when try to select colname from data.table

I try to group by my customize movieLense dataset
groupBy<- data.table(unifiedTbl)
x<- groupBy[,list(rating=sum(rating)
,Unknown=sum(unknown)
,Action=sum(Action)
,Adventure = sum(Adventure)
,Animation = sum(Animation)
,"Children's" = sum(Children's)
),by=list(user_id,age,occupation)]
but because of Children's I received some error which related to specified character
If I remove below part of my code every things is OK
,"Children's" = sum(Children's)
Now my question is how can I address to this column with full name?
how can I fix my codes?
You can use backticks with names that aren't valid syntax:
`Children's` = sum(`Children's`)
And of course, I'd recommend creating valid names instead:
setnames(groupBy, make.names(names(groupBy)))

using arulesSequences package : Error in makebin(data, file) : 'sid' invalid

I am using arulesSequences package in R. The documentation is too little for the type of data that read_baskets function receives. I guess data should be in text (.txt) format. Column names are: "sequenceID", "eventID", "SIZE" and "items". My data has about 200,000 rows and looks like following in z.txt file:
1,1364,3,{12,17,19}
1,1130,4,{14,17,21,23}
1,1173,3,{19,23,9}
1,98,5,{14,15,2,21,5}
2,1878,4,{1,10,14,3}
2,1878,13,{1,12,14,15,16,17,18,19,2,21,24,25,5}
2,1878,1,{2}
I tried to use:
x <- read_baskets("z.txt", sep = ",",info =c("sequenceID","eventID","SIZE"))
s <- cspade(x,parameter = list(support = 0.001),control = list(verbose =
TRUE),tmpdir = tempdir())
but I get this error :
Error in makebin(data, file) : 'sid' invalid
The combination of sequenceID and eventID must be unique.
Otherwise you'll get one of these errors:
Error in makebin(data, file) : 'sid' invalid
Error in makebin(data, file) : 'eid' invalid
This implies further that the items in your .txt file (per sequenceID, eventID combination) must be in the same row and (possibly) be separated with the same separator as the rest of the .txt file. Therefore, the item column should be the last column.
Hope this helps!
Ok I found the problem, and I'm posting it in case that some one has the same problem. The problem is both SequenceID and eventID (first and second columns must be ordered blockwise. package mentions this point, but I only ordered the first column.

Resources