Posts by Carlos Cinelli • 16,826 points
232 posts
-
3
votes1
answer365
viewsA: Extract elements from a List
To access the matrix element 2 in the list first you have to access the matrix -- a[[1]] because it is the first element of the list -- and then access the second element of the matrix --…
ranswered Carlos Cinelli 16,826 -
9
votes2
answers2457
viewsA: How to exclude element from a list in R
Another way that serves for any R object, and not just lists, is not to select the element you want to remove and assign back to the original object: x <- x[-2]
ranswered Carlos Cinelli 16,826 -
7
votes1
answer392
viewsA: How to use %<> % operator in R
That operator belongs to the package magrittr and it serves for you to pass an object to a function while modifying the object you passed. For example, suppose the following x in text format:…
-
4
votes1
answer105
viewsA: Build Data Frame with "get" Function
The function get() is not suitable for what you want to do. It only takes one object at a time. To pick up more than one object you would have to use the function mget, but still doesn’t make much…
-
5
votes2
answers1742
viewsA: Processing time of functions
Your test example is very fast, so Ofiling will have a hard time driving a lot of things. Let’s generate a larger test vector to make the test take longer: teste = rnorm(10000000) Basic Profiling of…
-
7
votes2
answers305
viewsA: Turn positive values into negative values in a data.frame based on one condition in another column (R)
Your code would also work normally, what you missed to do was to put the same logical condition within -Qtd[]. Correcting: within(df, Qtd[op == 'V'] <- -Qtd[op == 'V']) Qtd op 1 -100 V 2 200 C 3…
-
4
votes1
answer723
viewsA: Apply a function using some columns of all rows of a Dataframe (r)
You could use the apply in this case yes, the problem in your current code is as follows: apply the x that you are passing on to your function is no longer a data.frame rather a vector. Thus,…
-
5
votes1
answer141
viewsA: Overlay words on the R software graph
Unfortunately there is no simple solution for this. In general it is necessary to adjust the positions manually or using some algorithm. Depending on the package you are using this possibility even…
-
1
votes1
answer179
viewsA: information criteria Akaike temporal series
The error is actually simple, it is an indexing error in the loop. See that in the first loop, in the rows where you update the matrices, you are using rep instead of t. I mean, on the lines:…
-
3
votes1
answer3317
viewsA: How to turn rows into columns, when the number of rows is variable in R
William, what you want to do is pass the date.frame format long for the format wide. Constructing the variable col as the ID change would be as follows. Playing your original date.frame: df <-…
ranswered Carlos Cinelli 16,826 -
2
votes2
answers502
viewsA: Time Series autocorrelation
You are selecting only one element of the series. In correlation, for example, what you have to do is the following -- select all but the last and then all but the first. cor(y1[-length(y1)],…
-
3
votes1
answer419
viewsA: How do I access a position in a character array?
The function strsplit returns a list, so the vector res is a list, see: res <- "ACDEDEAEDCED" res <- strsplit(res,"", FALSE) res [[1]] [1] "A" "C" "D" "E" "D" "E" "A" "E" "D" "C" "E" "D" If…
ranswered Carlos Cinelli 16,826 -
1
votes2
answers878
viewsA: loop in correlation matrix in R
Assuming you want to use loops (to train or for another reason, because in this case you don’t need to use loops), you can save the results in a list. Recreating your data (with set.seed() for…
ranswered Carlos Cinelli 16,826 -
3
votes2
answers669
viewsA: Basic SAS ProcMeans for R
The function summary already gives you enough stuff, as minimum, first quartile, median, average, third quartile and maximum for each variable. For example: summary(mtcars) mpg cyl disp hp drat wt…
-
4
votes1
answer1126
viewsA: Filling out data.frame from another date.frame
What you want to do is actually move the data frame to the format wide ("pivotear"). You can do this with the function dcast package reshape2: library(reshape2) dcast(a, date~term, value.var =…
ranswered Carlos Cinelli 16,826 -
4
votes2
answers1558
viewsA: How to know the day of the week in R?
To format dates in general you can use the function format(). The functions weekdays() and months(), for example, in the background are Wrappers for format(). To extract the day of the week you will…
ranswered Carlos Cinelli 16,826 -
3
votes4
answers2494
viewsA: How to join observations of tables that have a different set of variables in R?
You can also use the function rbindlist package data.table, with the option fill = TRUE: library(data.table) rbindlist(list(df1,df2), fill = TRUE)…
ranswered Carlos Cinelli 16,826 -
6
votes1
answer272
viewsA: How to know the arguments contained in '...' in a function in R?
A simple way to capture the arguments is to put them in a list: imprimeArgumentos <- function(...) { args <- list(...) print(args) } imprimeArgumentos(x=3, z=NULL, y=3) #> $x #> [1] 3…
ranswered Carlos Cinelli 16,826 -
8
votes3
answers2490
viewsQ: What are the differences between pointer and reference in c++?
This question is a specific version for c++ of the question: What is the difference between pointer and reference? In practice, what are the differences between a pointer and a reference in C++? I…
-
4
votes2
answers1104
viewsA: Show an object/variable with different names in R?
You can also use the function get: for (i in 1:length(x)) { ## Nome da variável: nomevar <- paste0("Var_", i) var <- x[i] + 2 assign(nomevar, var) print(get(nomevar)) } [1] 3 [1] 4 [1] 5 [1] 6…
-
3
votes3
answers108
viewsA: How can I restructure information contained in a list object into two columns?
You can also use the function melt package reshape2. Using the same Molx data: li <- list(a = 1:3, b = 4:8, c = 9:10) library(reshape2) melt(li) value L1 1 1 a 2 2 a 3 3 a 4 4 b 5 5 b 6 6 b 7 7 b…
-
5
votes1
answer97
viewsA: Ggplot of a Data.frame in separate graphs
In this case you can include one more element to your chart specifying that variable ind will generate different facets, that is to say, facet_wrap(~ind): ggplot(data=df, aes(x=df$m, y=df$X2,…
-
5
votes1
answer876
viewsA: How to build an executable/installer from a routine written in R?
Currently this is not possible. That is, it is not possible to create a standalone executable where one does not need to have R installed on their machine. However, there are some alternatives, as…
ranswered Carlos Cinelli 16,826 -
6
votes1
answer1608
viewsA: Create a blank data.frame to receive results?
The way you’re doing now will bring trouble. See that when you do: res.ad <- c("Gumbel", 0.9105) You’re turning the number into text: res.ad [1] "Gumbel" "0.9105" So in this part the ideal would…
ranswered Carlos Cinelli 16,826 -
3
votes2
answers66
viewsA: How can I find out which packages/libraries belong to the functions of a routine written in R?
You can start by trying to find help for packages installed on your machine, for example ??"nome_da_funcao". However, this will only work if (i) the package is installed and (ii) the function has…
-
7
votes2
answers200
viewsA: How do you store a chart as a variable and export it later?
I would recommend using either the ggplot2 or the lattice, both work so that the graphics are R objects, which can be modified, saved etc in an easy way. For example: library(ggplot2) grafico_ggplot…
-
3
votes4
answers1242
viewsA: How can I save a list or export a list object in R?
You can also save in format .rds, he is more flexible than the .rda in the sense that you can upload the list with the name you find most appropriate. # salva lista x no arquivo x.rds saveRDS(x,…
-
4
votes1
answer57
viewsA: bayesQR package Error message
The message is saying that the arguments quantile, alasso and ndraw are not part of the function bayesQR. What this means is that you are using another function called bayesQR that’s not from the…
ranswered Carlos Cinelli 16,826 -
6
votes1
answer244
viewsA: Data Frame and Linear Regression
To make the log you can put directly into the formula. Example: # sem log lm(mpg ~ cyl, mtcars) # com log lm(log(mpg) ~ cyl, mtcars) To put the quadratic term you will use the auxiliary function…
-
7
votes2
answers2993
viewsA: Eliminating Double Lines from a Data.Frame
You can use the command unique, he will leave only the unique observations of his data.frame. For example, recreating your database: df<- read.table(text = "values ind 1 10.82000 2011-01-03 2…
ranswered Carlos Cinelli 16,826 -
5
votes1
answer757
viewsA: R does not create graph correctly
Your problem is reading the data. Do not save your csv with dot as thousands separator, as the dot is decimal separator in R. For example, by taking the dots at hand in your data the barplot works…
ranswered Carlos Cinelli 16,826 -
5
votes5
answers8758
viewsA: How to remove line that has Missing?
You can also use the function filter of dplyr: Creating sample data (based on Daniel’s data): dados <- data.frame(var1 = c(NA, 1, 3), var2 = c(1, NA, 3)) Carrying the dplyr: library(dplyr) Remove…
ranswered Carlos Cinelli 16,826 -
7
votes2
answers309
viewsA: Plotting Graphs ggplot inside loop
The problem is that you’re inside the loop then you have to ask explicitly to print on the chart of ggplot. For example, in the command below, nothing will appear: for(i in 1:6) i Already if you put…
-
3
votes1
answer59
viewsA: Majority vote step by step
A way: sapply(2:ncol(a), function(x) apply(a, 1, function(y) names(which.max(table(y[1:x]))))) [,1] [,2] [,3] [,4] [1,] "2" "2" "2" "2" [2,] "3" "3" "3" "3" [3,] "4" "4" "4" "4" [4,] "1" "1" "1" "3"…
ranswered Carlos Cinelli 16,826 -
3
votes2
answers379
viewsA: Repeating structure of a function
In that case you wouldn’t need a loop either: rf2 <- function(n, a, b, v) matrix(-log(runif(n, 0, 1)) / exp(b/rep(v, each = n)-a), ncol = length(v)) Comparing to the Rcoster solution: set.seed(1)…
ranswered Carlos Cinelli 16,826 -
1
votes1
answer62
viewsA: How to get columns from XLS files in R
The function read.xlsx2 has a parameter called colIndex where you can specify which columns (by position) you want to extract. Thus, in the code below, for example, putting colIndex = c(1,3,4) you…
-
2
votes2
answers63
viewsA: Majority vote in matrix in R
As you want to know the value of higher frequency on each line, I would suggest making one lapply of function table per line and then check the highest frequency value: sapply(lapply(split(a,…
-
3
votes1
answer1613
viewsA: How to convert data.frame to list?
You can use the function as.list. For example: df <- read.table(text = "TAXA Taxa.1 Taxa.2 Taxa.3 Taxa.4 Taxa.5 Taxa.6 Taxa.7 Taxa.8 09/04/2013 32.7188 8.8350 13.0662 9.0114 8.7003 8.9924 76.7003…
ranswered Carlos Cinelli 16,826 -
7
votes1
answer678
viewsA: Calling C functions from R
I think that currently the cleanest and easiest way to integrate C++ codes (or C in your case) is by using the Rcpp package (CRAN link) (package website here). With Rcpp you can define a function in…
-
4
votes2
answers3134
viewsA: calculate difference between two dates in months on the R
First, without treating fraction of the month, there are some alternatives to pick differences between calendar month. Examples: x <- as.Date("2014-01-07") y <- as.Date("2015-03-17") # criando…
-
37
votes4
answers14202
viewsQ: How are prime numbers important in cryptography?
How are prime numbers an important part of some encryption systems? How this process works and what part prime numbers come into?
cryptographyasked Carlos Cinelli 16,826 -
1
votes2
answers2923
viewsA: Comparing contents of a Column
Creating an example data.frame : dados <- data.frame(x = rnorm(30), y = c("a","b","c")) To delete lines you will do a logical operation of sets in which you will select the elements that are not…
-
0
votes2
answers182
viewsA: How to install the Krisp package in R?
An alternative answer to Daniel’s, without using the devtools. First you have to install the package dependencies (the other packages of which the KriSp depends on):…
ranswered Carlos Cinelli 16,826 -
4
votes1
answer880
viewsA: Data manipulation with in-line dates for column
Well, I’m going to try to give you an answer with a generic example, similar to the data you used. What you will do first is pass your data to the "long" format, that is, you will turn the columns…
ranswered Carlos Cinelli 16,826 -
4
votes1
answer172
viewsA: Rename a file with a vector value
If I understand correctly, basically what you want to do is remove the ". zip" extension from the file name. You can do this by using the gsub(). For example, suppose your zip file has following…
ranswered Carlos Cinelli 16,826 -
1
votes2
answers108
viewsA: Updating Mysql table using sqlSave in R
Probably this is happening because you had already created the "test" table before, at some point, and now you are trying to write on it again. To know if it’s that turn sqlQuery(connection, "drop…
-
1
votes2
answers647
viewsA: SAS and R run Merge differently
SAS is removing duplicate records from DOMPNS before merging. If you do d2013 <- unique(d2013) before merging into R, the number of observations will be equal.
-
2
votes1
answer339
viewsA: Work with errors within R loop
Briefly explaining the djas suggestion (I put as community wiki). One way to solve your problem is by using the try() or tryCatch(). For example the code below will stop at i+"a" because of the…
-
4
votes2
answers1551
viewsA: How to group data by an id in R
There are several ways to do this in R, what you want is to aggregate one variable using another as a group. This question of Sopt speaks exactly about it and will help you ( How to consolidate…
-
5
votes2
answers620
viewsA: How to smooth a curve and define ylim for a given function using ggplot2?
You will use the stat_function() for the function and ylim() and xlim() to define the limits: library(ggplot2) x <- c(-4:4) f1 <- function(x){x^4 - 6*(x^2)} ggplot(data.frame(x), aes(x)) +…