Posts by Marcus Nunes • 17,915 points
372 posts
-
5
votes1
answer110
viewsA: Insert image with kableExtra function::spec_image( )
I solved the problem, but perhaps not the answer sought. (reread the images of the medals. as they were not provided in the original post, I searched others on the internet) The trick I used was to…
-
0
votes1
answer37
viewsA: How to change the order of the ggplot2 bar graph?
Utilize position = position_dodge2(reverse = TRUE) within the function geom_col. The argument reverse = TRUE will reverse the bar order. library(tidyverse) df3 <-data.frame(Locus…
-
4
votes3
answers64
viewsA: Obtaining single records based on two columns
I would solve this problem as follows: library(tidyverse) df1 %>% na.omit() %>% group_by(origem, destino) %>% summarise(num_total = n(), valor_total = sum(valor)) %>% ungroup() #>…
ranswered Marcus Nunes 17,915 -
5
votes1
answer110
viewsQ: Reorder levels of a categorical variable within panels according to the group to which they belong
When using the command fct_reorder(class, hwy, .fun = median) below, I asked for the variable levels class were reordered according to median variable hwy: library(tidyverse) ggplot(mpg, aes(x =…
-
4
votes1
answer33
viewsA: Plot multiple columns at the same time
The secret here is to understand the output of the function gather. Let’s see him in detail below. df <- read.table( text = "c1 c2 c3 x 2 4 5 0 3 5 2 0 6 7 8 0 1 2 5 1 2 5 6 1 3 3 3 1", header =…
-
3
votes2
answers47
viewsA: Plotted graphs separately in single window
There is more than one way to do this. My favorite is by using the function grid.arrange package gridExtra. For this, just create two charts independently and save them inside objects in the R. In…
-
2
votes1
answer29
viewsA: X-axis values in bold
Just put the argument axis.text.x = element_text(face = "bold") within the function theme. The argument axis.text.x informs that the desired change must occur in the x-axis markings (make the…
-
4
votes1
answer30
viewsA: How to arrange the order of the wrong captions using ggplot2 in R
By default, the R will understand which characters should be placed in alphabetical order. In this case, a string of characters from 1 to 19, when ordered, will be as below: x <-…
-
3
votes2
answers35
viewsA: How to select rows that have text searching in all columns of a data frame
A solution using functions of tidyverse is the following: library(tidyverse) my.data %>% filter_all(any_vars(str_detect(., pattern = "(?i)tryp"))) #> A B c d e #> 1 prot trypsina catalic…
-
2
votes1
answer46
viewsA: Logical condition on a dataframe
The function below solves the problem. It is commented, but basically it tests whether x is in percentil. If it is, you only find the corresponding value. If x is not in percentil, she finds in…
ranswered Marcus Nunes 17,915 -
5
votes3
answers54
viewsA: Replacing NA values of a column by the value of the top row of the same column of a dataframe
The function na.locf package zoo does exactly what is requested: library(zoo) #> #> Attaching package: 'zoo' #> The following objects are masked from 'package:base': #> #> as.Date,…
ranswered Marcus Nunes 17,915 -
6
votes1
answer55
viewsA: How to change values on the y-axis
I can see at least two ways to do this. The first one is more crude. Just split the column Valor for a million (1e6). library(tidyverse) # Gera o gráfico de barra ggplot(producao_aquicultura,…
-
5
votes2
answers101
viewsA: Insert scale on y-axis for geom_line
The idea of creating panels with facet_wrap is very good. For this, the first providence is to put the df long-form: head(df) #> Período analise1 analise2 analise3 #> 1 1990 -17.4 0.08 0.33…
-
4
votes1
answer39
viewsA: How to delete several elements from a list() at once in R?
To remove exactly positions 4, 5 and 9, just inform a vector with these positions to the list: x <- list("1", 1, 2, 2.1, TRUE, "yes", "necessary", 31, FALSE, "FALSE") x[-c(4, 5, 9)] #> [[1]]…
-
2
votes3
answers44
viewsA: How to index subgroups in R
Just use mutate combined with if_else. If the column situacao is equal to b, result gets 2. Otherwise you get 1. You don’t even need to group the data, because the process information is not used at…
ranswered Marcus Nunes 17,915 -
3
votes2
answers56
viewsA: Apply a function to a dataframe R
Because of his parole if, your function was created to be applied to one element at a time. So much so that this is the Warning that is given: the condition has length > 1 and only the first…
-
1
votes2
answers42
viewsA: Graph error in R: "invalid color name"
Since you are using a data set of the package dplyr, I suggest you dive into tidyverse. Another solution to create this graphic, already captioned, is to use the function ggplot: library(tidyverse)…
-
3
votes1
answer45
viewsQ: Automatically creating new variables through interaction between two pre-existing variables
Suppose I own the following set of dados: dados #> letras numeros cores valor #> 1 a 1 branco 2 #> 2 a 1 preto 1 #> 3 a 2 branco 9 #> 4 a 2 preto 4 #> 5 a 3 branco 8 #> 6 a 3…
-
1
votes1
answer40
viewsA: What is the difference between the bar graph and columns in R?
You can get the same results using geom_bar and geom_col. The syntax of the commands will be different, but aesthetically it is possible to get exactly the same graphical result. geom_bar leaves the…
-
1
votes1
answer34
viewsA: For loop in R is repeating answers more times than desired
The problem of the first loop is in the snippets of code given by numero_pinguim_femea <- which(tabela_pinguins$percentagem_da_diferenca> 11) numero_pinguim_macho <-…
-
7
votes1
answer56
viewsA: How to calculate average between lines
This is a moving average problem. The goal is to calculate the arithmetic mean of the last two periods. The package zoo of R is able to accomplish this task easily with the function rollmean:…
ranswered Marcus Nunes 17,915 -
3
votes1
answer30
viewsA: Up to how many different levels can a classifier predict?
Unless it is a binary classifier, there is no limit to the number of classes a classifier can work with. Logistic regression is a binary classifier, so it cannot be used to perform a classification…
-
6
votes1
answer69
viewsA: In R, is there a difference between double and single quotes?
According to the documentation of the R, no difference for most applications, although double quotes are preferred: Single and double Quotes delimit Character constants. They can be used…
-
2
votes1
answer85
viewsA: Coloring a map with differing color intensity in ggplot2
The function scale_fill_distiller does not have a color palette called #2D3E50, for the package RColorBrewer does not define that palette. To check the available palettes, turn the command…
-
3
votes2
answers35
viewsA: Error to calculate pBias
Note what the error message says: Error in pbias.default(yes$Prec, Obs$Prec, na.rm = TRUE) : Invalid argument type: 'yes' & 'Obs' have to be of class: c('integer', 'Numeric', 'ts', 'zoo') That…
ranswered Marcus Nunes 17,915 -
2
votes1
answer36
viewsA: Random Number Generator with Restrictions
I would draw first the values of rendaIE and rendaVariavel. The way these proportions are defined, their sums will never exceed 40%. From there, rendaFixa would be the difference between 100% and…
-
3
votes2
answers46
viewsA: Multiple Moving Medium Columns
I believe the argument align = "right" within the function zoo:rollmean whatever is needed to solve this problem. library(dplyr) #> #> Attaching package: 'dplyr' #> The following objects…
-
1
votes1
answer36
viewsQ: Change the language of the result of a web-scraping with rvest from the IMDB site
I want to collect information about the IMDB Top 250 using the package rvest. While visiting the page link, the names of the movies appear in their original language, at least in my browser (Firefox…
-
4
votes1
answer52
viewsA: Apply row-to-row formula within a table in R
The R has a specific function for this called scale: fx <- c(6.9, 6.8, 6.7, 6.0, 6.8) fx_scale <- scale(fx) fx_scale #> [,1] #> [1,] 0.7129310 #> [2,] 0.4387268 #> [3,] 0.1645225…
-
2
votes2
answers58
viewsA: How does the numpy Prod method work?
The array [365,364,363,362,361,360,359,358,357,356,355,354] is understood as an array of integer numbers. Thus, an overflow occurs in python if an integer value exceeds the maximum value that it is…
-
4
votes2
answers39
viewsA: Graphic editing in ggplot2
Change the names mean.est and mean.obs in the data set itself. This will avoid many unnecessary manipulations with the ggplot2. One way I suggest to do this is through function ifelse. The line…
-
3
votes1
answer42
viewsA: Standardize phone number with REGEX on R
To solve this problem with a regular expression, I would use the expression .*(?=.{9}$), that will extract the last 9 characters from each field within numero_telefonico. As there is an occurrence…
-
1
votes1
answer54
viewsA: Plot of the CL50 Curve in R using ggplot2 with estimates obtained in Stan
The warnings give a good hint of what is happening: Warning messages: 1: Use of `dados$logdose` is discouraged. Use `logdose` instead. 2: Use of `dados$periodo` is discouraged. Use `periodo`…
-
1
votes1
answer15
viewsA: Transformation of data frames into binaries in R
First, let’s find out what are the unique elements present in the matrix: elementos <- sort(unique(as.vector(my.matrix))) elementos #> [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" Then the…
-
2
votes2
answers38
viewsA: how to identify and delete columns with characters and factor in R
Use the function select_if of tidyverse: library(tidyverse) j<-c(1,2,3,4,5,6,7,8,9,10) k<-c(50,2,042,3658,14,3586,324,24,352,217)…
ranswered Marcus Nunes 17,915 -
0
votes1
answer242
viewsA: Unable to Import csv file into pandas
If the user is called sistema and the file pop.csv is in the folder /Users/sistema/Documents/dados/, the controls below shall operate: import pandas as pd pd.read_csv('~/Documents/dados/pop.csv') If…
-
2
votes1
answer90
viewsA: how to create a frequency histogram with preset non-uniform intervals?
Basically, just use the argument breaks custom. For example, using R pattern: dados <- c(1, 1.2, 40, 1000, 36.66, 400.55, 100, 99, 2, 1500, 333.45, 25, 125.66, 141, 5, 87, 123.2, 61, 93, 85, 40,…
-
6
votes1
answer30
viewsA: Error while integrating a real function
The function integrate is defined as Adaptive Quadrature of functions of one variable over a Finite or Infinite interval In particular, see how she defines the integrating f (griffins of mine): an R…
-
4
votes1
answer29
viewsA: How to remove duplicate figures generated by the geom_errorbar function?
Like geom_errorbar in this case is not measuring, in fact, a variability, I recommend using geom_segment to make the desired marking. In this case, 3 segments are constructed: The vertical segment…
-
4
votes1
answer138
viewsA: Combine with grid.arrange two residue charts (lme vs. lm)
Before I give my answer to the question, I would like to say that there are a number of problems with this waste analysis. Created graphs do not even show the same information. The graph for the…
-
5
votes1
answer96
viewsA: Overlay two scatter charts into one (with ggplot)
Like almost everything in ggplot2, the best way to solve graphics display problems is by organizing the data frame. In case, I suggest it be a data frame only, with three columns: crc, maxilla and…
-
5
votes1
answer107
viewsA: What is the difference between facet_wrap() and facet_grid() in ggplot?
What is the difference of rationality behind these two options? facet_wrap transforms a sequence of one-dimensional panels into something in two dimensions, while facet_grid creates an array of…
-
5
votes1
answer72
viewsA: Inclusion x-axis name of months
First, confirm that all the packages below are installed on your machine: library(ggplot2) library(dplyr) #> #> Attaching package: 'dplyr' #> The following objects are masked from…
-
2
votes1
answer45
viewsA: Half of boxplot appearing in ggplot2 function
The boxplots are not appearing in half. The data has a very pronounced asymmetry, which makes some statistics get confused. For example, the CS variable has the minimum and first quartile identical…
-
3
votes3
answers49
viewsA: Normality and fragmentation of the sample
It is possible to use packages tidyverse and broom and get the results organized in a data frame: library(tidyverse) library(broom) set.seed(1) df_1 <- data.frame( x = rnorm(n = 30, mean = 10, sd…
ranswered Marcus Nunes 17,915 -
3
votes1
answer41
viewsA: Plot factors (spp) of metaMDS in ggplot
The object sol already has all the information needed to create the multidimensional scaling Plot. library(vegan) data(dune) sol <- metaMDS(dune) In particular, see the information present within…
-
2
votes2
answers38
viewsA: Reorder Columns in graph made by ggplot
I believe the code below can help you: nif <- nif %>% mutate(Genes = factor(Genes, levels = c("nifW", "nifV", "nifS", "nifU", "nifQ","nifX","nifN","nifE", "nifK", "nifD", "nifH", "nifT",…
-
1
votes1
answer36
viewsA: The "date" format disappears after using the "group_by" function followed by "summarise (Mean())" in R
By definition, the function year package lubridate returns a numeric value. This is the reason for the column dt was double and not a date. x <- ymd("2012-03-26") is.numeric(year(x)) ## [1] TRUE…
-
5
votes1
answer31
viewsA: No convergence of the estimates of the parameters of the Richards model in the "nls" package of r even with good kick start
In general, by adjusting a model to a data set, we seek to adjust the simplest possible model in the largest data set available. Remember this when reading my answer below. The problem with your…
ranswered Marcus Nunes 17,915 -
6
votes1
answer89
viewsA: Last characters of a data.frame column in R
A simple way to do this with R base is through the command substr. With it, I can extract a substring indicating where it begins and where it ends. Let’s look at two practical examples, with the…
ranswered Marcus Nunes 17,915