Posts by Carlos Cinelli • 16,826 points
232 posts
-
1
votes1
answer50
viewsA: @slot Extraction
There is a class of objects in R calling for S4. Contents within an object of type S4 are called slots. In the same way as the $ is used to access columns of a data.frame, you can access the slots…
ranswered Carlos Cinelli 16,826 -
1
votes1
answer181
viewsA: Loop using columns on objects in R
You are applying the function qnorm() in a text array. See: d <- "texto" qnorm(d) #> Error in qnorm(d): Non-numeric argument to mathematical function Apparently his intention was to rotate…
-
2
votes2
answers605
viewsA: What are the main functions for creating a minimum reproducible example in R?
As stated in the question link, a minimum reproducible example should have the following contents: A small data set; The smallest code possible that is executable and which reproduces the error in…
ranswered Carlos Cinelli 16,826 -
12
votes2
answers605
viewsQ: What are the main functions for creating a minimum reproducible example in R?
What are the main functions to create a reproducible minimum example in r? More specifically, I would like the answers to address the following topics:: What are the functions to ensure that the…
rasked Carlos Cinelli 16,826 -
6
votes2
answers186
viewsA: Run command for each column of a date.frame
You can do this in a line with lapply: dwc_ST <- lapply(df, mra, wf = "la8", method = "dwt", J=3, boundary = "reflection") The result is a list of the calculations for each column: str(dwc_ST,…
-
2
votes2
answers690
viewsA: Searching strings in R language
For a simple match, you can use the function str_subset package stringr: library(stringr) texto <- c("abc projeto de pesquisa cdf", "123 projeto de pesquisa", "progeto de pesquisa")…
ranswered Carlos Cinelli 16,826 -
4
votes2
answers538
viewsA: How to select data in R?
Using Rui’s sample database, another alternative is: tapply(pott$GPP, gl(nrow(pott)/30, 30), mean) Explaining: the command gl(nrow(pott)/30, 30) creates size 30 factors for your database. And tapply…
ranswered Carlos Cinelli 16,826 -
7
votes2
answers101
viewsA: Complete observations in a data frame
With the tidyr you can use the function complete(): library(tidyr) complete(dados, Ano, Categoria, fill = list(Valor = 0)) # A tibble: 20 x 3 Ano Categoria Valor <int> <fctr> <dbl>…
ranswered Carlos Cinelli 16,826 -
10
votes1
answer996
viewsA: Estimate the Poisson - R distribution
There are several ways to estimate the parameters of a distribution (maximum likelihood, moment methods, Bayes). To enter this scope would escape the scope of the site, because it is a question of…
ranswered Carlos Cinelli 16,826 -
2
votes1
answer124
viewsA: Plot of the degree distribution of a graph in R
You can request the log scale directly in the Plot command: g2 <- sample_gnp(1000, 10/1000) dg2 <- degree_distribution(g2) x <- 1:max(degree(g2)) - 1 zeros <- (dg2 == 0) # para remover…
-
1
votes2
answers381
viewsA: combine vectors per line in R by filling voids
You can increase the vector b to be the same size as the vector a including zeroes: rbind(a, b = c(b, rep(0, length(a) - length(b)))) [,1] [,2] [,3] [,4] a 1 2 3 4 b 1 2 0 0…
ranswered Carlos Cinelli 16,826 -
5
votes2
answers11355
viewsA: Select column of a data.frame --- Database Division in R
There are several ways to select columns from a data.frame in R, let’s use as an example the data.frame mtcars. To find out which columns exist, you can ask to see the names or colnames of…
ranswered Carlos Cinelli 16,826 -
3
votes2
answers658
viewsA: Obtain coefficients "a" and "b" of the Linear Regression Model in R
It is worth mentioning here also the package Broom which makes it easier to obtain data from a regression (estimates, standard error, statistics t, p-value etc). For example, to take the basic data…
ranswered Carlos Cinelli 16,826 -
3
votes1
answer2467
viewsA: Multiple Regression with R
You can run a regression on R using the function lm. Using the base mtcars that already comes in R as an example: regressao <- lm(mpg ~ cyl, data = mtcars) First we move to the function lm…
ranswered Carlos Cinelli 16,826 -
0
votes2
answers291
viewsA: Selecting part of a data frame and saving in loop
Also using the mtcars with the column cyl, follows example by making a loop with the for: for(i in seq_along(unique(mtcars$cyl))) write.csv(subset(mtcars, cyl == unique(mtcars$cyl)[[i]]),…
ranswered Carlos Cinelli 16,826 -
4
votes3
answers1016
viewsA: Split a data frame and save to different directories
This is the ideal case for the function split. With the function split you can share your data.frame according to the values in the column Quantidade: tab_split <- split(tab, tab$Quantidade) The…
ranswered Carlos Cinelli 16,826 -
2
votes3
answers749
viewsA: Add currency rating in R
Using paste and format: paste("R$", format(3454575678, decimal.mark = ",", big.mark = ".", nsmall = 2)) "R$ 3.454.575.678,00"
ranswered Carlos Cinelli 16,826 -
3
votes2
answers158
viewsA: Play a chart in R
On a Plot basis: plot( compl$x, compl$y, pch= 1,xlab="x",ylab="y",ylim=NULL,xlim=NULL,axes = F) axis(1, at=c(0, 3.5, max(compl$x)), labels = c("", expression(x[0]), ""), lwd.tick=0) axis(2, at=c(0,…
-
2
votes2
answers4518
viewsA: Select multiple lines of a data.frame from the highest R values
To complement, how to do data.table: library(data.table) setDT(df) df[order(x, decreasing = T),][1:5,] x y 1: 10 5 2: 10 4 3: 8 3 4: 8 2 5: 8 4 To remove duplicates in the column x, sort by x and…
-
5
votes1
answer175
viewsA: Invert factors in only 1 bar in ggplot2
Gambiarra, but it works: dados2$c <- ifelse(dados2$a == "a", dados2$b, NA) dados2$d <- ifelse(dados2$a == "a", dados2$val, NA) dados2$val[dados2$a == "a"] <- NA ggplot(aes(x = a, y = d,…
-
1
votes1
answer2020
viewsA: How to create a chart of averages and standard deviation in a data set that includes missing values (Nas)
You have to explicitly ask for the functions mean() and sd() ignore the NA, passing the argument na.rm = TRUE: lineplot.CI(dados$especie, dados$DeltaTime, las=1, type="p", xlab="Espécie",…
ranswered Carlos Cinelli 16,826 -
4
votes1
answer748
viewsA: filter in dplyr using a categorical variable
In this case you can use the %in%: dados %>% filter(grupos %in% c("a", "b")) grupos valores 1 a -1.4806 2 b 1.5772 3 a -0.2723 4 b -0.3153 5 a -0.7777 6 b -1.2939 7 a -0.7035 8 b 1.1889 9 a…
-
3
votes2
answers206
viewsA: Scatter charts fixing a response variable
A solution is to first put your data.frame in format long, and then use in the ggplot2 directly: # carrega pacotes library(reshape2) library(ggplot2) # coloca dados no formato long iris_long <-…
-
3
votes1
answer160
viewsA: Python / R Check density peaks in ggplot2
You can take the data from the graph generated by ggplot2 by asking the print explicitly. For example, let’s generate a histogram: rm(list = ls()) library(ggplot2) set.seed(10) df <- data.frame(x…
-
5
votes1
answer48
viewsA: R code_with() function
The function with is a convenience function that allows you to access the columns of the data.frame without having to repeat the name of data.frame at all times. The code: with(epilepsy,…
ranswered Carlos Cinelli 16,826 -
3
votes1
answer78
viewsA: Error with interactive menu function when running block code
Running the code block is the same thing as sending interactively commands to the console. That way, when you run the block code, the R will find that the line soon after menu() is the answer to the…
-
2
votes2
answers490
viewsA: Modify t test for linear regression parameters in R
With the R base you can ask for confidence intervals with the function confint: confint(reg) 2.5 % 97.5 % (Intercept) -0.005635243 0.7256352 x 0.849756826 1.0702432 In the above case, with a 95%…
-
6
votes3
answers202
viewsA: How to assign NA as value?
As you are using factors, you can also directly change the levels: levels(enem$TP_COR_RACA)[levels(enem$TP_COR_RACA) == "Nao"] <- NA
ranswered Carlos Cinelli 16,826 -
1
votes2
answers1175
viewsA: Find a particular line or specific value of a matrix vector in R
As a function below you can compare either the rows or columns of a matrix with a vector, respected a numerical tolerance level. If fun = all you compare all vector elements. If fun = any you check…
-
3
votes2
answers242
viewsA: Get only given last month using R
A quick and simple solution with the dplyr: library(dplyr) ultimos <- ntnb45 %>% group_by(anomes = format(ref.date, "%Y%m")) %>% filter(ref.date == max(ref.date)) ultimos Source: local data…
-
3
votes1
answer2685
viewsA: How to color the legend according to the chart in R
You can put a legend on top of another with transparent background: legend('bottomleft', legend = c('Control','10 mg/mL','5 mg/mL','2,5 mg/mL'), col = c("black"), lty = 1, lwd=2, pch = NA)…
-
3
votes2
answers1839
viewsA: Column chart stacked in R
If you reformat your database is easy to do with the ggplot2. To reformat I’ll use the reshape2 and dplyr: library(ggplot2) library(dplyr) library(reshape2) molten <- melt(base, variable.name =…
ranswered Carlos Cinelli 16,826 -
5
votes2
answers5899
viewsA: file read . XLSX in R
Well there are still some differences that are important in relation to the two formats and in general I would say that your friend is right. 1) txt reading is faster and you don’t depend much on…
ranswered Carlos Cinelli 16,826 -
2
votes2
answers1312
viewsA: How to change the "Key" orientation of a Caption in ggplot?
To horizontal the caption, simply change the part legend.direction = "vertical" for legend.direction = "horizontal". And to change the background of the legend to the same of the chart, just change…
-
3
votes2
answers91
viewsA: Adding the same substring to multiple columns in R
Using the same basis df created by @José: df[] <- lapply(df, paste0, "PF")
-
2
votes1
answer5304
viewsA: How to assemble models for GLM in R
There are several packages for adjusting generalized linear models (GLM) in R. However, people probably use the function itself more glm that already comes in R base. To adjust a model using the…
-
0
votes2
answers1264
viewsA: Calculation at a specific value of a dataframe?
There are several ways to manipulate the values of a column of a data.frame. With the base functions of R, assuming your data.frame is called df, in your case just do: df$Comum <- df$Comum*100 If…
ranswered Carlos Cinelli 16,826 -
1
votes1
answer671
viewsA: Reorganize Order of Dataframe Lines
In your specific case you are placing line 9 at position 17. So, assuming your data.frame be called df, you can simply select the lines in that order: df_novo <- df[c(1:8, 10:17, 9, 18:nrow(df)),…
ranswered Carlos Cinelli 16,826 -
1
votes4
answers4556
viewsA: How to change the order of appearance of Columns in a Data Frame?
Using the same names as the @Marcusnunes reply, you can also do it with the base R: df_ordenado <- df[c("Indice", "Produto", "Classificação", "Comun", "Quilo")]
ranswered Carlos Cinelli 16,826 -
2
votes1
answer1431
viewsA: How to stratify/divide a data.frame into categories of a variable in R?
There are several ways to run multiple regressions per category in R. I’ll show you how to do with the base functions of R and with the dplyr. As an example, let’s use the database mtcars. Suppose…
-
3
votes2
answers726
viewsA: How to rename the database (Rdata) in R?
Really this is a data problem in format .rdata, because you don’t know what’s inside the file and they can overwrite various things. If you do this manually it can be quite complicated, you will…
-
6
votes2
answers800
viewsA: How to convert numbers into categories in R
This is a case where factors are useful. Just turn your variable into a factor. Creating an example vector: set.seed(10) exemplo_numero <- sample(0:5, 10, replace = TRUE) You can use the function…
ranswered Carlos Cinelli 16,826 -
1
votes1
answer127
viewsA: Return of the gradient function when using Optim and colSums functions
The gradient function needs to return a vector with the same size as the number of parameters. In its function, there are two occasions when this does not occur. When sigma <0 you return the…
ranswered Carlos Cinelli 16,826 -
0
votes4
answers1920
viewsA: Create column with sum and percentage of the maximum of other columns
You can do everything easily with the R base functions as well. In your case you are applying a line function. Using the base dt_so_bruna created by Dan: dt_so_bruna$soma <- rowSums(dt_so_bruna,…
ranswered Carlos Cinelli 16,826 -
1
votes5
answers14983
viewsA: How to merge multiple dates frames into one
With the dplyr you can use the functions of Join chained with Piper Operator %>% (inner_join, full_join, left_join, right_join etc, depending on your goal). Example with full_join chained:…
ranswered Carlos Cinelli 16,826 -
1
votes3
answers955
viewsA: How to place multiple formats on the dots of my PCA by ggplot2
It is also possible to do on the R base. Example: ir.pca <- prcomp(iris[,1:4]) plot(ir.pca$x[,1], ir.pca$x[,2], pch = as.numeric(iris$Species), xlab = "PC1", ylab = "PC2") legend("topright", pch…
-
2
votes1
answer64
viewsA: perform . Globalenv function in parallel processing
You have to export the object add_a for each cluster node. One way to do this is to create the cluster manually and add the function to each node. For example: library(dplyr) library(purrr)…
-
3
votes1
answer71
viewsA: Date formatting for Gtrends (gtrendsR package)
This is a package bug gtrendsR: https://github.com/PMassicotte/gtrendsR/issues/96 They were supposed to fix this a long time ago. Well, while this is not happening, a partial solution is to run:…
-
6
votes2
answers12366
viewsA: How to sum the values of selected columns of each observation (row) in R?
As you are adding up, you can also directly use the rowSums, which is usually a little faster than the apply: rowSums(USArrests[,1:3])
ranswered Carlos Cinelli 16,826 -
4
votes1
answer4792
viewsA: How to delete any line from a data frame in R?
To delete a line from data.frame you must not select that line and overwrite your data.frame. For example, using data from data.frame of your image: notas.inform <- data.frame(nros = c(2355,…
ranswered Carlos Cinelli 16,826