Most voted "r" questions
R is an open-source environment and programming language for statistical computing, bioinformatics, and graphics. It is advisable to supplement your question with a minimum reproducible example.
Learn more…2,359 questions
Sort by count of
-
6
votes1
answer673
viewsHow to correctly identify clusters using kmeans?
Suppose I wish to classify the specimens of the iris dataset using the k-Means method. Also, I want to assess whether the rating was good or not. The easiest way to do this is as follows:…
-
6
votes2
answers1588
viewsHow do I place the inverted (decreasing) Y-axis in the R?
I’m trying to present some graphs on uniformity coefficient, but normally in this type of graph, the Y-axis is organized in descending order, from 100 to 0. I would like to know how do I perform…
-
6
votes2
answers242
viewsGet only given last month using R
I need to extract the last monthly values from dataframe column 2 ntnb45. Is there any direct way to do this in R? library(GetTDData) ntnb <- download.TD.data('NTN-B') ntnb45 <-…
-
6
votes2
answers272
viewsIn R, what is the best way to select sets of internal lists within a list of lists?
I have a list of lists like the one below: lista <- list(num = list(1:10, 11:20, 21:30), chr = list(letters[1:13], letters[14:26], LETTERS[1:13])) I’d like to turn it into a data.frame, but for…
-
6
votes1
answer289
viewsArgument of a function is another function
I would like to know how to put as an argument of a function another function. For example, let’s say I want to solve an integral by a given numerical method of approximation of an integral. And I…
-
6
votes1
answer347
viewsSplit base with "for" in R
As it is my first for in the R I found it difficult to apply this function. I have a base with a base date of different years and would like to divide the base by base dates. The variable "date" has…
-
6
votes2
answers938
viewsHow to take the Hosmer-Lemeshow test on R?
I have a data set to make the logistic regression of the dependent variable childbirth which is binary qualitative. With the following command I obtain the multivariate logistic model in the program…
-
6
votes1
answer708
viewsIn R, when does a vector become "Too long"?
When trying to create an infinite vector (1:Inf) I received the following message: Error in 1:Inf : result would be Too long a vector However, when the vector does not know in memory the message is…
-
6
votes1
answer1654
viewsText position on pie chart in ggplot2
I have the following table Var1 Freq Percentual Até 50 anos 4657 2.98% De 51 a 60 anos 32009 20.47% De 61 a 70 anos 57368 36.68% De 71 a 80 anos 46075 29.46% Mais de 80 anos 16299 10.42% And I need…
-
6
votes2
answers396
viewsWhy are loops slow in R? How to avoid them?
It is very common to hear (or read) that loops are not efficient in Rand should be avoided (at this link or another link or even in this). And proving this statement is simple: numeros <-…
-
6
votes1
answer2388
viewsMerge data frames
I have two data frame (see below) with information on consumption and gain of animals. Consumption was collected each day and gain in larger intervals. I want to join one next to the other, having a…
rasked 7 years ago Luciano Hauschild 431 -
6
votes1
answer230
viewsIn R, a function that reads only a few columns of a dataframe in Rda format
I have 27 txt files. Each file represents a state of brazil. In fact, each file is dataframe on labor market. The 27 files can add up to more than 20 gigs. The first thing I did to reduce this was…
-
6
votes1
answer35
viewsNumber of queries in a dataframe
I have a data frame where in each column is the value of events in the period, I need to calculate the increment and the decrease of these periods for a larger basis and I could not do: p1<-…
-
6
votes4
answers7118
viewsChanging the name of a variable in a dataframe R
I have a dataframe with 34846 observations and 15 variables, I would like to know how to change the name of a variable. Example: I have the variable "Country" and I would like to rename it "Country"…
-
6
votes3
answers1376
viewsFill column of a data frame with data from another data frame in R
I have the following df (data1): ITEM CLASSIFICACAO 123 AZUL 456 AMARELO 789 234 VERDE 345 PRETO 456 567 678 ROSA I need to fill in the blank lines of the column CLASSIFICACAO using another data…
-
6
votes2
answers354
viewsGenerate sequence in R
my logic is failing. But you could help me create a number array in R with this rule here? Vetor=1,222,2,223,3,224,4,.......,221,442 Thank you very much.
-
6
votes1
answer320
viewsRecursion in R error
I have the following recursive function: tamanho <- function(v){ if (is.null(v)){ return(0) } i <- tamanho(v[-1])+1 return(i) } I’m using Rstudio, and when I call the function with this…
-
6
votes1
answer2565
viewsExtract information from lattes
Introducing Since 1999, Brazilian researchers have had a website where they can post information about their academic career. This information is known as Currículos Lattes. I wish to download a few…
-
6
votes1
answer91
viewsHow to solve the 53 categories limit of R randomForest?
In R, using the library randomForest, when executed randomForest() I receive the following error message: Error in randomForest.default(m, y, ...) : Can not handle categorical predictors with more…
-
6
votes2
answers823
viewsWhat is the difference between [] and [[]] in the R?
I just used the [ ] to define the position of an element in a vector or matrix ([ ], [,], [, ,]...), but how does the [[ ]]?
rasked 5 years, 11 months ago Márcio Mocellin 1,443 -
6
votes1
answer360
viewsWhat is the use of the functions with underline (_) at the end?
Consider the functions of the following Packages: dplyr library(dplyr) gorup_by_ summarise_ mutate_ transmute_ tidyr library(tidyr) gather_ spread_ separate_ unite_ What is the usefulness of these…
-
6
votes2
answers639
viewsHow to split the dataframes of a list based on a group variable, common in all of them?
I have a list with n dataframes. They have a common variable, called group. I want to analyze the dataframes only with the groups a and c of group. My goal: to return, within the list, these…
-
6
votes1
answer499
viewsR - download CVM data via POST (package httr) method (new - continue)
Good afternoon guys!! I would like to have asked this question there in this post, which I reference in the title, however it was not possible. What happens is this: I am using, for the same…
-
6
votes3
answers158
viewsMinor Date in a Dataset
I have a data set and would like to select only the smallest date among each primary key (column MATRICULA). Follow the example of my DF: MATRICULA <- c(1,1,3,3,3,4,5,5,5,5,6) DATA <-…
rasked 5 years, 11 months ago Bruno Avila 867 -
6
votes1
answer37
viewsHow to optimize the removal of lines in an array?
I have a matrix of dimensions: > dim(filtro1) [1] 2806519 31 I need to remove the lines of this matrix that meet a condition. So far so good. However, computationally the loop for this has been…
-
6
votes3
answers155
viewsError with variable naming functions in a list
I try to run a function to rename variables from data.frames of a list and get only error messages. When I run the function for a database the analysis takes place: library(tidyverse)…
-
6
votes1
answer100
viewsWhy is "vector" considered a "list" in some cases?
Consider the objects: for(i in 1:6){ names<-paste0("var",i) assign(names,runif(30,20,100)) } dataset<-do.call( cbind.data.frame, mget(ls(pattern='*v')) ) cluster<-kmeans(dataset,centers=3)…
-
6
votes2
answers73
viewsEstimate variable of difficult isolation
Good afternoon collaborators. I am having difficulty estimating a parameter via R programming to complete my processing routine. The equation I am using is the one described below, where I have all…
rasked 5 years, 9 months ago Henrique Faria de Oliveira 725 -
6
votes7
answers1523
viewsHow to calculate the median of a line in a date.frame in R?
I have a database and my goal is to perform some behavior analysis of classes per line. Example: print(DADOS) Linha A B C D E L1 4 3 2 2 4 L2 1 11 1 1 1 L3 0 1 2 3 4 L4 2 0 0 8 0 Using the example…
-
6
votes2
answers135
viewsDifference between metacharacters . * and +
Consider this set of strings: my_names <- c('onda', 'ondas', 'ondass', 'ondassssssss', 'ond', 'on') Using the R language, I checked the metacharacters .* and + bring the same information:…
-
5
votes1
answer3442
viewsHow to plot the estimated logistic regression model
Suppose I have the data below, apply a logistic regression to them and want to visualize the estimated function that returns the probability. #Simula os dados nobs<-100 beta0=-10 beta1=0.006…
-
5
votes3
answers530
viewsHow to select all data.frame variables at once for a regression?
Suppose the following data.frame: set.seed(1) dados <- data.frame(y=rnorm(100), x1=rnorm(100), x2=rnorm(100), x3=rnorm(100), x4=rnorm(100)) If I want to run a y regression against x1...xn, I can…
-
5
votes1
answer363
viewsContinue running the loop even if a passage gives problem
I’m downloading Bovespa’s stock data for the package quantmod. However, I still do not know why, in the data from Santander (SANB11) the function getSymbols package is giving problem and loop for…
-
5
votes1
answer648
viewsHow to read a table missing or poorly configured elements?
Suppose a text table like the following: texto <- "a b c e f g h i" When I use the read.table command, the following error occurs:: tabela <- read.table(text=texto) Error in scan(file, what,…
-
5
votes3
answers693
viewsHow to know the amount of NA in each variable?
Suppose I’m working with the following database: df=data.frame(v=c(1,2,NA,4,NA,6,7,8,9,10),v2=c(11,NA,NA,14,NA,16,NA,NA,19,NA), v3=c(21,22,23,24,25,26,27,28,29,30), v4=c("a","b","c", NA,…
-
5
votes1
answer10722
viewsHow to plot a line chart with different colors depending on the value?
Suppose the following data: set.seed(1) y<-rnorm(101) x<-seq(from=0, to=100,by=1) I want to make a Plot with a line that has different color for negative values. To make a chart of points just…
-
5
votes1
answer497
viewsHow to remove unused categories (levels) in the database
Suppose I have the following database: df <- data.frame(categorias=c("A","B","C","D","E"), valores=seq(1:5)) When I do a subset of that data frame the categories I removed continue. subdf <-…
-
5
votes1
answer322
viewsCreating a matrix with variables with different correlations in R?
I need to generate data series that have correlations defined using R. I used a method I found here in the OS (How to generate correlated variables in R?) and I was able to create the variables with…
-
5
votes2
answers178
viewsCollapsing texts in a single line in a database
Suppose I have the following data tabela<-structure(list(nome = structure(c(2L, 9L, 6L, 1L, 8L, 3L, 4L, 5L, 7L, 10L, 11L), .Label = c("12 Anos de Escravidão", "A Caça", "Ela", "Gravidade", "O…
-
5
votes1
answer156
viewsEWMA volatility in a date.frame
I have a date.frame base_retorno_diario, of 3560 observations on 110 shares (daily returns), I want to create another data.frame from that with Volatility ewma with decay_factor = 0.97 Example of…
-
5
votes2
answers14389
viewsHow to filter a data frame?
I have a 5597 rows and 7 columns data frame. I would like to filter the results of that data frame so that only the lines in which the second column is written "AC" appear. I tried to use the…
-
5
votes2
answers604
viewsTransforming place into zip code
Does anyone know a script on R, or could help me assemble one, take the name of a location and look for the zip code of this?
-
5
votes3
answers8030
viewsHow to include lines in a data.frame?
Consider the following data.frame: df <- data.frame(x=c("a","b"), y=c(1,2)) How to include a new line with x="c" and y=3 in data.frame?…
rasked 10 years, 2 months ago Carlos Cinelli 16,826 -
5
votes1
answer774
viewsCreating a bar chart within another bar chart
I look for a very efficient and useful way to use the nested bar charts. A mode where you can demonstrate divisions and subdivisions in the same graph. How to generate such a graph in R?
-
5
votes4
answers8289
viewsHow to put different graphics of ggplot2, separately but on the same screen?
Suppose I have these 4 charts: p1 <- ggplot(mtcars, aes(mpg, cyl)) + geom_point() p2 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line() p3 <- ggplot(mtcars, aes(mpg, cyl)) +…
-
5
votes1
answer460
viewsWeb scraping for scientific papers collection at Sciencedirect
I am trying to use R to select articles from the Sciencedirect page using Keywords. I was able to extract pdfs from a page last week, using the source information of the page. The code used was the…
-
5
votes1
answer189
viewsProblem with missings in variable weight in complex sampling
The presence of missings in the variable weight of a complex sampling is preventing the R from calculating the mean. Initially I tried the na.rm = TRUE, but it did not work, follows excerpt:…
rasked 9 years, 10 months ago Henrique Pizarro 659 -
5
votes1
answer77
viewsIntegration by the Simpson method into the R
I have the electric field data in a matrix, from this data I need to use the method of Simpson in this And. At MATLAB, I already get. Does anyone know any package or built in R function that does…
-
5
votes1
answer259
viewsSelect first lines depending on group efficiently
Suppose I have the following database set.seed(100) base <- expand.grid(grupo = c("a", "b", "c", "d"), score = runif(100)) And that I want to select the lines with smaller score depending on the…
-
5
votes1
answer995
viewsIn R, create a function to change some levels of a variable
I want to create a function that facilitates my life when treating some variables I want a function that receives as input a database, a column or variable u of that database, a vector c specifying…