Posts by Carlos Cinelli • 16,826 points
232 posts
-
4
votes1
answer399
viewsA: In R, how to define a function with undefined arguments of the same nature
First by answering your specific problem, based on your comment. The example function you put up handles "non-standard" Evaluation, which makes it harder to program with functional (such as the…
ranswered Carlos Cinelli 16,826 -
7
votes2
answers3449
viewsA: In R, count words and enter a line break
You can do it using regular Expression: library(stringr) legenda <- c("Gostei muito da atuação" , "Gostei pouco da atuação", "Nem gostei e nem desgostei") legenda2 <- str_replace_all(string =…
ranswered Carlos Cinelli 16,826 -
2
votes1
answer259
viewsA: Select first lines depending on group efficiently
A form with the dplyr would be: base2 <- merge(base, qtds) base2 %>% group_by(grupo) %>% arrange(score) %>% slice(1:unique(qtd)) Source: local data frame [10 x 3] Groups: grupo grupo…
-
2
votes1
answer1427
viewsA: How to change x-axis values (not the axis title) in "seasonplot"?
Looking at the function code seasonplot package forecast, Note that these values are not as arguments but inside the code. Thus, one solution would be for you to change those parts of the code that…
ranswered Carlos Cinelli 16,826 -
5
votes2
answers202
viewsA: data.frame hierarchy for nested list in R
A solution using recursion would be: rec_split <- function(df){ if(ncol(df) == 2){ l <- split(df[[2]], df[[1]]) }else{ l <- split(df[-1], df[[1]]) lapply(l, rec_split) } } Example:…
ranswered Carlos Cinelli 16,826 -
10
votes1
answer573
viewsA: Dispersion graph with marginal histograms
Yeah, there’s a way. In the background it is only a matter of tidying the layout of the graphics on the screen. If you access the help of the function layout (?layout), there is an example of a…
-
2
votes1
answer222
viewsA: Is it possible to modify a value of an array with assign() in R?
You can’t do it directly with the assign. As stated in the aid: assign does not Dispatch assignment methods, so it cannot be used to set Elements of Vectors, Names, Attributes, etc. If you want to…
ranswered Carlos Cinelli 16,826 -
2
votes2
answers106
viewsA: Data window moving in time (t)
When I spin: window(periodo,end=c(2007,11+3)) Should get as given end February 2008 Answering this point of adding months: To get what you want in this operation, you have to operate with dates.…
-
2
votes1
answer161
viewsA: Size of a predefined range within a vector
Well, as Luiz Vieira said, in this case you can simply calculate. Anyway, just to not leave without an answer, you can subset your vector first vetor[2:6] and then count how many elements the vector…
ranswered Carlos Cinelli 16,826 -
27
votes1
answer2607
viewsQ: What are the main differences between a quantum computer and a conventional computer?
They say quantum computing will revolutionize computation if it is implemented successfully. Why is that? What are the main differences between a quantum computer and a conventional computer that…
computer-theoryasked Carlos Cinelli 16,826 -
2
votes1
answer435
viewsA: Data.frame for ts daily data in R
Diogo, the problem is that your values are not as a number, but as factor (factors). This is happening at the time of reading the database because the R is interpreting your data as text (and not as…
ranswered Carlos Cinelli 16,826 -
1
votes3
answers192
viewsA: Calculate average distance travelled with single tapply
Two other ways of doing: Using tapply, like Nishimura, but with the cut. The cool thing about the cut is that it extends easily to more than two conditions, just increase the breaks and the Abels:…
ranswered Carlos Cinelli 16,826 -
1
votes1
answer196
viewsA: Automatically check quantitative variables in R
Yes, you will use the sapply with the function is.numeric. In summary, the sapply will apply to each variable (column) of your data.frame the function is.numeric to know if that is a numeric…
-
4
votes1
answer2526
viewsA: Convert qualitative numeric variables into factors in R
Changing my answer, actually there is a previous problem, you would have to use the lapply instead of apply, because apparently the apply will play everything to an array and convert to character…
ranswered Carlos Cinelli 16,826 -
5
votes1
answer1707
viewsA: How to Label Distributions in ggplot
You have to put the colors as an aesthetic (within the aes). And if you want to use these specific colors ('red', 'deepskyblue1', 'goldenrod2'), they have to be passed as parameters in the…
-
3
votes2
answers239
viewsA: How to save an error message to a string in R?
The function geterrmessage() returns the last error message. If you don’t want anything very structured or efficient, you can do a quick hack that accumulates all the error messages in one vector.…
ranswered Carlos Cinelli 16,826 -
1
votes1
answer64
viewsA: How to use xmlSApply with separator?
A way would be: nos <- lapply(xmlChildren(rootNode), function(x) xmlApply(x, xmlValue)) nos[[1]] $name [1] "Belgian Waffles" $price [1] "$5.95" $description [1] "Two of our famous Belgian Waffles…
-
2
votes2
answers314
viewsA: Presentations (slidify and Rpres) with Latin characters (ç, ã, é...) corrupted
Windows default is latin1 (iso 8859-1) and Rpres is generating the HTML file as UTF-8. When you save the file with the UTF-8 encoding, then the HTML and the source file will be equal and the…
ranswered Carlos Cinelli 16,826 -
1
votes2
answers170
viewsA: In R, using dplyr, create a new matrix
You can put the condition inside the sum: data %>% group_by(zona) %>%summarise(votos_zona = sum(votos), votosA = sum(votos[candidato=="A"]), votosB = sum(votos[candidato=="B"])) Source: local…
-
4
votes3
answers1955
viewsA: Pre-process large text files in R
I made a partial solution (which should not be the most efficient) that reads the file in Chunks and goes saving in a csv, already with the decimal as a point instead of a comma. Then you just set…
-
2
votes2
answers823
viewsA: Dplyr and gsub: how to replace parts of one column with another
You can’t use it directly like this because the gsub is not vectored, so only the first element of Replacement will be used, replacing everything with sp. What the mapplyis doing is vectorize the…
-
3
votes1
answer5913
viewsQ: What is the master theorem (master Theorem)?
What is the master theorem (master Theorem)? What is its importance in the complexity analysis of algorithms?
algorithmasked Carlos Cinelli 16,826 -
17
votes4
answers1143
viewsA: Add rownames as column using dplyr
Heed: update on magrittr 1.5 From the magrittr 1.5, point (.) of the operator %>% works with nested calls. Thus, it correctly replaces the point within row.names(.) and now the example works…
-
1
votes0
answers217
viewsQ: Notepad++: how not to escape the backslash
This is perhaps a trivial doubt. But how to do for Notepad++ nay escape the backslash \ ? For example, if I save a . csv file with the content A\032 Notepad++ escapes backslash and interprets part…
-
20
votes2
answers13934
viewsQ: What is a complete NP problem?
What is a complete NP problem? What examples can be given to illustrate a complete NP problem? Why these problems are considered important?
algorithmasked Carlos Cinelli 16,826 -
3
votes1
answer278
viewsA: I get NA when I convert Character to time (Posixlt)
At the help of as.POSIXlt, there is the following passage that highlights that the conversion of date time formats needs a time-zone and will validate this time and that this can cause problems in…
-
5
votes4
answers8289
viewsQ: How to put different graphics of ggplot2, separately but on the same screen?
Suppose I have these 4 charts: p1 <- ggplot(mtcars, aes(mpg, cyl)) + geom_point() p2 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line() p3 <- ggplot(mtcars, aes(mpg, cyl)) +…
-
3
votes2
answers1789
viewsA: A: What objects does the Names() attribute apply to?
Matrices may have names also. Almost everything can have names in the R. The simplest way to think about these issues is the following: the names refers to the name of the element of the object. AND…
ranswered Carlos Cinelli 16,826 -
5
votes1
answer334
viewsQ: How to create immutable objects in Python?
Based on this question, Is it possible to create your own immutable objects/classes in Python, in the sense that by default, this object of mine will be copied when passed as a function argument? If…
pythonasked Carlos Cinelli 16,826 -
5
votes2
answers453
viewsQ: Make list changes to be local in Python?
In Python as I do for the function below change the feather list locally, that is, only in the scope of the function f without changing x set outside of f? x = [1, 2] def f(x): x[1] = "novo valor"…
pythonasked Carlos Cinelli 16,826 -
32
votes2
answers2760
viewsQ: What is a memoization?
What is memoization? Under what circumstances can it be useful and how can it be used? (If possible, illustrate with a simple example)
-
6
votes2
answers1533
viewsA: What does <<- mean in R?
Complementing Athos' response, here are some examples where the use of <<- can be useful to change variables within the function environment itself without changing global variables:…
ranswered Carlos Cinelli 16,826 -
8
votes2
answers1533
viewsQ: What does <<- mean in R?
What the operator means <<- in R, what are your differences with respect to <- and under what circumstances it may be useful?
rasked Carlos Cinelli 16,826 -
6
votes5
answers12797
viewsA: How to consolidate (aggregate or group) the values in a database?
You can also use tapply: Per salesman: with(df, tapply(vendas, vendedor, sum)) A B C D 3300 440 1020 200 By seller and region: with(df, tapply(vendas, list(vendedor, regiao), sum)) Leste Norte Sul A…
-
78
votes2
answers35446
viewsQ: What is the complexity of an algorithm?
What is the complexity of an algorithm? And what are the ways to measure it? (Big O, Big Theta...)
-
4
votes1
answer287
viewsA: loop objects
Ennio, you cannot create object names that start with a number. 2012ocup10 is not a valid name for an R object. You have to at least put a letter or something like that before in this name, for…
ranswered Carlos Cinelli 16,826 -
5
votes2
answers441
viewsA: How to make a "rbind" in tables of an SQL base from R?
It should not be the only way to do, but try putting the command CREATE TABLE in the query itself: dbGetQuery(con, 'CREATE TABLE TabelaC AS SELECT * FROM TabelaA UNION ALL SELECT * FROM TabelaB' )…
-
3
votes1
answer147
viewsA: How to import data from clibboard to a data-frame in R
Yes, it is possible. But there are differences in Windows and Mac: On Windows: You can use the function readClipboard(). x <- readClipboard() This will paste the Clipboard data as text in x. If…
ranswered Carlos Cinelli 16,826 -
7
votes4
answers16549
viewsA: How to include columns in a data.frame?
Maybe the simplest way is: df$z <- c(1,2) df x y z 1 a 1 1 2 b 2 2 One way that is not very well known, but it is interesting to know that it exists, is by using the function within(). Creating a…
ranswered Carlos Cinelli 16,826 -
3
votes4
answers16549
viewsQ: How to include columns in a data.frame?
Consider the following data.frame: df <- data.frame(x=c("a","b"), y=c(1,2)) How to include a new column, say, z = c(1,2)?
rasked Carlos Cinelli 16,826 -
5
votes3
answers8030
viewsA: How to include lines in a data.frame?
One way to do this is to create a new data.frame with the line you want to include and use the function rbind: linha <- data.frame(x="c", y=3) df <- rbind(df, linha) df x y 1 a 1 2 b 2 3 c 3…
ranswered Carlos Cinelli 16,826 -
5
votes3
answers8030
viewsQ: How to include lines in a data.frame?
Consider the following data.frame: df <- data.frame(x=c("a","b"), y=c(1,2)) How to include a new line with x="c" and y=3 in data.frame?…
rasked Carlos Cinelli 16,826 -
13
votes2
answers1065
viewsQ: Object orientation in R: S3, S4 and Reference Class
R has, among others, three main forms of object orientation: S3; S4; and, Reference Classes. What are the main differences between the three methods? And how to implement them (preferably provide a…
-
9
votes1
answer266
viewsA: How to create Needles chart in R?
If you mean the Needle Plot, that is, plot the variables as sliders coming out of the zero (or minimum) axis, just put the parameter type = h in the plot. For example: with(economics, plot(date,…
-
32
votes4
answers5405
viewsQ: Strategies to analyze very large databases in R (that do not fit in RAM)
Suppose I have a huge database that doesn’t fit in RAM. What strategies to analyze this database in R, since I cannot load it entirely into memory? PS: The question isn’t just about how to get R to…
-
2
votes2
answers4318
viewsA: How to save or reserve a list of objects? (R)
Well, you can either store arrays in a list or an array. With the array the idea would be to use a three-dimensional array. The third dimension would be an indexer of the matrices. However, for this…
-
6
votes2
answers604
viewsA: Transforming place into zip code
Google provides the zip code in some cases and there are already packages with functions to access the google API, the ggmap is one of them. For example: library(ggmap) end <- geocode("Avenida…
-
5
votes2
answers381
viewsA: Create new matrix from a fairly large first efficiently
To complement, I also leave a reply with the data table.. Both dplyr and data.table are extremely fast for large databases. dplyr is, in my opinion, more intuitive and data.table is more flexible.…
-
7
votes2
answers1318
viewsQ: Calling C++ from Javascript?
I have a simulation in C++. I passed this simulation to Javascript so that the user could play with it in the browser, but it became much slower. If possible, therefore, I would like to call the…
-
1
votes1
answer102
viewsA: Return a Matrix that has all possible column multiplications of another Matrix (R)
If I understand correctly, you want all combinations of the multiplication element to the element of the columns 1 to 3, resulting in 9 combinations (three of them redundant) i.e.: (1,1), (1,2),…