How can i write as a csv file for S4 class? - r

I am using DEP proteomics package to analyse my mass spectometry data. I want to remove the batch effect from the my data. So after the preprocessing of my data i want to download as a CSV file file so that i can upload into batch server. but I am not able to write as CSV. Whenever i try i am getting error (no method for coercing this S4 class to a vector)? I am new to R. i read something about this and still i don't have clear idea?
can someone help me with this?
> data_se <- make_se(data_unique, LFQ_columns, experimental_design)
> LFQ_columns <- grep("LFQ.", colnames(data_unique))
> data_se_parsed <- make_se_parse(data_unique, LFQ_columns)
> is(data_se)
[1] "SummarizedExperiment" "RectangularData" "Vector" "Annotated"
[5] "vector_OR_Vector"
> data_se#metadata
list()
> View(data_se)
> colnames(data_se)
[1] "Ubi4_1" "Ubi4_2" "Ubi4_3" "Ubi6_1" "Ubi6_2" "Ubi6_3" "Ctrl_1" "Ctrl_2" "Ctrl_3" "Ubi1_1" "Ubi1_2" "Ubi1_3"
> write.csv2(data_se, "/home/dell/Desktop/Preoteomics_TMT_data/ubi_data_se.csv")
Error in as.vector(x) : no method for coercing this S4 class to a vector

Sorry, the question is not too clear (not sure what you want to upload for example), but you can access and write to file what looks like mass spec readings using the following:
my_data <- data_se#assays#data#listData
write.csv2(my_data, "my_upload.csv")
It probably won't be in the format needed for your batch server, but that is a separate question.
Not sure if this is the data you want! If you have no luck here, try biostars, where this type of question is more common.

Related

How to I create a data frame from inbuilt data set 'iris'?

