Posts by Daniel Falbel • 12,504 points
268 posts
-
4
votes1
answer80
viewsA: Max of a numeric field returning NA
Try the following: max(dados_enem$NOTA_CN, na.rm = TRUE) min(dados_enem$NOTA_CN, na.rm = TRUE) mean(dados_enem$NOTA_CN, na.rm = TRUE) By default, these functions return NA result when there is NA…
ranswered Daniel Falbel 12,504 -
8
votes2
answers1310
viewsQ: How to remove a word from a string without changing larger words that contain it
I would like to remove a word from a string in R. I was doing it as follows: > s <- "ele esta bem mas tambem esta triste" > stringr::str_replace_all(s, "tambem", "") [1] "ele esta bem mas…
-
4
votes1
answer1756
viewsA: Error: The condition has length > 1 and only the first element will be used
The if in R is not vectored, that is, it accepts only one value TRUE or FALSE. Turns out on your line: BWG <- if ((BWGmax*(1-exp(-K*(MCisimulate-Xm)))) > WG) { WG } else {…
-
3
votes1
answer493
viewsA: How to turn plot of scatter into density?
Using the ggplot2 the following scatter chart: library(ggplot2) ggplot(faithful, aes(x = eruptions, y = waiting)) + geom_point() + xlim(0.5, 6) + ylim(40, 110) You can stay like this, just changing…
-
1
votes2
answers669
viewsA: Basic SAS ProcMeans for R
You will achieve using the code below: Substitute mtcars by your database. Also make sure that all variables in your database are numerical, otherwise the code will give error. library(plyr)…
-
1
votes2
answers940
viewsA: Filter 5GB CSV file on R
André, as you will only filter your database and after that it will get much smaller, you can read it in Chunks. For this you can do as follows: Just p/ test created the following file "large":…
ranswered Daniel Falbel 12,504 -
7
votes2
answers940
viewsA: Filter 5GB CSV file on R
Here are some tips p/ solve your problem: 1 - Read the file using another function: > microbenchmark( + base = read.csv(file = "df-write-csv.csv", header = T), + readr =…
ranswered Daniel Falbel 12,504 -
3
votes7
answers9711
viewsA: Remove accents
I like it this way: s <- c("ájakla","ééhasj", "hsíklf", "fdhjó") chartr("áéíó", "aeio", s) [1] "ajakla" "eehasj" "hsiklf" "fdhjo" This way just add the accented characters and their substitutions…
ranswered Daniel Falbel 12,504 -
1
votes1
answer432
viewsA: Marginal Effect for GLM (Logit) with categorical variables.
Rafael, an alternative would be to do the following. calculate the predicted probability p/ each of the individuals in your sample using your model and function predict. filter into your sample only…
ranswered Daniel Falbel 12,504 -
4
votes4
answers2494
viewsA: How to join observations of tables that have a different set of variables in R?
In this case I always use the function bind_rows of dplyr: library(dplyr) dados <- bind_rows(df1,df2) > dados Source: local data frame [20 x 3] id z x (int) (dbl) (dbl) 1 1 0.8179472 NA 2 2…
ranswered Daniel Falbel 12,504 -
2
votes2
answers1104
viewsA: Show an object/variable with different names in R?
You can replace your last line by the following: print(eval(parse(text = nomevar))) The function parse transforms the string that is the contents of the variable nomevar in an expression of R. The…
-
3
votes3
answers108
viewsA: How can I restructure information contained in a list object into two columns?
Another way to do this is this: > library(dplyr) > x <- list(x = 1:3, y = 6:10, z = 8:13) > lapply(x, function(x) data.frame(X1 = x)) %>% bind_rows(.id = "X2") Source: local data…
-
5
votes3
answers1888
viewsA: A: Remove columns from a dataframe
You can do so too: df[, -3] Negative indices exclude columns in R. If you want to delete more than one column, do so: df[,-c(1,3)] The problem with this is that if there is only one column left it…
ranswered Daniel Falbel 12,504 -
3
votes2
answers57
viewsA: A: problems with Sort (NA)
If you use the function order, the standard is na.last = T. So this way it would work: > x <- c("c", "a", NA, "b") > x[order(x)] [1] "a" "b" "c" NA Just like in Alexandre’s answer, you can…
-
34
votes2
answers4860
viewsA: Algorithm to detect nudity with good accuracy
To complement the @Luizvieira response in the Machine Learning question to sort images. In Machine Learning, first of all, you will need a large number of images previously classified by a human in…
-
4
votes2
answers686
viewsA: How to make webscrapping of an https using rvest?
This does not seem possible using the package rvest. Reading the source code, we see that the function read_html is a function wrapper read_xml. The source code is available in this link. The…
-
7
votes1
answer1466
viewsA: How to make a bubble chart, each filled in according to a ratio
I managed in a very complicated way, if someone knows how to simplify it will be very welcome: # função que separa os pontos de um círculo de acordo com a proporção # definida pelo parametro p…
-
13
votes1
answer1466
viewsQ: How to make a bubble chart, each filled in according to a ratio
I’m trying to make a chart like this on R: The graph is available on this link. I don’t need it to be dynamic like the link, but if it is, no problem. In it, each bubble is divided between the blue…
-
7
votes1
answer241
viewsA: Programmatically generate links and download content
You can do this using the package rvest. The following code will help you: library(rvest) # criando a sessão de navegação sessao <-…
-
5
votes1
answer4952
viewsA: How to generate random values for a known distribution?
Every statistical distribution can be defined by a cumulative distribution function F(x). A well-known result states that if you have a random variable U with uniform distribution in the range…
-
2
votes4
answers1242
viewsA: How can I save a list or export a list object in R?
Besides the other alternatives, I also like to save in a file .json. This file format has become standard in data sharing in list format. In R, it’s easy to save like this: x<- list(cod = 1:10,…
-
3
votes1
answer164
viewsA: ARIMA model with 1 and 25 lag in R
Yes there is. Considering the series lh of R: > lh Time Series: Start = 1 End = 48 Frequency = 1 [1] 2.4 2.4 2.4 2.2 2.1 1.5 2.3 2.3 2.5 2.0 1.9 1.7 2.2 1.8 3.2 3.2 2.7 2.2 2.2 1.9 1.9 [22] 1.8…
ranswered Daniel Falbel 12,504 -
3
votes1
answer2745
viewsA: R how to generate a time series?
The class object ts will not understand your column of dates. On the contrary, Take try to convert it into some number. Something like: > as.numeric(as.Date("2014-01-02")) [1] 16072 To create…
-
3
votes2
answers344
viewsA: How to make a matrix in R that its inputs are equal to i * j?
You can do it like this: > x <- matrix(rep(1:10, each = 10), ncol = 10, byrow = T) > apply(x, 1, function(x) x*1:10) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 2 3 4 5 6 7 8…
-
1
votes1
answer191
viewsA: Redeem time period in r
I didn’t see how your data was stored so I invented a database. This sequence of operations will take only the period from 20/06 to 20/09 of all the years that are at the base. library(lubridate)…
-
2
votes1
answer67
viewsA: How to store the address of the equal elements between vector and matrix?
Try it like this: > set.seed(1) > a = sample(1:3, 4, replace = T) > b = matrix(sample(1:3, 20, replace = T), ncol = 5, byrow = T) > a [1] 1 2 2 3 > b [,1] [,2] [,3] [,4] [,5] [1,] 1 3…
ranswered Daniel Falbel 12,504 -
3
votes1
answer369
viewsA: R package to convert numbers to full-text number
As far as I know, there is nothing ready, but as the rules are simple it is not difficult to program. You need a few-stop: excessoes <- data.frame( num = 11:19, nome = c("onze", "doze", "treze",…
ranswered Daniel Falbel 12,504 -
40
votes3
answers2481
viewsQ: How is a sound interpreted by a computer?
How a song is interpreted by the computer? I understand that an image is a matrix of pixels, in which each element is a colour. When I open a song on R, he presents it as two sequences (left and…
asked Daniel Falbel 12,504 -
3
votes1
answer857
viewsA: How to get the first non-zero element?
Try the following function: primeira_linha_nao_nula <- function(m){ vetor <- apply(m, 1, function(x) return(all(x != 0))) indice <- NULL if(max(vetor) == 1){ indice <- order(vetor,…
ranswered Daniel Falbel 12,504 -
1
votes2
answers82
viewsA: Comparison of elements of a vector
Complementing Wagner’s answer. It’s correct, but it doesn’t work if the vectors contain values NA. > a = rep(NA, 5) > all(a[1] == a) [1] NA > b = c(rep(1, 4),NA) > all(b[1] == b) [1] NA…
ranswered Daniel Falbel 12,504 -
3
votes1
answer193
viewsA: Update XLS spreadsheet without overwriting data using R
If your data isn’t too large, I would create a function like this: write.xlsx2 <- function(obj, path, sheetName){ dados <- read.xlsx(path = path, sheetName = sheetName) dados <-…
ranswered Daniel Falbel 12,504 -
7
votes1
answer2787
viewsA: How to read PDF data in R?
I’ll give you an incomplete answer because I’m running out of time, but I think it might help. Someone can then edit by adding the last step. You can use the package extractr. Read the installation…
-
1
votes2
answers81
viewsA: Obtain the Summary of the results from the use of the LOESS
Makes it easier to respond by explaining each of your commands: the command: xl <- with(dados.frame, seq(min(date_time),max(date_time), (max(date_time) - min(date_time))/1000)) creates a sequence…
ranswered Daniel Falbel 12,504 -
5
votes5
answers8758
viewsA: How to remove line that has Missing?
Consider the following database: > dados <- data.frame( + var1 = c(NA, 1), + var2 = c(1, NA) + ) > > dados var1 var2 1 NA 1 2 1 NA You can delete all lines that have at least one Missing…
ranswered Daniel Falbel 12,504 -
5
votes3
answers611
viewsA: How to build time series with frequencies different from the original?
In R, you can use the package lubridate which will greatly facilitate the manipulation of dates, together with the dplyr. Take an example: library(lubridate) dados <- data.frame( data =…
-
2
votes2
answers2625
viewsA: Bar graph text stacked in R
You can try this using the ggplot2. I made an example that you can try to adapt to your data: 1. Creating the database: dados <- data.frame( nomes = c("A", "A", "B", "B"), tipo =…
-
2
votes3
answers656
viewsA: Commands as. Date and srtptime in R
You need to use the command: as.Date(datas, "%d/%m/%Y") Note the %Y uppercase. It indicates that the year is in four digits. When you use the %y tiny, he understands that the year has only two…
-
2
votes2
answers122
viewsA: How can I get maximum and minimum values after applying LOESS
When you use the function stat_smooth of ggplot2, he performs the following steps: I’ll use the database mtcars, but you can easily replace it with your own. modelo <- loess(mpg ~ hp, data =…
-
2
votes1
answer305
viewsA: GLM, Poisson - non-integer count numbers (average)
There is no way to use the average of the two observations and at the same time use the Poisson distribution. You might try to figure out what the probability distribution of the average of two…
ranswered Daniel Falbel 12,504 -
7
votes1
answer424
viewsQ: vector moving mean of a matrix in R
I’m doing the following operation with a matrix in the R: > m <- matrix(1:9, ncol = 3, nrow = 3) > m [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 > m2 <- m > for(i in…
-
1
votes1
answer1049
viewsA: How to transform data imported from Excel (.csv) into time series
I believe the problem is reading the data. Apparently his data has a thousands separator that R is not understanding. That’s why he reads the variables as factor, which is then transformed into a…
-
2
votes2
answers5264
viewsA: How to import data (.csv) to R while maintaining the original format
You need to define the field separator. In your case, as should be the European/Brazilian csv, the separator is probably ";". variavel=read.table("dados.csv", header=T, dec=",", sep=";") A shortcut…
-
7
votes2
answers3134
viewsQ: calculate difference between two dates in months on the R
How do I calculate the difference between two dates in months on the R? Suppose the two dates: x <- as.Date("2014-01-07") y <- as.Date("2015-03-17") I can easily calculate in seconds, minutes,…
-
2
votes2
answers106
viewsQ: swap commands in the shell
The command python foo.py When called on the terminal, it runs the uploaded python file. Imagine that I have a python file that always needs to be called as follows: xvfb-run python foo.py to work.…
-
5
votes2
answers240
viewsQ: render specific part of a page
I am using the following code to render a web page: import dryscrape # set up a web scraping session sess = dryscrape.Session(base_url = 'http://www.google.com') # we don't need images…
-
5
votes3
answers1163
viewsA: How to separate a string from a certain line of a data.frame and at the same time create more rows?
Create a test basis: b <- data_frame(x = 1:10,y = 1:10, string = rep("123-235-203", 10)) With the following code you can get what you wanted: b %>% separate(string, into = c('s1', 's2', 's3'),…
-
1
votes2
answers182
viewsA: How to install the Krisp package in R?
To install this package the easiest way is to use the devtools. Install the devtools using install.packages("devtools"). In Windows, devtools also asks for the installation of RTools that it is not…
ranswered Daniel Falbel 12,504 -
4
votes1
answer558
viewsA: Create frequency distribution matrix in R (categorical variable VS numeric)
I’m gonna create a data.frame such an example: library(dplyr) base <- data.frame( situacao = rep(c("a ser concluida", "ativa", "concluida"), length.out = 100), qtd_alunos = rep(c(6,7,2,3),…
-
3
votes2
answers1033
viewsQ: Make two Directives have the same $Scope at Angular
I have a directive that generates a button, and when I click this button a counter increases 1. This directive is inserted in two places in my index.html. I wish that when I clicked on one of the…
-
1
votes1
answer164
viewsA: Edit nls or nlsLM function for waste calculation
I made an example that should help you: # dados do modelo DNase1 <- subset(DNase, Run == 1) # ajuste do mdoelo fm1DNase1 <- nls(density ~ SSlogis(log(conc), Asym, xmid, scal), DNase1) #…
ranswered Daniel Falbel 12,504