Posts by Carlos Cinelli • 16,826 points
232 posts
-
2
votes1
answer449
viewsQ: Build of Sublime Text 2 gives different result than ipython on the command line
I’m trying to migrate to Sublime Text 2 and do everything without leaving the program. Before I ran a script with the following line: from scipy.stats import beta, bernoulli, gaussian_kde In the…
-
8
votes3
answers1784
viewsA: How to format a table (data.frame) with pdf publishing quality (latex) in R?
A package that I think is fantastic is the tables. It is very flexible. For example, arranging the tabela in three different ways: library(tables) tabela1 <-…
-
6
votes1
answer829
viewsQ: R workflow: strategies to organize a data analysis project
Based on this question from the SOEN, ask: What strategies do you recommend to organize a data analysis project on R? The project usually involves the following steps (not necessarily in that…
-
9
votes3
answers1784
viewsQ: How to format a table (data.frame) with pdf publishing quality (latex) in R?
Suppose the following table: tabela <- structure(list(Sexo = structure(c(1L, 1L, 2L, 2L), .Label = c("Homem", "Mulher"), class = "factor"), Grupo = structure(c(1L, 2L, 1L, 2L), .Label = c("A",…
-
5
votes3
answers2755
viewsA: How to assign the results of a function that returns a list of objects?
The solution that I found simpler and more interesting was to define another binary operator (such as,%<-% signaling a modification of the <- ) apply the assign in multiple arguments with the…
-
8
votes1
answer2292
viewsA: How to create objects (variables) with different names within a loop?
You can use the function assign for that reason. set.seed(1) for (i in 1:3){ nome <- paste0("dados", i) assign(nome, runif(10,1, 20)) } The first part nome <- paste0("dados", i) creates the…
-
9
votes1
answer339
viewsA: How to declare constants in R
You can use the function lockBinding: For example: pi <- base::pi lockBinding("pi", globalenv()) This will lock the variable pi in the constant base::pi internal in the global Environment. So if…
-
6
votes2
answers4567
viewsA: How to turn a string into Date format in R?
An interesting package is the lubridate (CRAN). In your case you will use the function mdy() (Month, day, year), and say that the date is in English: library(lubridate) dados$DataAno <-…
-
3
votes3
answers3281
viewsA: Sublime Text 2 - python error message /usr/bin/python: can’t find '__main__' module in ''
In general this problem can be solved by saving the script before running it.
-
3
votes3
answers3281
viewsQ: Sublime Text 2 - python error message /usr/bin/python: can’t find '__main__' module in ''
Using the cmd+B (Tools -> Build) in Sublime Text 2, the following error message appears: /usr/bin/python: can't find '__main__' module in '' How to solve this?…
-
16
votes2
answers4769
viewsQ: Apply, sapply, mapply, lapply, vapply,rapply, tapply, replicate, Aggregate, by and correlates in R. When and how to use?
What is the difference between the functions apply, sapply, mapply, lapply, vapply, rapply, tapply, replicate, aggregate, by and correlates in the R? When and how to use each of them? There are…
-
6
votes2
answers2443
viewsA: How to smooth a curve in R
Another very quick way to adjust a smoothed line is to use the smoothed line itself geom_smooth of ggplot2: library(ggplot2) ggplot(data = df, aes(x, y)) + scale_x_log10() + geom_point() +…
-
20
votes5
answers12797
viewsA: How to consolidate (aggregate or group) the values in a database?
Hadley recently created the dplyr, a much faster version with more intuitive syntax than the plyr. (links for the cran and to the announcement on Rstudio’s blog) In the dplyr would look like this…
-
5
votes1
answer648
viewsA: How to read a table missing or poorly configured elements?
One way to solve the problem is by putting the argument fill=TRUE in the read.table: tabela <- read.table(text=texto, fill=TRUE) tabela V1 V2 V3 1 a b c 2 e f 3 g h i…
-
5
votes1
answer648
viewsQ: How to read a table missing or poorly configured elements?
Suppose a text table like the following: texto <- "a b c e f g h i" When I use the read.table command, the following error occurs:: tabela <- read.table(text=texto) Error in scan(file, what,…
-
7
votes3
answers3287
viewsQ: How to put the regression equation on a graph?
On another question saw how to put the regression line on a graph. But, how to put the regression equation in the graph? For example: Or…
-
3
votes1
answer5683
viewsA: How to run a R script from the Windows command line?
You can use the argument BATCH of R CMD or the Rscript: R CMD BATCH teste.R or Rscript teste.R
-
4
votes2
answers355
viewsA: Negative variance in R? Floating point error propagation
Complementing Marcos Banik’s response. A floating point number of type double (64 bits) can be roughly summarized in 3 parts: Floating point type double: signal (1bit), order of magnitude (11 bits)…
-
11
votes2
answers355
viewsQ: Negative variance in R? Floating point error propagation
Suppose the following formula to calculate the variance: variancia <- function(x) { n <- length(x) (1/(n^2-n))*(n*(sum(x^2))-(sum(x)^2)) } Note that it is equivalent to the function var in…
-
12
votes4
answers1519
viewsQ: Why is 0.1 + 0.05 not equal to 0.15? What solutions can be used in R?
In the R: 0.1+0.05 == 0.15 [1] FALSE Why does this happen? How to get around the situation (functions and packages to handle floating points)? Editing: This other question already contains general…
-
4
votes1
answer4682
viewsA: How to create a Plot with 2 superimposed histograms?
It is possible to do with the base functions by placing semi-transparent colors: h1<-hist(grupo1) h2<- hist(grupo2) plot(h1, col=rgb(0,0,1,1/4), main = "Histogramas", xlab = "x", ylim…
-
7
votes2
answers637
viewsA: How to make a conditional y~x graph for each data.frame factor?
One way is to use the function coplot: coplot(y~x |w, data=dados) It is also possible to do with the ggplot2 using facet_wrap: library(ggplot2) ggplot(data=dados, aes(y=y, x=x))+…
-
8
votes2
answers637
viewsQ: How to make a conditional y~x graph for each data.frame factor?
Suppose a data.frame like the following: set.seed(1) dados <- data.frame(w=rep(c("A", "B", "C", "D"), 50), y= rnorm(200), x=rnorm(200), stringsAsFactors=FALSE) How to create a chart y~x separated…
-
5
votes3
answers530
viewsQ: How to select all data.frame variables at once for a regression?
Suppose the following data.frame: set.seed(1) dados <- data.frame(y=rnorm(100), x1=rnorm(100), x2=rnorm(100), x3=rnorm(100), x4=rnorm(100)) If I want to run a y regression against x1...xn, I can…
-
6
votes1
answer3442
viewsA: How to plot the estimated logistic regression model
With the function plot base, you would have to sort the data before: plot(saldo,y) lines(data$saldo[order(data$saldo)], default.glm$fitted[order(data$saldo)], type="l", col="red") With the gpplot2…
-
8
votes4
answers8280
viewsA: How to put the regression line on a graph?
You can use the function abline along with coef to extract model coefficients and plot the line: plot(y~x) abline(coef(modelo))…
-
9
votes4
answers8280
viewsQ: How to put the regression line on a graph?
Suppose a linear regression model like the following: set.seed(1) x <- rnorm(100) y <- rnorm(100) + 2*x +10 modelo <- lm(y~x) If I plot y against x, how do I include the regression line in…
-
2
votes2
answers195
viewsA: How to load all functions from a folder?
It might be interesting to create a function that reads files from an arbitrary folder, so you can reuse the code several times. To make the code cleaner, you can use the sapply instead of a loop…
-
5
votes3
answers17265
viewsA: How to sort a column-by-column data.frame in R?
You can use the function order. To sort the date.frame by column z in descending order, for example: dados[order(dados$z, decreasing=TRUE),] w x y z 4 B C 1.5952808 0.7383247 3 A D -0.8356286…
-
6
votes3
answers17265
viewsQ: How to sort a column-by-column data.frame in R?
Suppose a data.frame with numerical values and strings: set.seed(1) dados <- data.frame(w=rep(c("A", "B"), 2), x= rep(c("D", "C"), 2), y= rnorm(4), z=rnorm(4), stringsAsFactors=FALSE) dados w x y…
-
6
votes6
answers31940
viewsA: How to remove a data.frame column in R?
There are many ways to do this. The simplest is to assign NULL to the column, for example to remove the column x: dados$x <- NULL head(dados) y z w 1 -0.6264538 0.4094018 0.8936737 2 0.1836433…
ranswered Carlos Cinelli 16,826 -
7
votes6
answers31940
viewsQ: How to remove a data.frame column in R?
Suppose a generic date.frame, such as: set.seed(1) dados <- data.frame(y=rnorm(100), x= rnorm(100), z=rnorm(100), w=rnorm(100)) head(dados) y x z w 1 -0.6264538 -0.62036668 0.4094018 0.8936737 2…
rasked Carlos Cinelli 16,826