Posts by Carlos Cinelli • 16,826 points
232 posts
- 
		2 votes3 answers517 viewsA: Comparing matrices of different sizes in RYou can do this with the merge: resultado <- merge(all_sec, portfolio, all=TRUE) # faz o merge das duas matrizes resultado <- apply(resultado, 2, as.character) # transforma em texto de novo… 
- 
		3 votes2 answers389 viewsA: *apply with three-argument functions in RYou can do it directly with apply by separating the three arguments from the line as the parameters. Resultado <- apply(matrix, 1, function(linha) suafuncao(linha[1],linha[2],linha[3])) In this… 
- 
		6 votes1 answer3385 viewsA: Replace specified column values in a matrix (R)In the matrix, when you put the condition ret_matriz==0 you are already making a subset of the whole matrix, rows and columns. So what you have to do in this case is first filter only the columns… 
- 
		4 votes2 answers446 viewsA: Function using as arguments successive values of a column (R)You can use the sapply along with the mapply. Making with a data.frame for example: set.seed(1) dados <- data.frame(x=rnorm(100), y=rnorm(100)) resultados <- sapply(dados, function(col)… 
- 
		1 votes3 answers1031 viewsA: Replace Zero and Infinity values in a matrix (R)You can do it as follows. Below m is your matrix: m[m==0] <- m[row(m)[m== 0], 1] m[is.infinite(m)] <- m[row(m)[is.infinite(m)],1]… 
- 
		6 votes2 answers14389 viewsA: How to filter a data frame?Well, in principle your code is correct, it should subset the data, what may have occurred is some other problem that would only be possible to verify with the specific case. Showing in a sample… ranswered Carlos Cinelli 16,826
- 
		3 votes5 answers14983 viewsA: How to merge multiple dates frames into oneTo illustrate, I will create 3 different example data.frames, one with variable x, other with variable y and the other with the variable z for the same individuals id: ### exemplos #### set.seed(1)… ranswered Carlos Cinelli 16,826
- 
		10 votes2 answers15918 viewsA: Delete lines containing NA in a data frameThere are two solutions. If you want to omit all NA from the data.frame, you can use the function na.omit. For example, suppose a date.frame with two columns, where there are NA’s in both columns.… ranswered Carlos Cinelli 16,826
- 
		2 votes1 answer1401 viewsA: Using function to make a subset and plot with only 1 command (R)There is no R function that will do this automatically, but you can create a function for it. A function that automatically graphs the actions in a certain period would look like this. I’ll use the… 
- 
		3 votes1 answer602 viewsA: Minimum value of a row excluding zero (R)Instead of using the min in the apply, you can apply an anonymous function that selects the minimum only of non-zero numbers (function(x) min(x[x!=0])). For example, in your code it would look like… ranswered Carlos Cinelli 16,826
- 
		15 votes1 answer2705 viewsQ: How to sort array of strings disregarding accents?If I have an array like the following: exemplo = ["Árvore", "Casa", "Computador", "É", "Poste", "Pássaro", "Índia", "Ar", "Ásia"] The exemplo.sort() considers the accentuation of the words to order,… 
- 
		4 votes1 answer1449 viewsA: Changing Missing data <NA> by "something else"The pattern of read.spss is to transform categorical variables into factors (categories, factors). When a variable is a factor, she only accepts what you define as the levels for her. So when you… 
- 
		63 votes2 answers5844 viewsQ: Coffeescript, Typescript and JavascriptAmong Javascript studies, I found in some places many people suggesting the use of Coffeescript or Typescript to accelerate the development process. If I understand correctly, both are like a… 
- 
		4 votes1 answer248 viewsQ: jQuery Unique, difference in Chrome and Firefox?I’m having an unexpected behavior with the uniquejQuery. The following command: var x = [1,2,1,2]; var y = $.unique(x); document.write(y); Chrome results in 1,2 (which is correct), but in Firefox… 
- 
		31 votes6 answers7191 viewsQ: How to put default (default) arguments in a Javascript function?In R this would be very simple: funcao <- function(x=10) return(x) If you call funcao() without arguments the result will be 10. How to do the same in Javascript? And how to know if there are… 
- 
		61 votes1 answer1802 viewsQ: How do prototypes work in Javascript?It seems to me that the concept of prototype is fundamental in Javascript and even in several places I have read that is one of its strengths. However, this does not seem such a simple concept to… 
- 
		5 votes3 answers1021 viewsA: How to add object properties in Javascript?I got an even more synthetic shape using the jFunk and Underscore. totais = _.chain(jF("*[Regiao]", vendas).get()).groupBy('Regiao').map(function(g, key) { return { type: key, val:… javascriptanswered Carlos Cinelli 16,826
- 
		9 votes3 answers1021 viewsQ: How to add object properties in Javascript?Suppose an object as follows: vendas = { obs1:{ Venda1:{Regiao:"Norte", Valor: 200}, Venda2:{Regiao:"Sul", Valor:100} }, obs2:{ Venda1:{Regiao:"Norte", Valor: 50}, Venda2:{Regiao:"Sul", Valor:20} }… javascriptasked Carlos Cinelli 16,826
- 
		10 votes1 answer8713 viewsA: How to know the "size" (amount of properties/attributes) of an object in Javascript?One solution I found was to use the function Object.keys() together with length: Object.keys(vendas).length //2, isto é Obs1 e Obs2 Object.keys(vendas.obs1).length //2, isto é, Venda1 e Venda2 In… 
- 
		8 votes1 answer8713 viewsQ: How to know the "size" (amount of properties/attributes) of an object in Javascript?Suppose an object as follows: vendas = { obs1:{ Venda1:{Regiao:"Norte", Valor: 200}, Venda2:{Regiao:"Sul", Valor:100} }, obs2:{ Venda1:{Regiao:"Norte", Valor: 50}, Venda2:{Regiao:"Sul", Valor:20} }… 
- 
		8 votes3 answers954 viewsQ: How to list variables defined within a Javascript scope?How do I know/list which variables have already been defined within a scope, whether global or local? For example if I set var x, y, z = 10, the result of a possible command to list the variables… 
- 
		21 votes2 answers8317 viewsQ: How to delete a variable in Javascript?I program in other languages, I’m still starting in Javascript. I was testing the mgibsonbr response code to this question and found a difficulty that may be trivial. If I define var x = 0 in the… 
- 
		6 votes1 answer5017 viewsQ: Export formatted R to Excel dataSuppose a data.frame like this: tabela <- structure(list(vendedor = structure(1:4, .Label = c("A", "B", "C", "D"), class = "factor"), Total = c(3300, 440, 1020, 200)), .Names = c("vendedor",… 
- 
		12 votes1 answer1619 viewsA: The order function in RThe function order does not return the ordered original vector, but returns a vector with the positions so that x stay in ascending order. Thus, to get back the vectorx ordered, you have to put… 
- 
		68 votes3 answers19793 viewsQ: Functional Programming and Object-Oriented Programming. What are they and what are their main differences?What are and what are the main differences between Functional Programming and Object-Oriented Programming? 
- 
		38 votes3 answers10198 viewsQ: What are lexical scope and dynamic scope and what are their main differences?What are lexical scope and dynamic scope and what are their main differences? asked Carlos Cinelli 16,826
- 
		1 votes2 answers412 viewsA: Connecting dots to the 3d regression lineMaking with the base charts: First, run the model: model <- loess(Income ~Education + Seniority, data=Income2) Create sequences of x and y: x <-range(Income2$Education) x <- seq(x[1], x[2],… 
- 
		3 votes2 answers412 viewsQ: Connecting dots to the 3d regression lineThis is the 3d version of this other question. Consider the following data: Income2<-structure(list(X = 1:30, Education = c(21.5862068965517, 18.2758620689655, 12.0689655172414, 17.0344827586207,… 
- 
		2 votes2 answers969 viewsA: How to do an optimization with inequality restriction?In this example, you can define in the function that if the values exceed the constraint, it receives infinite value: fr <- function(x){ if(x[1]+4*x[2] > 3 ){value<-Inf} else{ x1 <- x[1]… 
- 
		3 votes2 answers443 viewsA: Reorder categories in a data frameBy creating the factor, you can tell the R what order you want for the levels: df$categorias <- factor(df$categorias, levels=c("Muito baixa","Baixa","Média","Alta","Muito alta") Upshot:… 
- 
		4 votes1 answer497 viewsA: How to remove unused categories (levels) in the databaseYou can use the function droplevels subdf <- droplevels(subset(df, valores <= 3)) Upshot: levels(subdf$categorias) [1] "A" "B" "C" The advantage is that it works for more than one variable… 
- 
		3 votes2 answers178 viewsA: Collapsing texts in a single line in a databaseYou can do with the dplyr using the option collapse of paste: library(dplyr) tabela2<-tabela %>% group_by(ano) %>% summarise(quantidade = n(), nomes = paste(nome,collapse=", ")) Out of… ranswered Carlos Cinelli 16,826
- 
		3 votes2 answers302 viewsA: Connecting the points to the regression lineIn the ggplot2 you can use the geom_segment to draw lines between the points and the values predicted by the model. But first you need to run the model "outside" of the gpplot2 to obtain the… 
- 
		7 votes2 answers302 viewsQ: Connecting the points to the regression lineSuppose the following database: Income <- structure(list(X = 1:30, Education = c(10, 10.4013377926421, 10.8428093645485, 11.2441471571906, 11.6454849498328, 12.0869565217391, 12.4882943143813,… 
- 
		3 votes3 answers693 viewsA: How to know the amount of NA in each variable?You can use the function colwise of plyr to make its function applicable to data frame columns: Defining the function: library(plyr) quantos.na <- colwise(function(x) sum(is.na(x))) Applying the… ranswered Carlos Cinelli 16,826
- 
		3 votes1 answer322 viewsA: Creating a matrix with variables with different correlations in R?Peter, First of all, you have an extra loop in your code. Note that you are generating an array 1000 by 5. Then you start a loop by k (correlations), then by i (columns), and then by j (lines). See… 
- 
		1 votes3 answers830 viewsA: Loop in R with indexing and matrixYou don’t need to use for in this case. Follows a solution with sapply: y <- function(x,z) 5+3*x+4*z set.seed(1) x <- rnorm(1000, mean = 0, sd = 1) z <- c(1, 2, 3, 4, 5)… 
- 
		2 votes1 answer168 viewsA: How to vary parameters of an equation?Suppose you have two parameters a, two parameters b and a value of x: a<- c(1,2) b <-c(3,4) x <- 10 You want to find all the y possible with the four combinations of the parameters, given… 
- 
		1 votes2 answers97 viewsA: Subtracting matrices with different argumentsYour object x has dimensions 4x1x1 and the object y only 4x1, you can not subtract objects with different dimensions. If you put the dimensions 4x1 in x works: x<-array(1:4,c(4,1))… 
- 
		2 votes2 answers6494 viewsA: How to create inverse matrix in RFelipe, it is not very clear what the problem is, ideally you would put the code with the (possible) error. But here are some ways to reverse a matrix in the R: Generating an example database: ###… 
- 
		2 votes1 answer941 viewsA: How to create invertible random matrices in RI’m not sure I understand what the problem was, but it’s almost impossible (X'X) do not have inverse in this case, because you will generate 1000 random numbers and only has 2 variables. So I think… 
- 
		4 votes1 answer10722 viewsA: How to plot a line chart with different colors depending on the value?A partial "solution" would be to generate a Spline with many points (about 100 for example) that the colors would be less likely to be in the wrong place. But this solution can spend a lot of memory… 
- 
		5 votes1 answer159 viewsA: How to find a table by category with minimum observations?You can take only the attribute names of which.min: a<-c(rep("agosto",3),rep("janeiro",4),rep("maio",6)) tabela<-table(a) names(which.min(tabela)) [1] "agosto" To better understand, when you… ranswered Carlos Cinelli 16,826
- 
		10 votes2 answers10996 viewsA: Error while converting numbers. How to convert factors to numbers?In the R, the standard behavior of data.frame is to turn texts into factors. This can generate unexpected results when numbers, during the data import/manipulation process, are misinterpreted as… 
- 
		13 votes2 answers10996 viewsQ: Error while converting numbers. How to convert factors to numbers?In the following example: dados <- data.frame(x=c("11", "10", "20", "15"), y=c("25", "30", "35", "40")) dados x y 1 11 25 2 10 30 3 20 35 4 15 40 When trying to transform the variable x number,… 
- 
		27 votes3 answers1491 viewsQ: Strategies to prevent software regressionWhat programming strategies/best practices can be adopted to minimize the risk of software regression? Questions that can help guide: which "pranks"/bad practices can fool the programmer and leave a… 
- 
		1 votes1 answer462 viewsA: How to set up Sublimerepl for Anaconda (Python)?Inside the folder Packages/User create the folders SublimeREPL/config/Python/ and the file Main.sublime-menu with the following content (format json), where on the line "cmd" you will exchange the… 
- 
		0 votes1 answer462 viewsQ: How to set up Sublimerepl for Anaconda (Python)?I installed Sublimerepl for execute the line or selection directly from Sublime Text 2. Meanwhile, in the same way as in this previous problem, Sublimerepl is not using Anaconda but another version… 
- 
		4 votes1 answer4059 viewsQ: How to run the current line in Sublime Text 2? Or, IDE that allows running the current line in PythonIn the rstudio (IDE to r) it is possible to run the current line instead of the entire file using ctrl (or cmd) + enter. This is very useful for when you want to test just one piece of code separate… 
- 
		15 votes1 answer6546 viewsQ: Naming conventions for variables and functions in Python?In the R, there is a lot of freedom and variety in function names between packages. Dot names (get.this), names in camel (getThis), names in snake_case (get_this). This has its pros and cons. Why,…