Skip to content
Advertisement

Tag: r

python in Rmarkdown using reticulate cannot read packages

I am using R on a MacBook. I have an Rmarkdown document and I’m trying to use reticulate in order to use python within R. First I download the libraries: Next I look at an R chunk and figure out my working directory. Then I write mtcars to my desktop. Then I try to use python instead to read in

np.linalg.multi_dot for R

I’m trying to do a nested dot result is effectively x.dot(M.dot(M)): In python this loop can be reduced by: Is there something similar for R? Answer As @akrun commented, you could also use Reduce:

Sort by custom function in R

In python, I can do something like It gives me [1, 5, 99, 100, -5, -7, -100] It is sorted by positive/negative number and abs value. How can I do the same thing in R? Without splitting into positive and negative numbers? a = c(1,100,5,-5,-7,99,-100) Answer Use the order() function: Created on 2022-03-22 by the reprex package (v2.0.1) Another possibility

Why is this task faster in Python than Julia?

I ran the following code in RStudio: It reads a huge NASA CSV file, converts it to a dataframe, converts each element to string, and adds them to a vector. RStudio took 4 min and 15 seconds. So I decided to implement the same code in Julia. I ran the following in VS Code: The result was good. The Julia

Creating a series of Quarters

Let say I have a date Now starting from mydate, I want to create an array of 10 quarters e.g. 2020-Q1, 2020-Q2, … [total 10 values] Is there any direct way to achieve this? In R, this is pretty straightforward, as I can use seq() function to generate the same. Any pointer will be very helpful Answer it doesnt give

Summing up all repeated values in a dataset

I have a dataset in which in a column I have the name of a person and in another column I have the amount she was paid for a given service. I’d like to build a list with the names of all people ordained by the total amount they were paid regardless of the service they performed. Example: I figured

making list index possible for python class list

Python has no function for list index argument in __getitem()__, __setitem()__, __delitem()__. Since I have been an R user for long time, it looks quite natural to utilize list index. I know there is pandas or numpy but it is cumbersome to change the type. AND IT NEEDS IMPORTING EXTRA PACKAGE! Here is my proposal. It seems to work okay.

How to invoke a specific shell with python?

I want to invoke a specific command shell in python to execute some scripts. For exemple, in R, I use: system2(os_shell = “C:/Program Files (x86)/pgAdmin III/1.18/pg_dump.exe”, args = “very long argument”) Thanks to this code, I can backup my Postgresql’s tables with a for loop. Problem: I didn’t find an equivalent in Python. Answer If you just want to execute

Advertisement