# Questions tagged [r]

R is a free, open-source programming language and software environment for statistical computing, bioinformatics, visualization, and general computing. Please provide minimal and reproducible example(s) along with the desired output. Use `dput()` for data and specify all non-base packages with `library()` calls. Do not embed pictures for data or code, use indented code blocks instead. For statistics related questions, use https://stats.stackexchange.com.

310,422
questions

**0**

votes

**2**answers

12 views

### Sumproduct by condition in a data frame in R

Consider the following data frame:
df <- data.frame(row_id = c("r1","r2","r3","r4","r1","r2","r3","r4"),
v1 = c(3,2,5,2,5,2,6,4),
v2 = c(4,3,5,3,7,4,6,7))
I want ...

**0**

votes

**0**answers

10 views

### unable to drop column in dataframe, R

I am reading an api using R and was able to retrieve the data, however, the data itself is retrieved in JSON format with what seems to be in 3 sections.
Converting the json data to a df using
...

**0**

votes

**1**answer

14 views

### RStudio - How to push to origin/master (on GitHub)?

First of all, I'm a super noob when it comes to git. I have connected my RStudio to my GitHub repo without using any git code.
The problem is my commits are being pushed to a local branch (called ...

**0**

votes

**0**answers

7 views

### Remove plot element without replotting everything else in R (Shiny)

This is not an specific question, but mostly a "looking for suggestions", since any kind of answer is welcome.
I built a huge website using pure shiny in R, but a really small thing is bothering me..
...

**0**

votes

**0**answers

12 views

### Is there a way to beep / play a sound in the motherboard at the end of a script?

In my work, I cannot use a speaker. When I run R scripts I go do something else on a different desktop. If I don't check frequently, I never know when something is finished. Is there a way to invoke a ...

**0**

votes

**0**answers

8 views

### How do I manipulate the index of a list in R

I have converted a row in my dataframe to string which has not produced a list of string characters.
My row in now my index and each index has a character setting of character(0)
example
index= ...

**0**

votes

**1**answer

11 views

### How would I get the Euclidean distance between any two 0s in a binary matrix?

I am trying to find the Euclidean distance between any two 0s in the binary matrix provided...
[,1] [,2] [,3] [,4] [,5]
[1,] 0 1 0 0 1
[2,] 1 1 0 0 1
[3,] ...

**0**

votes

**0**answers

5 views

### How can I get this function to add entries correctly for special characters that have been converted into a strange form?

I want to export Mendeley entries to excel using R, but I ran into problems because Mendeley does not correctly export special characters like and ¨¦, and the R-package I use seem to import them as ...

**0**

votes

**0**answers

13 views

### How to select values from each row of a matrix according to each number of a column and calculate the average of the selected values row by row

I have three csv files which I want to run in R.
File (1) looks like this:
enter image description here
File (2) looks like this:
enter image description here
File (3) looks like this:
enter image ...

**0**

votes

**2**answers

17 views

### Replace value in a data.frame within a function (for use with apply)

I have a data.frame that looks like this:
GROUP | YEAR | VAL
A | 2007 | 10
A | 2007 | 11
A | 2007 | NA
A | 2008 | 13
B | 2006 | NA
B | 2006 | 5
B | ...

**0**

votes

**0**answers

14 views

### R won't let me left_join two csv datasets [duplicate]

I am trying to merge 2 CSV datasets together using the lef_join command but I get an error in RStudio indication the the function doesn't exist. How do I get the function to work?
Here's the code I'...

**0**

votes

**0**answers

15 views

### How to run a function on all elements of list having multiple arguments

I have three data frames. First data frame is stock return file consisting of 240 rows and 427 column. Second stock rank file consisting of 240 rows and 427 column. Third consisting of number of ...

**1**

vote

**0**answers

11 views

### How exactly R is affected by Branch Prediction?

Some references:
This is a follow-up on this Why is processing a sorted array faster than processing an unsorted array?
The only post in r tag that I found somewhat related to branch prediction was ...

**0**

votes

**2**answers

20 views

### How to rename multiple columns with different column names and different order in several dataframes based on a dictionary in R

I am working on merging multiple datasets from different sources. The column names from each dataset (as datframes) have different names and are in different orders. I have created a dictionary that ...

**0**

votes

**2**answers

24 views

### how to collapse data by intervals?

I would like to take a data set like this:
dat <- data.frame(pos = 1:120, state = c(rep("state1", 30), rep("state2",30), rep("state3",30), rep("state1", 30)))
And collapse it into this form:
...

**0**

votes

**0**answers

19 views

### How to remove the same chunk of text from different texts in a character vector?

I have a character vector that has 231 documents (231 rows by one column). The beginning of each document has a chunk of text that I would like to remove from each of 231 documents.
Let's take an ...

**0**

votes

**0**answers

16 views

### How to create matrix of all 2^n binary sequences of length n using recursion in R?

I know I can use expand.grid for this, but I am trying to learn actual programming. My goal is to take what I have below and use a recursion to get all 2^n binary sequences of length n.
I can do this ...

**0**

votes

**0**answers

14 views

### How to annotate points which are outside the limit in ggplot2?

I'm drawing a plot in R using ggplot2 with most of points in a small range.
However there are some points that have much larger y value and if I keep them in the view, most of points would crowd ...

**0**

votes

**0**answers

7 views

### Custom icon on infoBox shinydashboard

Is there a way to use a custom icon on infoBox on shinydashboard. I am trying to use the example on hrbrmstr gist but it I can not find the place to update the infoBox function.
library(shiny)
ui &...

**0**

votes

**0**answers

14 views

### ggplot2 manually set facet_wrap colors with groupings with geom_line

I'm trying to do as in this post here:
ggplot2 manually specifying colour with geom_line
but with additional modifications to the lines in each facet. Specifically, for each facet plot I want manually ...

**0**

votes

**0**answers

9 views

### Any available algorithm for numerical feature selection in caret with r

I am using the following code (caret in r) to check the feature importance. An error of wrong model type for regression is reported. I found out that the method lvq is for categorical attributes, but ...

**0**

votes

**1**answer

40 views

### Create column names with spaces

I'm trying to create a DF with columns that contain spaces in their names, but an error is appearing. This is the DF I am trying to create:
CITY <- c('NEW YORK', 'LONDON', 'SAN FRANCISCO', 'MADRID'...

**0**

votes

**0**answers

16 views

### Add gradients and hand-drawn effects to gganimate plot

I am interested in using gganimate with gradients and hand-drawn and painty-type fill effects (see sketchy and painty and gradients here: https://semiotic.nteract.io/guides/sketchy-painty-patterns). ...

**0**

votes

**2**answers

16 views

### Adding traces to plotly chart based on the data in plotly r

I am trying to make a plotly chart for a shiny app. As of now I am doing is hardcoding by adding each trace. But I did realize that the columns in data get updated and the way I am doing that will not ...

**0**

votes

**0**answers

7 views

### Devtools HTTP403 error, not installing from Github or from local copy [duplicate]

Trying to install a package with Devtools off Github, but to no avail, tried downloading a local copy, but also not working.
> devtools::install_github('satijalab/seurat-wrappers')
Error: Failed ...

**0**

votes

**0**answers

9 views

### Is it better to go through stemming or lemmatizing when preprocessing text to apply Latent Dirichlet Allocation?

I am applying Latent Dirichlet Allocation to 230k texts in order to organize the data presented. I'm not sure if it would be better to apply stemming or lemmatizing in the preproessing tokenization ...

**1**

vote

**2**answers

23 views

### Concatenating each element of a data frame in R

I would like to concatenate each element of a data frame with the respective element of an equally sized data frame. Here's an example of two data frames I need to combine:
# A tibble: 3 x 2
X ...

**0**

votes

**1**answer

31 views

### Applying functions stored in a dataframe to another dataframe in R

I am dealing with a situation wherein I have multiple, distinct data sets with different column names, but the functions to be applied to them are similar. I thought, to reduce code duplication, I ...

**0**

votes

**0**answers

9 views

### How do I add a tibble column assigning values for groups of values from another variable?

I'm trying to create a new column in my tibble that assigns one value for a group of factor levels in another column.
For example, in a tibble where variable "time" has the values "t1", "t2", "t3", "...

**0**

votes

**0**answers

6 views

### Bookdown does not render image in output pdf

I'm trying to include an image in the example index.rmd file that comes with the bookdown-package within RStudio. (New Project -> Book Project using bookdown)
I then simply appended the following code ...

**0**

votes

**1**answer

15 views

### How to split xts data half yearly

I want to split xts data into half years. I have tried split.xts() and split(). Also tell me how i can split by rows like 1:6,6:12,12:18 etc.
library(xts)
library(zoo)
split.xts(dataframe,f=)

**0**

votes

**0**answers

8 views

### How Can I estimate structural parameter of job search model using otpim

I have difficulties to estimate the parameter of my model. It's a structural model of job search and matching. I write using optim and BFGS method. But many warnings appear like Warning messages:
1: ...

**0**

votes

**0**answers

17 views

### Import CSV with postgres geometry column as SF

I have a csv file with a postgis geometry column. I want to import the csv in to R and export the file in to a postgres database. I'm trying to make the geometry column in to the proper postgis ...

**0**

votes

**0**answers

16 views

### How can I select the data frames of a large list to get each data frame individually in R/Rstudio?

Good morning/afternoon/night
I report my situation, starting with a context:
I have a folder with 543 .csv files of hourly temperatures, in which you will find temperature data per hour and the ...

**-2**

votes

**0**answers

10 views

### R - Find Beta for stock based on sample data using CAPM model

I am new to R and am working on an assignment this semester.
vi. Find Beta for the stock based on the sample data using CAPM model.
Here is the code I have thus far:
library(quantmod)
library(...

**0**

votes

**0**answers

14 views

### My Dendrogram Looks Vertically Squished in RStudio Despite Hanging

Any and all help would be appreciated: Basically I can get my microbiome dendrogram to look like I want it except that the bottom bits where the clustering is the greatest looks VERTICALLY squished, ...

**0**

votes

**1**answer

21 views

### sort data columns in R with % symbols in it

I have a small data table with the % symbol in the column names. For brevity I am only showing part of the output.
thevars w1.2.5% w1.97.5% w2.2.5% w2.97.5% w3.2.5% w3.97.5% mw.2.5% mw.97.5%
...

**0**

votes

**0**answers

14 views

### Compute the number of predictors used for a prediction

I want to be able to compute the number of predictors used to make a prediction. For a regression model of the form y = a + bx1 + cx2 the number of predictors used to make a prediction is 2. For a ...

**0**

votes

**0**answers

26 views

### For Loop and If in R Programming

I think I have a simple question, but I can not figure out how to do it.
I just wanna plot all hist of an data input and check if all var are numerical.
plotting_histogram_data<-function(x) {
...

**2**

votes

**2**answers

30 views

### cumsum is.na with rle ignoring consectives NA's

simple problem. Lets say I have the following data:
library(tidyverse)
df <- data.frame(group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2),
variable = c(NA, "a", ...

**-1**

votes

**0**answers

14 views

### How to fix: 'Error in .f(.x[[i]], …) : object 'GDP2016' not found'?

I'm taking a course on how to analyse data using r and have run into a problem (a couple actually and they are all of the noob variety), anyways:
The goal ultimately is to calculate
the mean gdp for ...

**0**

votes

**1**answer

36 views

### Restructuring and creating a 5 x 5 summary matrix r

I have a dataset with three columns
The first column is ID
The second column is Intervention with two levels : Pre, Post
The third column is Response with 5 levels : strongly disagree, disagree, ...

**-3**

votes

**0**answers

25 views

### Merge data to nearest date time in R using tidyverse

i have data which has measurements, that are 3 minutes apart. I want to do further calculations and the 3 minutes difference dont matter really.
df <- tibble(structure(list(Abs_Druck_mbar = c(NA, ...

**1**

vote

**3**answers

39 views

### Divide rows by conditional row sums

Consider the following matrix:
m <- cbind(c("r1","r2","r3","r4","r1","r2","r3","r4"),c(3,2,5,2,5,2,6,4),c(4,3,5,3,7,4,6,7))
For each row I want to divide the row sum with their conditional row ...

**0**

votes

**0**answers

23 views

### How to write superscript in a character string in R

This is a very basic question and an already discussed topic here, but somehow none of the offered solutions seem to work for me: I simply want to write superscript in a vector of strings in brackets (...

**-1**

votes

**0**answers

29 views

### How to convert numeric value to date in R? [on hold]

I am trying to mutate a new column named 'LED'. Where ever the 'Lease.End.Date' is blank, I am trying to replace it with Distribution.Date else with Lease.End.Date. I have used the following command:
...

**0**

votes

**1**answer

21 views

### How to calculate AUC under twoClassSummary?

here is my code:
train <- data.frame(***contain label, feature group 1 and feature group 2***)
formula <- label ~ features group 1
ctrl <- trainControl(method = "repeatedcv",
...

**0**

votes

**1**answer

17 views

### How to extract the URL from the Shiny Bookmark Button and Create My Own Action Button That Can Shorten the URL?

I would like to know if it is possible to extract the URL created by the Shiny Bookmark button? I know the Bookmark button is a special action button that can create a URL documenting all the input ...

**0**

votes

**0**answers

30 views

### Greatest common divisor in R [duplicate]

I want to set lgm(),
which returns the Greatest common divisior in two integers.
I already tried like this.
i <- integer()
lgm <- function(x , y) {
if(x%%i==0 & y%%i==0) {
which.max(...

**0**

votes

**1**answer

20 views

### How to bootstrap R-squared of a mixed model?

I'm trying to obtain bootstrapped R^2 for a mixed effect model. As there is already only a workaround to obtain the conditional and marginal R^2, I tried to bootstrap these statistic based on an ...