I am a beginner at using Rstudio and have been working through the exercises outlined as part of our course notes.
We are to work with the 'iris' dataset however I haven't been able to successfully save it as a valid data.frame. The best I have done is created an empty dataframe in the global environment with 0 obs. of 0 variables.
Here is some of the codes I have worked through and the outputs. I am very new to R and am struggling a little with using inbuilt data sets in terms of loading and using - I am ok with importing and creating however.
data()
> View(iris)
> iris<-write.csv(iris)
""
> iris
NULL
> str(iris)
NULL
> iris<-data.frame(iris)
> iris<-read.csv(iris.csv)
Error in read.table(file = file, header = header, sep = sep, quote = quote, :
object 'iris.csv' not found
> library(datasets)
> data.frame(iris)
data frame with 0 columns and 0 rows
I have tried > write.csv(iris, 'iris.csv') # no luck
First check if iris is already a data.frame by running the following command:-
is.data.frame(iris)
If the answer is TRUE, then run in the following command to write it to a .csv file:-
write.csv(iris, "/location/at/which/you/want/to/save/the/file)
If one wants to save objects as R objects, one has to use save() and the file extension has to be .RData. Like in you case you can run the following command:-
save(iris, file = '/location/iris.RData'
And you can load an .RData file with the load() function in R. In your case it could be :-
load('/location/iris.RData')
Some mistakes that you've made:-
In your second code line where you run
> iris<-write.csv(iris)
you've just provided write.csv with it's first argument called x, but never specified the second argument it requires which is file. And also, one never assign write.csv() function with it's arguments to an object with the help of <- because write.csv() is a function which does not returns a value or an object. Another example of a function like write.csv() could be library().
So the way you code flows is you worte a wrong syntax for by running the following line
> iris<-write.csv(iris)
and hence, you got a NULL object. And the str of a NULL object is itself NULL.
Then you created a data.frame objects by passing iris as a data object, but since earlier iris became a NULL object, data.frame of a NULL object is NULL. Since there never was an iris.csv file written, R won't be able to read it too.
Also in your read.csv() function, you passed the file argument as a data object and not as a path. This is why you got the error object 'iris.csv' not found and not as cannot open file 'iris.csv': No such file or directory. To pass it as a path you should always put the location of your file in quotes, either single or double.
If you ever you don't understand how you have to pass objects in a function, please run the command ?function_name, for example ?write.csv(), ?library, ?read.csv. This will provide you with documentation on the function. It will also provide you with usage examples.
I hope this helps.

Rds File size increasing after reading [duplicate]

What are the main differences between .RData, .Rda and .Rds files?
Are there differences in compression, etc.?
When should each type be used?
How can one type be converted to another?
Rda is just a short name for RData. You can just save(), load(), attach(), etc. just like you do with RData.
Rds stores a single R object. Yet, beyond that simple explanation, there are several differences from a "standard" storage. Probably this R-manual Link to readRDS() function clarifies such distinctions sufficiently.
So, answering your questions:
The difference is not about the compression, but serialization (See this page)
Like shown in the manual page, you may wanna use it to restore a certain object with a different name, for instance.
You may readRDS() and save(), or load() and saveRDS() selectively.
In addition to #KenM's answer, another important distinction is that, when loading in a saved object, you can assign the contents of an Rds file. Not so for Rda
> x <- 1:5
> save(x, file="x.Rda")
> saveRDS(x, file="x.Rds")
> rm(x)
## ASSIGN USING readRDS
> new_x1 <- readRDS("x.Rds")
> new_x1
[1] 1 2 3 4 5
## 'ASSIGN' USING load -- note the result
> new_x2 <- load("x.Rda")
loading in to <environment: R_GlobalEnv>
> new_x2
[1] "x"
# NOTE: `load()` simply returns the name of the objects loaded. Not the values.
> x
[1] 1 2 3 4 5

Having saved a dataframe to hdfs I have an error when I try to unserialize it when reading it back in using rhdfs

I have written a dataframe into hdfs using the rhdfs library and when I try to read it back in I have errors.
The code to write the dataframe is as follows,
df.file <- hdfs.file("/mydir/df.Rdata", "w")
hdfs.write(df, df.file)
hdfs.close(df.file)
And to read it back in I use
df.file <- hdfs.file("/mydir/df.Rdata", "r")
m <- hdfs.read(df.file)
df <- unserialize(m)
hdfs.close(df.file)
But I get an error at the unserialize stage,
Error in unserialize(m) : read error
Does anyone have any idea what the cause of this error is and what I can do to prevent it. Any help would be much appreciated.
This happens when the object you unserialize is bigger than 65536 bytes
If you look at the RStudio Environment, you will see that df object is raw[1:65536] and you missed a part of the file
you should read it by pieces like this code:
http://chingchuan-chen.github.io/posts/2015/04/08/installations-of-rhdfs-rmr2-plyrmr-and-hbase

What are the main differences between R data files?

What are the main differences between .RData, .Rda and .Rds files?
Are there differences in compression, etc.?
When should each type be used?
How can one type be converted to another?
Rda is just a short name for RData. You can just save(), load(), attach(), etc. just like you do with RData.
Rds stores a single R object. Yet, beyond that simple explanation, there are several differences from a "standard" storage. Probably this R-manual Link to readRDS() function clarifies such distinctions sufficiently.
So, answering your questions:
The difference is not about the compression, but serialization (See this page)
Like shown in the manual page, you may wanna use it to restore a certain object with a different name, for instance.
You may readRDS() and save(), or load() and saveRDS() selectively.
In addition to #KenM's answer, another important distinction is that, when loading in a saved object, you can assign the contents of an Rds file. Not so for Rda
> x <- 1:5
> save(x, file="x.Rda")
> saveRDS(x, file="x.Rds")
> rm(x)
## ASSIGN USING readRDS
> new_x1 <- readRDS("x.Rds")
> new_x1
[1] 1 2 3 4 5
## 'ASSIGN' USING load -- note the result
> new_x2 <- load("x.Rda")
loading in to <environment: R_GlobalEnv>
> new_x2
[1] "x"
# NOTE: `load()` simply returns the name of the objects loaded. Not the values.
> x
[1] 1 2 3 4 5

Loading someone else's .rdata file, can't access the data

My professor has sent me an .rdata file and wants me to do some analysis on the contents. Although I'm decent with R, I've never saved my work in .rdata files, and consequently haven't ever worked with them.
When I try to load the file, it looks like it's working:
> load('/home/swansone/Desktop/anes.rdata')
> ls()
[1] "25383-0001-Data"
But I can't seem to get at the data:
> names("25383-0001-Data")
NULL
I know that there is data in the .rdata file (it's 13 MB, there's definitely a lot in there) Am I doing something wrong? I'm at a loss.
Edit:
I should note, I've also tried not using quotes:
> names(25383-0001-Data)
Error: object "Data" not found
And renaming:
> ls()[1] <- 'nes'
Error in ls()[1] <- "nes" : invalid (NULL) left side of assignment
You're going to run into a lot of issues with an object that doesn't begin with a letter or . and a letter (as mentioned in An Introduction to R).
Use backticks to access this object (the "Names and Identifiers" section of help("`") explains why this works) and assign the object to a new, syntactically validly named object.
Data <- `25383-0001-Data`
Maybe it has to do with the unusual use of dashes in the name and backquotes work:
names(`25383-0001-Data`)
Edit:
More for reference (since Joshua already answered the main question perfectly), you can also reassign an object from ls() (what Wilduck tried in the question) using get(). This might be useful if the object of the name contains very weird characters:
foo <- 1:5
bar <- get(ls()[1])
bar
[1] 1 2 3 4 5
This of course requires the index of foo in ls() to be [1], but looking up the index of the required object is not too hard.

Resources