R equivalent to matrix row insertion in Matlab - r

In Matlab, without any coding, I can create a matrix, open up its spreadsheet, and copy multiple columns of values from Excel and paste them into the spreadsheet. I can then right click this matrix and plot it instantly.
I've tried googling for how to do the equivalent in R, and everything seems to involve creating a function iterating over each value with a for loop. This seems a bit cumbersome, is there an equivalent simple way to do this in RStudio?
Thanks.

You certainly can have a similar functionality by using R's integration with a clipboard. In particular, standard R functions that provide support for clipboard operations include connection functions (base package), such as file(), url(), pipe() and others, clipboard text transfer functions (utils package), such as readClipboard(), writeClipboard(), as well as data import functions (base package), which use connection argument, such as scan() or read.table().
This functionality differs from platform to platform. In particular, for Windows platform, you need to use connection name clipboard, for Mac platform (OS X) - you can use pipe("pbpaste") (see this StackOverflow discussion for more details and alternative methods). It appears that Kmisc package offers a platform-independent approach to that functionality, however, I haven't used it so far, so, can't really confirm that it works as expected. See this discussion for details.
The following code is a simplest example of how you would use the above-mentioned functionality:
read.table("clipboard", sep="\t", header=header, ...)
An explanation and further examples are available in this blog post. As far as plotting the imported data goes, RStudio not only allows you to use standard R approaches, but also adds an element of interactivity via its bundled manipulate package. See this post for more details and examples.

Related

Write custom metadata to Parquet file in Julia

I am currently storing the output (a Julia Dataframe) of my Julia simulation in a Parquet file using Parquet.jl. I would also like to save some of the simulation parameters (eg. a list of (byte-)strings) to that same output file.
Preferably, these parameters are different for each column as each column is the result of different starting conditions of my code. However, I could also work with a global parameter list and then untangle it afterwards by indexing.
I have found a solution for Python using pyarrow
https://mungingdata.com/pyarrow/arbitrary-metadata-parquet-table/.
Do you know a way how to do it in Julia?
It's not quite done yet, and it's not registered, but my rewrite of the Julia parquet package, Parquet2.jl does support both custom file metadata and individual column metadata (the keyword arguments metadata and column_metadata in Parquet2.writefile.
I haven't gotten to documentation for writing yet, but if you are feeling adventurous you can give it a shot. I do expect to finish up this package and register it within the next couple of weeks. I don't have unit tests in for writing yet, so of course, if you try it and have problems, please open an issue.
It's probably also worth mentioning that the main use case I recommend for parquet is if you must have parquet for compatibility reasons. Most of the time, Julia users are probably better off with Arrow.jl as the format has a number of advantages over parquet for most use cases, please see my FAQ answer on this. Of course, the reason I undertook writing the package is because parquet is arguably the only ubiquitous binary format in "big data world" so a robust writer is desperately needed.

How to create and use an R function in SAS 9.4?

I have defined some R functions in R studio which has some complicated scripts and a lot of readlines. I can run them successfully in R studio. Is there any way, like macros to transfer these user-defined functions to SAS 9.4 to use? I am not pretty familiar with SAS programming so it is better just copy the R functions into SAS and use it directly. I am trying to figure out how to do the transformation. Thank you!
You can't natively run R code in SAS, and you probably wouldn't want to. R and SAS are entirely different concepts, SAS being closer to a database language while R is a matrix language. Efficient R approaches are terrible in SAS, and vice versa. (Try a simple loop in R and you'll find SAS is orders of magnitude faster; but try matrix algebra in R instead).
You can call R in SAS, though. You need to be in PROC IML, SAS's matrix language (which may be a separate license from your SAS); once there, you use submit / R to submit the code to R. You need the RLANG system option to be set, and you may need some additional details set up on your SAS box to make sure it can see your R installation, and you need R 3.0+. You also need to be running SAS 9.22 or newer.
If you don't have R available through IML, you can use x or call system, if those are enabled and you have access to R through the command line. Alternately, you can run R by hand separately from SAS. Either way you would use a CSV or similar file format to transfer data back and forth.
Finally, I recommend seeing if there's a better approach in SAS for the same problem you solved in R. There usually is, and it's often quite fast.

R bindings for Mapnik?

I frequently find myself doing some analysis in R and then wanting to make a quick map. The standard plot() function does a reasonable job of quick, but I quickly find that I need to go to ggplot2 when I want to make something that looks nice or has more complex symbology requirements. Ggplot2 is great, but is sometimes cumbersome to convert a SpatialPolygonsDataFrame into the format required by Ggplot2. Ggplot2 can also be a tad slow when dealing with large maps that require specific projections.
It seems like I should be able to use Mapnik to plot spatial objects directly from R, but after exhausting my Google-fu, I cannot find any evidence of bindings. Rather than assume that such a thing doesn't exist, I thought I'd check here to see if anyone knows of an R - Mapnik binding.
The Mapnik FAQ explicitly mentions Python bindings -- as does the wiki -- with no mention of R, so I think you are correct that no (Mapnik-sponsored, at least) R bindings currently exist for Mapnik.
You might get a more satisfying (or at least more detailed) answer by asking on the Mapnik users list. They will know for certain if any projects exist to make R bindings for Mapnik, and if not, your interest may incite someone to investigate the possibility of generating bindings for R.
I would write the SpatialWotsitDataFrames to Shapefiles and then launch a Python Mapnik script. You could even use R to generate the Python script (package 'brew' is handy for making files from templates and inserting values form R).

Calling Stata Functions from R

Is it possible to call Stata functions from R?
Not directly, i.e. there is no package I am aware of that implements a bridge.
You can always call external programs using system() but that is neither elegant nor efficient. That said, you could prepare data in R, write it out, call Stata and then read the results in; see help(system).
There's now an RStata package on CRAN that bridges R and Stata.
The real problem is that Stata doesn't have an interactive interpreter you can pass arguments to.
Dirk is right; you can just go ahead and write the data to a common format
(if size is large and speed is an issue, fixed width is safe), but you can also just use .dta throughout the process, using read.dta in R and natively reading in Stata.
Also, in R you can call to the system() you can pass a do file or a string containing a bunch of Stata commands.
So, generally, trying to use Stata for this or that task may or may not be worth it, especially if an R equivalent is close by.

How read an analogue input in R? / or how to translate an existing Matlab code

I use a photoelectric cell to time an event in a response time experiment. It works well in Matlab, but I wanted to control it in R. Would any of you be able to help me translate the Matlab code to R?
global AI ch0
AI = analoginput('nidaq', 1);
AI.InputType = 'SingleEnded';
ch0 = addchannel(AI,0);
Thanks!
You could play around with the R.matlab package. This allows you to read your MAT files in R, so you could simply save the input and analyse it in R. Alternatively, you could interact directly between Matlab and R. I didn't try it out before, but it should be possible to call the matlab code from within R. The provided manual on CRAN shows you how.
http://cran.r-project.org/web/packages/R.matlab/index.html
There is a program called windmill:
http://www.windmill.co.uk/index.html
(windows only I think) that interacts with serial devices, you could probably use that to get the information from your device to a file or pipe and then have R read from there.
You could also do a search for "gps" at rseek or in the R-help archives, there have been a few times that people have posted questions and answers about reading location information directly from gps devices into R, some of those replies may work for you as well.
R does not support any kind of direct interaction with DAQ devices (at least for now). The only option you have is to write some C code to do this and bind it to R.
Write National Instruments and ask them to make an R package for their I/O devices. :)

Resources