Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

Questions tagged [r]

R is a free, open source programming language and software environment for statistical computing and graphics.

Filter by
Sorted by
Tagged with
2 votes
1 answer
23 views

R: Function to return the category that has the maximum in another variable [closed]

Is there a nice simple function to return which diet has the highest average? I can calculate the means from each diet using: ...
user avatar
  • 21
1 vote
0 answers
34 views

Extract, transform, and load Excel files

I am curious to know if there is a cleaner/better way to write this. Any suggestions would be greatly appreciated! I do ask some questions in comments within the code specific to certain functions/...
user avatar
  • 11
1 vote
1 answer
35 views

R code to calculate coef

Can you help me think of some way to reduce the computational time of a code that generates a certain value, which in this case I call coef, which will depend on <...
user avatar
  • 121
0 votes
0 answers
32 views

Adjust code that uses data.table function

The idea of ​​this code below is to find the coef value for each date/category. To find the value of ...
user avatar
  • 121
1 vote
0 answers
26 views

Another solution approach to find if trailing zeros exist for each row id

The code below removes trailing zeros exist for each row id. This question has been resolved here https://stackoverflow.com/questions/69452882/delete-column-depending-on-the-date-and-code-you-choose. ...
user avatar
  • 121
0 votes
0 answers
35 views

calculating all prime numbers up to a given number

This program calculates all prime numbers up to n. However it runs pretty slowly. I was wondering if there was any optimisations I could make. ...
user avatar
-1 votes
1 answer
39 views

Mapping variables in two data frames using a third data frame

I have two data frames, df1 and df2 of identical structure. The first three columns, id, <...
user avatar
  • 144
2 votes
1 answer
32 views

Create a folder with subfolders and files from a R dataframe

I have such a dataframe: ...
user avatar
0 votes
1 answer
49 views

A function that modifies the column types of a `data.frame`, given a character vector of "format codes"

Problem A business process requires me to read two different text files (raw and processed) into R as data.frames, with each column being read as a character type. ...
user avatar
1 vote
1 answer
54 views

How can I simplify the code using just the tidyverse?

So I have the data frame below, and I'm trying to creat a new Data frame which show me the name of the department which have the highest and the lowest employee turnover. The code that I write is ...
user avatar
0 votes
1 answer
41 views

Faster way to print Number of Descents in a permutation in R

I am trying to find a more efficient way to print the number of descents in a permutation using R as my programming language. This code does it for the numbers 3 to 10: ...
user avatar
2 votes
0 answers
25 views

R language optimize converting mpfr to gmp

