# 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.

281,426 questions

**0**

votes

**0**answers

2 views

### Conditional search, match and replace vaues between data frames

I have two dataframes as shown below. I would like to replace text (cells) in dataframe 1 with corresponding values taken from dataframe 2 when there is a match. I have tried to give a simple example ...

**0**

votes

**0**answers

4 views

### How to keep a TABLE “here defnitely” in R markdown

The question is essentially the below, but for tables, because the "figure" solution does not work for tables.
How to hold figure position with figure caption in pdf output of knitr?

**0**

votes

**0**answers

7 views

### Any way to speed up functional programming with Rcpp?

I'm trying to figure out how to incorporate Rcpp into R effectively, and as my test case I chose the Bisection Method, a popular method of root finding. It also requires a tiny bit of functional ...

**0**

votes

**1**answer

9 views

### Pass variable as argument name in function call inside another function call in R

I can't seem to find an answer to this question.
I am writing a function. One of its arguments is a variable name (as a character string like "age"). I want to pass this to a function inside mine, ...

**0**

votes

**0**answers

19 views

### How to get dplyr::mutate() to work with variable names when called inside a function?

I am exploring data from the Pokemon API (not actually using the API, just pulling the .csv files from the github). In a file that contains the types of every Pokemon in narrow format (a Pokemon can ...

**3**

votes

**1**answer

15 views

### Even after converting my file as factor why does my output give factor(0) 30956 Levels?

I am new to R and working on the below dataset:
I have a file called zippopinc
Repex:
head(zippopinc)
Year Zip Total_Population Median_Income City State
1 1 2017 ZCTA5 00601 ...

**-1**

votes

**0**answers

15 views

### Calculate duration between entries and exit - Code optimisation

I have some data with:
An unique identifier
An action (entry or exit)
A time stamp
A building ID
and some other columns.
I am trying to calculate the time spent into a building based on the entry, ...

**2**

votes

**1**answer

18 views

### ggplot geom_point plot two categorical variables and fill in missing

Hi suppose I have the following dataframe and want to generate the plot below. I can plot this simply, however, for the missing value: s2,b1 is there a way I can add a circle with a different color? ...

**0**

votes

**0**answers

12 views

### Structuring transactional data for Sankey Diagram

There are a lot of packages for Sankey diagrams however these packages assume the data is already structured. I'm looking at a transaction dataset where I would like to pull out the first sequence of ...

**0**

votes

**1**answer

14 views

### How do I loop through column names to compute statistics?

say I have data that looks like this
rating repair model
5 0 1
4 0 0
2 1 1
5 1 0
I want to be able to find the mean of rating for every time ...

**1**

vote

**2**answers

29 views

### Default value for regex in dplyr::matches that never selects any columns

EDIT:
As divibisan notes, this question provides a range of general regex answers, which were tested on Python. I'm not sure works all of them apply in R. The highlighted answer is noted to be time-...

**0**

votes

**0**answers

7 views

### Fill gaps between dates in xts

So I have a very basic question. Let's say we have a few date gaps in a time series object, and I want to fill those gaps with an arbitrary value. For example let's say we have:
i <- c(seq.Date(...

**0**

votes

**0**answers

18 views

### Trying to make a function in R, but I keep getting the error message “unexpected string constant”

I am trying to make a function in R, where I first make a data frame that combines .csv files, then calculates the mean of a desired variable. I tested the individual "parts" and they all seem to work....

**3**

votes

**2**answers

25 views

### Why do I receive an evaluation error when I call this previously defined variable using dplyr?

I am new to R and working on a small project:
Repex:
I have a dataset called filterdascom4 which has the variables as below
> head(filterdacsom4)
Year Zip Total_Population Median_Income ...

**0**

votes

**0**answers

12 views

### How to convert h2o frame into numeric in r?

I ran an ANN code with h2o. There is no error appearing while I run the code. When I am trying to calculate the mean of the h2o output it is showing " Warning message:
In mean.default(cv) : argument ...

**1**

vote

**2**answers

21 views

### Group Rows Based On Column Value And Keep Row With Minimum Value In R

In the data set below, I want to first check which rows for the column U and D have same value. Then, for such set of rows having U and V as same value, I want to keep that row which has minimum value ...

**0**

votes

**2**answers

15 views

### Generate Sequence of Dates and Time for each ID in R

I'm trying to figure out the way of creating sequence of dates and time in this format: 2018-01-01 01:00 till 2018-03-30 01:00
for each Patient and fill the new empty value with random numbers.
My ...

**0**

votes

**0**answers

14 views

### R How to subtract Million(word) from numbers in tables

I'm trying to get net budget from other territories to budget by subtracting
https://en.wikipedia.org/wiki/List_of_Marvel_Cinematic_Universe_films#Critical_response
at box office performance
I ...

**0**

votes

**3**answers

17 views

### R - replace zero values by average of non-zero ones for fixed categories

I am given a dataset of the following form
year<-rep(c(1990:1999),each=10)
age<-rep(50:59, 10)
cat1<-rep(c("A","B","C","D","E"),each=100)
value<-rnorm(10*10*5)
value[c(3,51,100,340,441)]&...

**0**

votes

**0**answers

10 views

### Issue with Multiple Reactive Filters and Updateselectinputs - Strange Behavior

I am struggling to solve an issue with passing multiple filters in a row, and sometimes the result is not as expected. In the example below, there are 7 Deer, 2 Bears, 1 Cougar, 1 Beaver, 1 Skunk, 1 ...

**0**

votes

**1**answer

14 views

### How to fix ‘Error in FUN(X[[i]], …) : only defined on a data frame with all numeric variables”

I intend to draw a qq plot on the data, but it reminds me that qqnorm function only works on numerical data.
As the factor include A,B,C,D and their two, three and four way interaction, I have no ...

**0**

votes

**2**answers

31 views

### How to loop through subsetting indices in multiple matrices

Problem
Say I have a function that is currently not vectorized. The following is just an example :
FunctionNotVectorized = function(x,y,some_options) return(x[1]+y[1])
which has, say, 10 different ...

**0**

votes

**0**answers

8 views

### Different results between fpc::dbscan and dbscan::dbscan

enter image description hereI want to implement DBSCAN in R on some GPS coordinates. I have a distance matrix (dist_matrix) that I fed into the following functions:
dbscan::dbscan(dis_matrix, eps=50, ...

**0**

votes

**2**answers

31 views

### Split character strings in column & make new rows

I have a dataframe with 2 columns. Column 2 has genes separated by ; such as "A;B", "A;B;C;D". Number of these genes may range from 2 to many. I want to split the genes in pairs of 2 and put them into ...

**-1**

votes

**0**answers

10 views

### How can i take factors used in a pruned tree and apply these to a random forest model?

The problem i am having is that i have created some decision trees and used the minerror and 1-se rule to prune these trees. From a pruned tree i want to use the variables that were used in a random ...

**1**

vote

**1**answer

14 views

### Generating a Column of Timestamps from a Column of Numbers Returns NAs in the Middle and End of the Column

I'm having some trouble getting a column of numbers to convert to a time format. Which I would like to use as a reference for a timeseries. The column increments at intervals of 3, which represents ...

**0**

votes

**1**answer

24 views

### Julia or R: create symmetric matrix by minimum of off-diagonal elements

Here is a complex problem. I have an arbitrary square matrix in R (can be in Julia as well), for example
> set.seed(420)
> A <- matrix(runif(16),nrow = 4,byrow = T)
> A
[,1] ...

**0**

votes

**1**answer

21 views

### Flagging field from wide format

I am working with a df which takes the structure
df = data.frame(customer = c(1,2)
, destination_1 = c("c", "b")
, destination_2 = c("a", NA)
)
+-------...

**0**

votes

**1**answer

17 views

### Convert Format Of Date using /

Sample data
sasq=c(-5844, 0, 7121)
d2=as.Date(sasq, '1960-01-01')
>d2
"1944-01-01" "1960-01-01" "1979-07-01"
But I try to obtain:
"1/1944" "1/1960" "1/1970"
with just MONTH/YEAR and no ...

**0**

votes

**1**answer

19 views

### how to separate a datafarme based on specific string in column name

I have a huge data that I cannot split into two sets
df<- structure(list(name = structure(1:3, .Label = c("a", "b", "c"
), class ="factor"), X3C_AALI_01A = c(651L, 2L, 1877L), X3C_AALJ_01B = ...

**0**

votes

**0**answers

12 views

### 3D scatterplot using custom image

I am trying to use ggplot and ggimage to create a 3D scatterplot with a custom image. It works fine in 2D:
library(ggplot2)
library(ggimage)
library(rsvg)
set.seed(2017-02-21)
d <- data.frame(x = ...

**0**

votes

**0**answers

18 views

### R: Converting Rows to Column and Assigning Values to Corresponding Columns

I am new to R, and I am struggling with the following issue.
I want to convert the row values which are countries to Columns and assign the corresponding values from Column Year_2000 to it. Thank you ...

**-8**

votes

**0**answers

25 views

### How to calculate this sum

i'm trying to calculate the value of sum of any random matrix sum on picture i tried using apply,lapply but didn't work, any suggestion
![sum{sum{(Nij-((Ni*Nj)/n)^2)/)(Ni*Nj)/n))}}]
where Ni sum of ...

**0**

votes

**0**answers

13 views

### How to add reference line to the plot? Need explanation of qqPlot function of Car package

I am trying to see if the data i am working with is following the beta distribution. I have use method of moment to calculate alpha and beta
parameter. if a is close to 1 we can conclude that data ...

**1**

vote

**0**answers

19 views

### Floating TOC doesn't work in interactive RMarkdown

I have an interactive RMarkdown document. The TOC works fine with rmarkdown::render(), but doesn't work with rmarkdown::run().
Here is my test code:
---
title: "Testing shiny Rmd"
output:
...

**0**

votes

**1**answer

11 views

### How to keep levels when using factor variables in a new dataframe

I expect that this is a basic question; however, I looked all the suggested posts and searched myself and I couldn't find the answer. I just to know why if I create a new dataframe based on factor ...

**-3**

votes

**0**answers

22 views

### Finding R^2 of a regression of the residuals [on hold]

demonstrate the $R^2$ of a regression of the residuals of model1 on the original regressors must be zero. What is the code for this?

**0**

votes

**0**answers

20 views

### Iterate function across columns in data.frame

I want to apply some function from a package in R to every column in one data.frame. As for this function, the data must be structured like the following data.frame called x:
var1 group
...

**0**

votes

**0**answers

12 views

### fullrange isn't extending geom_smooth(method=“lm”) line beyond the data

I am trying to extend the lm line beyond the range of the data when plotting using geom_smooth. However, setting fullrange=TRUE doesn't seem to do the trick.
I have set the xlim beyond the data ...

**0**

votes

**0**answers

21 views

### Mapping over vectors of different lengths? - R

I have a csv file of forest fires.
Data: https://archive.ics.uci.edu/ml/machine-learning-databases/forest-fires/
I'm trying to create box plots for each variable with the x-axis being month/day.
I ...

**1**

vote

**2**answers

26 views

### I have an empty dataframe which I want to fill using another dataframe

I have an empty dataframe which is my template
temp <- data.frame(matrix(ncol=3))
colnames(temp) <- c("variable", "group", "bin")
And another dataframe which has the details of these:
info &...

**0**

votes

**0**answers

13 views

### Evaluation error of lubridate::interval objects

Assume a df like this:
df <- data.frame(id = c(rep(1:5, each = 2)),
time1 = c("2008-10-12", "2008-08-10", "2006-01-09", "2008-03-13", "2008-09-12", "2007-05-30", "2003-09-29","2003-09-29", "2003-...

**0**

votes

**0**answers

16 views

### How to fix lapply with function with 3 variable

I'm writing the code to calculate a sum but my lapply have function which consist of 3 variable
I tried all apply function and reduce function g to 2 variables:
g<-function(x,y,z){
res<-((...

**0**

votes

**0**answers

23 views

### Dataset until the first appearance of a word

Ok, guys, the idea is: I need to subset a dataframe, from row 1 to the row that a certain word appears, in this case "PInd". I couldn't find anything to help me in this problem. I tried:
dftest <- ...

**1**

vote

**1**answer

26 views

### I would like to show the total of a variable across months, for different subgroups

I would like to use ggplot2 and dplyr to create a chart that can show how our halls perform across months. So From Aug-Dec, I would like to see three bars for each of the three halls, and their totals....

**0**

votes

**0**answers

23 views

### How to predict multiple linear regression r

I am trying to predict the next 24 months based on the following data from the electricity data set in the TSA package. I made a model using linear regression based on which the month and time.
...

**0**

votes

**0**answers

9 views

### How to change the theme in Rstudio using the online editor?

I want to change or customize the Rstudio theme and all that I have found on google actually doesn't work for me.
the online editor if found is: https://tmtheme-editor.herokuapp.com/#!/editor/theme/...

**0**

votes

**0**answers

10 views

### when using Rvest, how to imputate a 'NA' to a dataframe when there is no wanted element on some of items

I am using Rvest and Rselenium to scrape the titles, number of thumbs up and picture links from http://zhihu.sogou.com/. However, I notice that seems not every title following with a picture. That ...

**0**

votes

**0**answers

9 views

### Graph based keywords extraction-error in making keyword network

for my master research I need to find keywords for users entries from product development platform, and i am using graph based method-pagerank. I came across codes that generate the initial keyword ...

**4**

votes

**1**answer

32 views

### Sorting: Put space before hyphen

I would like to sort a character vector , but have spaces be before hyphens in the sort.
For example
c("Want-#3","Want #2","I want to be first") %>% sort()
[1] "I want to be first" "Want-#3" ...