Posts by JdeMello • 844 points
23 posts
-
0
votes2
answers57
viewsA: How to replicate a line with a difference of names on it? R
You can use Ellipsis ... in its function. Within the argument of a function, ... allows the function to accept n arguments -with names or not. See how the function works in action: foo <-…
-
1
votes3
answers115
viewsA: Regular expression of citations in R
No need to use the stringr in this exercise. We can use regmatches with gregexpr. regmatches will extract the substrings that were found with gregexpr -this last function finds all possible strings…
-
3
votes1
answer611
viewsA: SIZE OF A DATAFRAME
Not a package or R problem, but a Sidra API problem. For example, d <- data.frame(A = runif(10000000)) is stored smoothly. On the API help site, we have: Limit on searching data The data query is…
-
0
votes4
answers188
viewsA: Import Excel file to R
If it is an encoding problem, try to find the file through pattern in dir(). I put a pattern well general but you can replace it with something more specific. file <- dir("C:/R sidra", pattern =…
-
0
votes2
answers145
viewsA: Operations with Data Frame
Only to complement the above answer. We can do this dynamically (without having to name the columns). This can be useful if you have a large number of columns or if you are not sure of the nature of…
-
3
votes7
answers1523
viewsA: How to calculate the median of a line in a date.frame in R?
The fastest method is using data.table together with the package matrixStats (bundle rap is still under development so I won’t use it but I suspect it’s very efficient). For comparative purposes, I…
-
4
votes1
answer251
viewsA: problem with unnest_tokens()
You need to translate your text into data.frame: library(tidytext) library(dplyr) library(readr) estrela <- "texto stackoverflow português" estrela <- data.frame(text = estrela,…
-
4
votes4
answers315
viewsA: How to apply several functions to the same object?
We can create a function using as argument an Ellipsis (...). set.seed(123) x <- rnorm(10) myFun <- function(x, ...){ funs <- list(...) res <- vector("list", length = length(funs)) for(i…
-
4
votes2
answers292
viewsA: Cumulative count of group occurrences on dates
Using data.table: library(magrittr) library(data.table) setDT(dados) dados <- dados[, .(N = .N), by = c("data", "grupo")] %>% dcast(data ~ grupo, value.var = "N", fill = 0) %>% melt(.,…
-
3
votes1
answer385
viewsA: How to create a categorical variable in R?
We can use data.table to do this operation: # carregar data.table library(data.table) dadosDt <- as.data.table(dados) # transfomar dados em formato data.table res <- data.table::dcast(dados,…
-
7
votes2
answers130
viewsA: R - cut digits
We can use the function substr doing the following: ano<- c(1999,2000,2001,2002,2003) substr(ano, 3, 4) outworking: [1] "99" "00" "01" "02" "03" What the function substr ago? The function has 3…
-
4
votes3
answers2296
viewsA: How to format a "date" column inside of a data.frame in R?
I also use the package a lot lubridate but in this case it is not necessary. Just use the function format() that is already loaded in the package base: tabela <- data.frame(timestamp =…
-
3
votes1
answer100
viewsA: Why is "vector" considered a "list" in some cases?
The attribute that cluster inheritance is not list but yes kmeans. is.vector must look for the class attribute of an object (if you have it), see this example: baz <- c(1:10) is.vector(baz) #…
-
3
votes2
answers74
viewsA: Problem in generating multiple charts
You need to transform your data from "wide" to "long" format. I use the function melt package data.table: # criando os dados set.seed(1) yrs <- c(2014:2018) df <- do.call(cbind, lapply(yrs,…
-
4
votes2
answers501
viewsA: How to receive an input during an R function?
We can also use menu(): pergunta <- function(){ sel <- c("Sim", "Não") ans <- menu(sel, title = "Tem certeza que quer fazer isso? (S/N)") return(paste0('Sua resposta foi ',…
-
4
votes4
answers4136
viewsA: Count equal values in one data frame and store in another in R
It is also possible to do this in a line with the package data.table: library(data.table) setDT(total_amostral) # transforme total_amostral em data.table total_amostral[, .N, by = "TOTAL"] Upshot:…
-
3
votes1
answer170
viewsA: How to create a data frame with results from a function
If what you want to simplify is the process of merging the results of the function into a vector to form a columna in Tabela_final, then you can use lapply with do.call to form a vector. z <-…
-
2
votes2
answers455
viewsA: Filter data frame according to indexes (lines) stored in a vector
Expanding the answer given by @Rafaelcunha. If in the case you want to filter one data.frame based on another data.frame, you can do it this way. set.seed(1) d1 <- data.frame(col1 =…
-
4
votes4
answers145
viewsA: How to count strings from a variable
A solution without dependency on external packages: partidos_txt <- c("AVANTE / PDT / PODE / PMN AVANTE / PR / PV DC / PRTB / AVANTE / SOLIDARIEDADE / PRP / PATRI DC / PSL / PRTB /…
-
1
votes3
answers969
viewsA: Differences and similarities between apply and for loop functions
Differences between lapply() and for This is a non-exhaustive comment on that topic. Really, lapply tends to be faster than for in r. That’s why lapply() is partly written in a language low-level…
-
1
votes2
answers72
viewsA: Function `Broom::Tidy` does not produce the output for some functions
The problem is that shap returns a list through lapply. "Disconnect" the output from shap: shap <- function(x){ lapply(x,shapiro.test) %>% unlist } Producing: res2<-dataset%>%…
-
3
votes3
answers155
viewsA: Error with variable naming functions in a list
The answers given are excellent and follow the pattern tidyverse. Anyway, I will give other solutions using the excellent package data.table and base. Data.table solution data.table has the function…
-
5
votes3
answers186
viewsA: Remove Environment (and list) elements based on their classes
Call an anonymous function inside rm() not to affect the objects shown in ls(). a<-1:10 b<-1:10 c<-'a' d<-'a' e<-list('b') f<-list('b') g<-1.1 h<-1.1 rm(list = (function(x,…