I wrote a function to convert, without loss of precision, a mpfr - class value to a gmp bigq value. (This particular operation ...
user avatar
1 vote
1 answer
51 views

Conditionally drop rows in grouped data.frame using dplyr in R

I have a data frame with columns for three predictors that try to predict the target variable 'change' with values 'yes' and 'no'. The data frame is grouped by id, and for every id, I have 4 years. ...
user avatar
  • 113
0 votes
0 answers
73 views

How can I make this code more efficient

i try to get the row count for last three column (Kindly refer the below image ).I have done using for loop but the concern is it takes so much of time to execute in my application, and i heard apply ...
user avatar
2 votes
2 answers
49 views

Assign 0 or 1 with different probabilities conditional on another column in R

I am trying to assign a 0 or 1 with some stochasticity based on another column in a data frame (outcome). If outcome == 1, the ...
user avatar
  • 144
1 vote
0 answers
55 views

Estimating risk differences for marginal models

I need to estimate risk differences for marginal models. I have worked on the Probratio function to create an equivalent function for RDs that works. Please provide feedback on accuracy or comments to ...
user avatar
  • 11
0 votes
0 answers
28 views

Printing file archives using R and libarchive

This is my first formal R script that reads an archive and prints its contents. It uses libarchive R-bindings, which can be ...
user avatar
  • 2,897
0 votes
0 answers
106 views

R ggplotly with hovermode = "x unified"

I managed to get this ggplotly graphic: It uses the layout option hovermode = "x unified" (see https://plotly.com/r/...
user avatar
1 vote
0 answers
44 views

Combine several calculations on several variables

This is my very first time here, I am coming from stackoverflow.com thanks to @r2evans. So I apologize in advance if I don't follow all the rules. I wrote a code in ...
user avatar
  • 111
0 votes
1 answer
48 views

Increase speed of lapply function identifying the sum of all existing combinations

I have a dataframe of cases that score 0-1 on a group of binary attributes. What I want to do is extract all possible ombinations of attribute triplets (e.g. A/B/C, A/B/D... out of A-E) and then sum ...
user avatar
  • 163
2 votes
0 answers
25 views

Transform dataframe to a certain JSON format

I need to go from this dataframe: ...
user avatar
2 votes
1 answer
51 views

Simulating disease spread in a population

I am trying to build a model that allows me to estimate the spread of disease in the population. The setup is similar to the SIR model, with the difference that this model includes the stochastic ...
user avatar
  • 21
0 votes
1 answer
57 views

An efficient way for removing rows with zero elements iteratively while constructing the matrix

Based on a previous post, Accelerating creation of matrices and finding ways for optimal scaling, we managed to accelerate the way that I construct a matrix in Rcpp (inputs for example are at the end ...
user avatar
0 votes
0 answers
47 views

R code that creates stellar interior light curves

Here is a minimal example of my task... I have four 2-column files. profile1.data ...
user avatar
  • 199
0 votes
1 answer
50 views

R code for reading tabular data files and plotting light curves of modeled stars

Here is a minimal example of my task... I have four 2-column files. profile1.data ...
user avatar
  • 199
1 vote
1 answer
38 views

R code for reading tabular data files and plotting light curves of modeled stars

I am given a file named profile1.data that looks like this: ...
user avatar
  • 199
0 votes
0 answers
65 views

R Count rowwise missing values for specific columns - dplyr cleanest version

I want to count the missing values for each case in different sets of variables in a bigger dataset. Lets say in the mtcars between the variable disp and the ...
user avatar
  • 13
1 vote
1 answer
66 views

Accelerating creation of matrices and finding ways for optimal scaling

I have the following specific task, for which I have written code, in Rcpp, but it doesn't scale well and I would like to improve it. I'll describe first the steps of the procedure Step 1: We have ...
user avatar
1 vote
2 answers
60 views

R coding: creating a name variable to use with select

I am wondering if this is correct: ...
user avatar
0 votes
1 answer
55 views

R code optimization

I have about 977 obs in top500Stocks which contains name of 977 stocks. ...
user avatar
2 votes
1 answer
146 views

Find if a dataset of words contains any words containing all vowels by using exactly 5 calls to str_detect

Take the words dataset from stringr. An R4DS exercise challenges the reader to check if there are any words in that dataset that ...
user avatar
  • 61
1 vote
0 answers
32 views

using R probability

I am working on the subject of the French championship 2019, composed of 20 teams, and each team plays 38 times, if team A plays with team B, whatever the result, each team plays another time with ...
user avatar
2 votes
0 answers
65 views

Rcpp sparse CSC matrix class

This is a sparse matrix (dgCMatrix) class that extends Rcpp. WHAT: This class includes Rcpp::NumericVector and ...
user avatar
  • 121
1 vote
1 answer
40 views

R Container for Multiple data.frame with a Brief Description of the Content of the data.frame

For a project I have a large dataset with a multitude of variables from different questionnaires. Not all variables are required for all analyses. So I created a preprocessing script, in which subsets ...
user avatar
  • 13
0 votes
2 answers
77 views

Algorithm that creates in a systematic form, matrix with zero-one elements

I'm working on Rcpp and I wrote the following code ...
user avatar
1 vote
0 answers
39 views

Reimplemented R function(s) is surprisingly slow

I reimplemented the functions https://rdrr.io/cran/fclust/man/RI.F.html, https://rdrr.io/cran/fclust/man/ARI.F.html and https://rdrr.io/cran/fclust/man/JACCARD.F.html of the R package https://rdrr.io/...
user avatar
  • 161
0 votes
1 answer
50 views

Speed up a function that checks for a sequence

I created a function yes.seq that takes two arguments, a pattern pat and data dat, the ...
user avatar
  • 103
1 vote
0 answers
73 views

Search rows in dataframe corresponding to a value in R - Performance & Scalability

Overview of the Question I would like to ask for performance/scalability feedback on an R function that I recently wrote. I have a large 4-column dataframe (the <...
user avatar
2 votes
1 answer
61 views

Calculating edges of a social network

I am working on an open Chess data-set (~15500 rows after cleaning), and I create nodes and edges. But the way I create the edges takes a bit of time. A sample of my nodes tibble: player 1 bougris ...
user avatar
  • 123
3 votes
0 answers
89 views

Was this an idiomatic and prudent way to extend R's matrix multiplication syntax? Should I have used R's object system?

After reading one too many Lisp books, I decided to try extending R's syntax. My goal was to implement repeated matrix multiplication in a way such that I could write ...
user avatar
  • 61
1 vote
0 answers
197 views

Dplyr friendly function for computing moving average

Background The function computes moving arrange and offers some other minor widgets like possibility to return data frame in original order or automatically derive variable name for the moving average....
user avatar
  • 241
2 votes
1 answer
46 views

How can I strengthen this function? I want it to give me an estimated date of hearing back from grad school applications

I created a function to give me an estimated date of hearing back from grad school applications using the median length of days between each theGradCafe submission and January 1st. I separated ...
user avatar
  • 23
2 votes
0 answers
55 views

How to make my code with for/if loop more efficient in R

My code is structured so that I create an ellipse through my first three points of data. Then I go through each next row, and if the point falls outside the ellipse, I record the minimum distance ...
user avatar
  • 21
2 votes
0 answers
27 views

Implemented my own version of every() from {purrr}

I am new to programming / R and new to writing functions. I am working through the book ...
user avatar
3 votes
1 answer
59 views

Construct list from a dataframe and a formula

A dataframe and a right-sided formula are given: ...
user avatar
2 votes
1 answer
68 views

Unefficent data wrangling in R

so I've been working with R quite a while now, but due to very stressful deadlines in my last university module I had, I wrote code that I'm not very proud of. I would like to be more efficient in ...
user avatar
  • 55
2 votes
1 answer
37 views

Writing an excel doc with a subset of my data in R. Currently the code is excessively long

I'm fairly new to R. I have used the 'r2excel' package to create an excel workbook from a subset of my R data. This should have 3 sheets for the three different subsets. What I have written works fine,...
user avatar
  • 131
1 vote
0 answers
24 views

normal distribution line does not show up when plotting [closed]

I am plotting a distribution of 10,000 samples of sum of 100 poisson variables, and overlaying that distribution of the approximate normal distribution. However, the normal distribution line does not ...
user avatar
  • 111
3 votes
1 answer
65 views

Does this R code look ok to expand out to other sites?

I'm after some constructive criticism on my code and maybe some ways to simplify before I expand it to include other sites. I am new to R and coding and have been working on this problem for weeks now....
user avatar
  • 31
1 vote
1 answer
35 views

transform columns to binary encoded columns in R

I have some nominal variables encoded as integers (not ordinal), which I would like to encode as binary (not dummies nor one hot!). The following code is what I came up with (adapted from other code I ...
user avatar
  • 631

1
2 3 4 5
8