Posts by Marcus Nunes • 17,915 points
372 posts
-
2
votes1
answer243
viewsA: Linetype and Shape in ggplot2 on R
First I will create an array with the values of x where I want the dots to be plotted. In this case, I want all dots between 0 and 2 with spacing of 0.25: x <- seq(0, 2, by=0.25) With this vector…
-
4
votes1
answer1515
viewsA: With merging several txt files into one
The code below should be adapted to your specific needs, but it should give a good starting point. Heed: R must be configured for the directory in which the . txt files are located: lista <-…
ranswered Marcus Nunes 17,915 -
3
votes3
answers94
viewsA: Find exponent of the data-fit equation, R
There is no problem with this adjustment or code. See what happens when I change the initial kick to 0.9: y=c(1.1178329,1.0871448,1.0897010,1.0759255,1.0535190,0.8725332) x=c(6,5,4,3,2,1)…
ranswered Marcus Nunes 17,915 -
3
votes2
answers1047
viewsA: Presentation of disproportionate rmarkdown chart
I arrived at the following chart: For this, I have adjusted the following parameters: Within theme, option axis.text.x, put the argument angle=90, to rotate the x-axis legend In function geom_text,…
-
1
votes2
answers61
viewsA: default values for namedtuple
Subclass the result of namedtuple and rewrites the value of __new__ as follows: from collections import namedtuple class Move(namedtuple('Point', 'x, y, z')): def __new__(nome, piece, start, to,…
-
3
votes2
answers1908
viewsA: How to convert Hour:minutes:seconds to decimal number in R
Since I don’t have access to your original dataset, I’m going to assume that the time is not in date format, but in character, like "h:m:s". I created the function HoraDecimal, that does exactly…
ranswered Marcus Nunes 17,915 -
2
votes1
answer602
viewsA: Multiple Linear Regression in R in different groups
One way to solve this problem is through the packages dplyr and broom: library(dplyr) library(broom) iris.regressao <- iris %>% group_by(Species) %>% do(regressao = lm(Sepal.Length ~…
-
7
votes3
answers243
viewsA: How to filter data according to part of the characters of a variable?
Suppose your dataset is called dados: dados <- data.frame(Nome=c("João Silva", "Pedro Souza", "Ana Silva", "Isabela Cabral", "Paulo Santos"), Nota=c(9, 8, 6, 10, 5)) Use the function grep to find…
ranswered Marcus Nunes 17,915 -
3
votes1
answer560
viewsA: How to smooth this graph in R/Rstudio?
One way to make this smoothing is to first choose a tool capable of generating the curve that will interpolate your data. My suggestion is to do a loess regression of pontos.y in pontos.x and plot…
ranswered Marcus Nunes 17,915 -
4
votes2
answers3195
viewsA: Error importing data . csv to Rstudio
The archive Melipona.csv is not located in the directory C:/Users/N1na3/Documents/R/win-library/3.4/dismo/ex/. I listed the contents of this directory on my computer and there are only two . csv…
-
7
votes2
answers658
viewsA: Obtain coefficients "a" and "b" of the Linear Regression Model in R
Using part of a reply published here in the OS a few days ago: regressao <- lm(mpg ~ cyl, data = mtcars) coef(regressao) (Intercept) cyl 37.88458 -2.87579 That is, just use the command coef the…
ranswered Marcus Nunes 17,915 -
3
votes3
answers1016
viewsA: Split a data frame and save to different directories
Two other ways to solve the problem. The first one uses the package dplyr: library(dplyr) tab01 <- tab %>% filter(Quantidade==1) tab02 <- tab %>% filter(Quantidade==2) tab03 <- tab…
ranswered Marcus Nunes 17,915 -
10
votes1
answer347
viewsA: Split base with "for" in R
Whenever possible, avoid using for in the R. It is computationally slow and can lead to making silly mistakes. For example, make a for starting like this for(i in 199501:201703) will take you to…
-
5
votes2
answers74
viewsQ: List the amount of space occupied by a file type in the terminal
I suspect that the Pcs I use have many files .RData, used by the R program to save data sets. I want to do a cleaning in these files, but without going into directory by directory, on computer by…
-
4
votes1
answer2932
viewsA: Calculate covariance matrix in R
The command cov calculates the matrix of covariances between vectors: m <- structure(c(0.768452329393413, 0.520393273867425, -2.09890502749191, -0.654528466570541, 0.919830179164542,…
-
7
votes1
answer1670
viewsA: Bar graph sorted using dplyr and ggplot2
I had an idea of how to research my question and I ended up getting an answer a few moments after publishing my question: dados %>% group_by(categorias) %>% count() %>% ggplot(.,…
-
8
votes1
answer1670
viewsQ: Bar graph sorted using dplyr and ggplot2
I would like to create a bar chart after counting the number of occurrences of the categories of a data set. Suppose my dataset is this below: dados <- structure(list(categorias = structure(c(5L,…
-
4
votes2
answers158
viewsA: Play a chart in R
Using ggplot2: library(ggplot2) dados <- data.frame(x=x, y_original=compl$y, y_trunc=sample$y, OLS=pred.OLS, trunc=pred.trunc) ggplot(dados, aes(x=x)) + geom_point(aes(y=y_original), shape = 21,…
-
3
votes1
answer4316
viewsA: Error: non-numerical argument for binary operator
Note that the result of funcao is not numerical: funcao(10) 1.000333 with absolute error < 1.1e-14 It is necessary to extract the numerical value (1.000333) of this result. One way to do this is…
-
1
votes2
answers1095
viewsA: Add a new value at the beginning of a pandas series
Using the created series in this answer: import pandas as pd serie = pd.Series() for n in range(5): serie = serie.set_value(n, n*n) serie 0 0 1 1 2 4 3 9 4 16 dtype: int64 serie2 = pd.Series(100)…
-
4
votes2
answers726
viewsA: Create sequential counter
No need to create a loop. You can solve this problem using the package dplyr: dados <- structure(list(x = structure(c(1L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 2L), .Label = c("A", "B", "C"), class =…
-
1
votes2
answers62
viewsA: Merge two series (zoo) of the same variable making the intersection and filling
I solved it with a little gambit. I imagine there must be some simpler way, but this below is working as it should for at least this example. First, I created the data frames a and b, as in the…
ranswered Marcus Nunes 17,915 -
4
votes2
answers4518
viewsA: Select multiple lines of a data.frame from the highest R values
Using the package dplyr: library(dplyr) df %>% top_n(x, n=5) x y 1 10 5 2 10 4 3 8 3 4 8 2 5 8 4 Using order, one of the standard functions of R: df[order(df$x, decreasing=TRUE), ][1:5, ] x y 1…
-
8
votes1
answer748
viewsQ: filter in dplyr using a categorical variable
Suppose I have the following data set: set.seed(12) dados <- data.frame(grupos=rep(letters[1:5], 5), valores=rnorm(25)) head(dados) grupos valores 1 a -1.8323176 2 b -0.0560389 3 c 0.6692396 4 d…
-
4
votes1
answer1070
viewsA: Number of items to replace is not a multiple of the length of the substitute
In doing matriz_distancia[i,j] <- (abs(xteste[i,] - xtreinamento[j,])^r)^(1/r) you are trying to put a vector, which is the result of the formula (abs(xteste[i,] - xtreinamento[j,])^r)^(1/r)…
ranswered Marcus Nunes 17,915 -
4
votes1
answer210
viewsA: Plot grid histograms with fixed Y-axis - R
See if the codes below help you. I created three samples x, y and z, each with different normal distributions, and plotted one next to the other. Note that I first created x and y, just to add z.…
-
7
votes1
answer4378
viewsA: How to delete repeated values in a text column in R?
Suppose your data frame is called dados and the column with these names is called nomes, do unique(dados$nomes) If you want to know the number of times each name appears, do table(dados$nomes)…
ranswered Marcus Nunes 17,915 -
4
votes2
answers206
viewsQ: Scatter charts fixing a response variable
Suppose I have an interest in the dataset iris, already present in the memory of R: head(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2…
-
3
votes1
answer145
viewsA: Strange result on to.Monthly (quantmod package) cut series
Note that from 2013-06-27, the column ibovespa$IBOV.Volume has only NA. The function to.monthly cannot, as far as I know, deal with this lack of information. Note that if I switch assets, this error…
ranswered Marcus Nunes 17,915 -
23
votes2
answers6460
viewsQ: What is the difference between a programmer and a software engineer?
I am aware that the site already has the questions What’s the difference between architecture and software engineering? and What is the difference between architecture, engineering, science,…
-
4
votes1
answer1958
viewsA: Are there any R functions similar to excel PROCV?
I don’t have the original datasets or Excel installed to test the function PROCV, but I think the code below solved the problem. The function procura calculate the difference, in absolute value,…
ranswered Marcus Nunes 17,915 -
1
votes2
answers490
viewsA: Modify t test for linear regression parameters in R
I don’t know any function capable of doing this the R. In general, what is suggested is to do likelihood ratio tests, which are much more general and solve more sophisticated problems, even in…
-
1
votes1
answer562
viewsA: A: singular gradient Matrix at initial Parameter estimates
The mistake Error in nlsModel(formula, mf, start, wts) : singular gradient matrix at initial parameter estimates means that the gradient of the search for the best estimates for your equation is…
-
3
votes1
answer60
viewsA: GEE on R-someone can explain to me the SFV code
The objects model0 and model1 are generalized linear model (MLG) adjustments to set data ctsib. The argument binomial assumes that the response variable in this case is binary, success and failure.…
ranswered Marcus Nunes 17,915 -
1
votes1
answer400
viewsA: In R: How to group the results of a loop into a rbind function?
I generated some random data because I don’t have access to the originals. See if the command paste(unlist(b[2:length(b)]), collapse=", "), at the end of the code, help yourself. page <-…
-
4
votes2
answers85
viewsA: Decrease default error in Fitting (Std Error) R
The response of Erikson K. is very good. He raised an excellent point: the form of the function you are trying to adjust to the data is not good. I’ll try to expand a little what he did by…
ranswered Marcus Nunes 17,915 -
2
votes1
answer3892
viewsA: Bar graph with relative and cumulative frequency
This is solved with the package ggplot2. First of all, I build a data frame with everything that needs to be plotted, with names that have some meaning in this context: dados <- c(1L, 5L, 3L, 3L,…
-
5
votes2
answers3756
viewsA: Chart of average profiles (including error bars)
I recommend using the packages ggplot2 and Rmisc to make this graph. The first package makes the graph itself, while the second prepares the data for analysis. Below I will explain step by step how…
-
1
votes2
answers5601
viewsA: How to calculate the average of a column in Rstudio but ignore the 0 values in the column?
Assuming the dataset is called dados and have two columns called c1 and c2, with the following values: dados <- data.frame(c1=c(1:4, rep(0, 3)), c2=7:1) dados c1 c2 1 1 7 2 2 6 3 3 5 4 4 4 5 0 3…
-
2
votes2
answers5146
viewsA: Merge two data.frames into one
The R is saying that it cannot join data frames because they are of different types. Columns of df.temp are not numerical values, whereas itens2 sane. First, in order to solve this problem, I would…
ranswered Marcus Nunes 17,915 -
1
votes1
answer76
viewsA: Create new database from random values with loop or other method
I believe that the best way to solve this problem is not through a loop. I solved it by selecting the rows randomly, all at once. I saved these results inside called vectors index_a, index_b,…
ranswered Marcus Nunes 17,915 -
1
votes2
answers102
viewsA: Factor Column for Date
Date format is not correct. Use x$V1 <- as.Date(x$V1, format="%d-%b-%y") More information about date format can be found using ?strptime.
ranswered Marcus Nunes 17,915 -
2
votes2
answers751
viewsA: Error reading file: Error in scan line 6 Did not have 63 Elements
Without having access to the file savedrecs.txt, it is impossible to give a definitive answer to this question. It is only possible to speculate. It seems that the problem lies in the argument…
ranswered Marcus Nunes 17,915 -
3
votes3
answers202
viewsA: How to assign NA as value?
Spin enem$TP_COR_RACA[enem$TP_COR_RACA=="Nao"] <- NA The code enem$TP_COR_RACA=="Nao" will find the rows of the column enem$TP_COR_RACA which are equal to "Nao". It is then sufficient to replace…
ranswered Marcus Nunes 17,915 -
4
votes2
answers242
viewsA: Get only given last month using R
I don’t know if there’s a function that does this directly, but I was able to build an algorithm that apparently solved the problem. First of all, I separated the column ref.date, which has the…
-
4
votes2
answers1348
viewsA: Compare vector elements in R of different sizes
Use the command intersect: a <- seq(from=1, to=5, by=1) b <- seq(from=5, to=13, by=1) intersect(a, b) [1] 5
-
2
votes1
answer256
viewsA: How to play a graph that was created with the Plot command using ggplot2?
As a rule, the datasets to be plotted with the ggplot2 must be in the long format. In the specific case of your problem, your dataset should have three columns: t, bs and mu. So my first concern…
-
1
votes2
answers1175
viewsA: Find a particular line or specific value of a matrix vector in R
The function all.equal gives the result you want. I did not use the mat of your example because my computer, even with the adjusted seed, generates random numbers different than yours. So I created…
-
6
votes2
answers5899
viewsA: file read . XLSX in R
What is the problem and/or difference between reading a file in .txt and .xlsx in R? Strictly speaking, none. Both are valid ways to store data for analysis, as well as .csv, .sav and .dat are also.…
ranswered Marcus Nunes 17,915 -
3
votes1
answer108
viewsA: What’s new on R3.3.3?
Here’s a list of what’s new in R 3.3.3. As there are 3,137 characters of information, I find it counterproductive to copy and paste everything around here. Take a look at the link to see what has…
ranswered Marcus Nunes 17,915