Reverting a model.matrix in R back into a data.frame - r

I was wondering if someone knows if there's an easy and smart way to revert a matrix (that was generated by calling "model.matrix" on a data frame) back into a data.frame? The reason being that I am using the matrix in a function where the original data frame is not in the scope and I want to try something with the data frame without modifying the whole code (before I know if what I'm trying is even useful 🙂).
Thanks in advance!

Related

(r) turning data (DNAbin) into a matrix

I am trying to run stamppFst() and stamppConvert() with haplotype data. The data I have is a squence of nucleotides in a DNAbin. I have tried to find ways to turn it into a matrix but what I have read goes way over my head since this is the first time I have ever used R.
data
This is an example of one of the data sets I want to use.
I apologize if this is a very basic question. Thanks for any help!

How to create a loop with R function specgram(signal)

I am working with many signals; each one in a time series but is too many and, I need to make more than 1000 but, I am not sure how to implement it because I not only need the plots but the values of output for each spectrogram stored in a file or an R object. I am sorry I don't have an approach. Can anyone help out, please?

SparkR - Convert dataframe into Vector/list

Can anyone tell me, whether we can convert data frame to list in SparkR. I am aware that collect() function helps you do that. However, it is not advisable when we use large amount of data. In python/Scala, there is a function called local Iterator() which will convert the data frame to list. Am struggling with that in SparkR. Can anybody help!
Unfortunatelly collect() is the best method to do this. You can also try: saveAsTextFile but in that case you probably will not obtain whole data.

How to refer to a custom data frame in knitr document

I am new to knitr and would appreciate if someone could help me with a pointer on this.
Most examples I see using knitr/sweave create a data frame inside a chunk and then refers to that in the subsequent processing. However, what if I have massaged the raw into a data frame, and then want to use that. How do I do that?
I have tried saving the data frame as an R object, and then loading it inside a chunk, but I am not sure if this is the best way.

Growing matrices in R from NULL

What is the problem with initializing a matrix object to NULL and then growing it with cbind() and rbind()?
In case the number of rows and columns are not known a priori, is it not necessary to grow from NULL?
Edit: My question was prompted by the need to understand memory efficient ways of writing R code. The matrix context is more general and I'm probably looking for suggestions about efficient ways to handle other data objects as well.
Apologize for being too abstract/generic, but I did not really have a specific problem in mind.
It would be helpful if you provided more detail about what you're trying to do.
One "problem" (if there is one?) is that every time you "grow" the matrix, you will actually be recreating the entire matrix from scratch, which is a very memory inefficient. There is no such thing as inserting a value into a matrix in R.
An alternative approach would be to store each object in your local environment (with the assign() function) and then assemble your matrix at the end once you know how many objects there are (with get()).

Resources