Posts by Molx • 2,659 points
66 posts
-
1
votes1
answer259
viewsA: How to import a file . xls in R keeping accented names in categorical variables?
You can use the package readxl, that solves the problem automatically: > library(readxl) > read_xlsx("Drogas.xlsx") # A tibble: 3 x 1 Drogas <chr> 1 Antipsicótico 2 Benzodiazepínico 3…
-
1
votes2
answers172
viewsA: Handle values of a data.frame
Follow a solution in R only. It’s not as short as the one that uses the terminal tools, but it works. I made the separate steps also to make it clearer. # Lendo os dados dat <-…
-
2
votes1
answer67
viewsA: In R, use fread in a Shh connection with pipe
In reality to do this just remove the pipe, for the very fread already does the Piping of command: capture_file <- paste0('plink -ssh ', user, '@123.45.678.901 -P 12 -pw ', #fictional password, '…
-
1
votes3
answers126
viewsA: Organize data in excel
If you saved . csv from an excel in English, you must use read.csv2. In relation to read.csv, it changes because it uses sep = ";" and dec = ",", which is the non-American standard. It’s worth using…
-
1
votes1
answer661
viewsA: How to join Bibtex files in R
The solution to your problem is even simpler than the code that Daniel Falbel suggested. You don’t even need the package bibliometrix because it even reads the files as text. What you need is to…
-
0
votes1
answer2622
viewsA: How to place values in faceted bar charts in R?
What you need to do in this case is to calculate the central position of each column before making the graph and then use this value as y of geom_text. This calculation is simple but can be boring…
-
3
votes1
answer37
viewsA: Problem endpoint function R
Your problem is in reading the data. First, you need to use stringsAsFactors = FALSE so that R does not convert non-numerical columns into factors. You also need to inform the na.string = "ND",…
-
2
votes1
answer48
viewsA: Create a package that depends on other packages
In the directory where your package code is, there must be a metadata file called DESCRIPTION. In this file, one of the fields that can be included is the Imports, indicating that your package…
-
4
votes1
answer111
viewsA: Data presentation in ggplot2
Apparently you just need to wear boundary = 0 in the geom_histogram to force the first bar to 0. Since you did not provide the data, I created an example, it was a little different but seems to have…
-
1
votes1
answer87
viewsA: Error in output of Optim when creating a regression structure
It seems that Eduardo’s suggestion in the comments has to do with your problem, yes. In that reply for that question, the hint was given to check the result of its function with the initial values.…
-
5
votes2
answers255
viewsA: Error in r function Optim output
These aren’t mistakes per se, they’re warnings (warnings) that something happened in a way that probably shouldn’t, but that the code was executed completely. In your case, they are given because…
-
2
votes1
answer270
viewsA: A: Insert date difference in a time difference function
Working with functions created by other people is not very simple (especially without the explanation of the algorithm), so I found it simpler to make it from scratch. First, it is important that…
-
3
votes3
answers319
viewsA: Remove dataset elements by factors
You don’t need to complicate much to do this, you just have to count the occurrences of each level using table and then remove the lines where occurrences are smaller than the limit. For example: tb…
-
3
votes2
answers1742
viewsA: Processing time of functions
A good option for you is the package lineprof Hadley Wickham. Since the package is not in CRAN, you have to install from Github using the package devtools: install.packages("devtools")…
-
1
votes1
answer309
viewsA: Function Predict() does not accept exponential regression in R
In your code, you use base as an argument data, but I imagine you meant modelo. It is not a good practice to use the same variable name for completely different objects as well (data frame and…
-
1
votes2
answers1477
viewsA: Plot Graph Multiple Variable Bars in R
I’m not sure I understand the format of your data. It seems that you have some columns filled with counts of people in different observations, and want the sum of each column in the graph. If so,…
-
1
votes1
answer1272
viewsA: How to print/plot a table in #R?
Using as reference that reply from Soen, I came to the next: First of all, make sure you really want to do this. Text and formatting are much more flexible and easy to modify than an image. A simple…
-
9
votes4
answers1339
viewsA: Equivalent to Excel SOMASES, in R
This can be done in many ways, as can be seen by the various responses. A way without using add packages is to use tapply: > tapply(df$Quantidade, list(df$Day.of.Week, df$Hour), sum) 10 11 12 13…
-
3
votes1
answer103
viewsA: How to delimit the number of characters written per line in a "txt" output in R?
What you want can be easily achieved using only the function cat. For example: cat(cod, file = "jean.txt", fill = 80) Will create the archive jean.txt with the following content: 82024 82042 82067…
-
1
votes2
answers1031
viewsA: How to extract a specific string snippet
Extract part of a string using only the package base is pretty boring, but possible. I chose a simpler regular expression than Daniel’s, since you weren’t very specific. It would look like this:…
-
0
votes1
answer582
viewsA: How to combine two vectors and get all the results of a given account with them
Well, if I understand your problem correctly, you want to calculate the expression bwg <- (max*(1-exp(-K*(MCi-(BW*Xm))))) for all combinations of mci and rp, and then keep, for each calculated…
-
1
votes3
answers1099
viewsA: How to divide the elements of a matrix by the average of its column
This type of die operation can be performed with the function scale, which serves precisely to resize the columns of an array. The default of this function is to subtract each column from its…
-
4
votes1
answer1327
viewsA: How to change table rows in R by values from the same row only from another column
Your data has a very strange format, as it is an array with text and numbers. Probably the ideal would be to replace the NAO INFORMADO for NA, in reading the values. In the current form, you can do…
-
1
votes1
answer78
viewsA: How to test multiple data.frames and set a maximum test score for a certain position? #R
If I understand correctly, the code below does what you want. But the data does not seem to be ideal, because there is no case of A==B==C much less A==B==C==D. If you have more appropriate actual…
-
2
votes2
answers999
viewsA: How to read data with variables in lines, in Rstudio?
If when you say "in Rstudio" you mean the option via the menu (Tools > Import Dataset), it is not possible. This menu just creates a snippet to read the data according to the parameters you…
-
4
votes1
answer436
viewsA: How do I make tables smaller from a data.frame in R?
Regarding the format of the data, if they are as factor, the ideal is to correct this in the reading of the data. Numerical values read as factors may occur for any of the following reasons: Your…
-
1
votes4
answers2494
viewsA: How to join observations of tables that have a different set of variables in R?
A solution of the base package itself is to use the merge. Although it is usually used to make the match between columns, also works as a rbind with Fill as long as you give the correct arguments.…
-
6
votes2
answers222
viewsA: Questions about the model y=a/(x+b)?
The simplest way is to use nonlinear regression by least squares (function nls). Mathematically, this is an iterative method, which looks for values for the parameters to reduce the residues below a…
-
5
votes3
answers108
viewsA: How can I restructure information contained in a list object into two columns?
There is a function on the base that does just that, called stack. The hardest part of using it is remembering the name, because it is very unusual (I spent 10 minutes searching until I found...):…
-
4
votes1
answer137
viewsA: Remove zero column from an array
If you need to simply remove elements from the array, you can do the same as you would with vectors or matrices, that is, use the minus sign with the index of the elements you want to remove: b2…
-
2
votes1
answer418
viewsA: How to plot several graphs on an A4 sheet with defined margins?
That was a little harder than planned, because I couldn’t do the pdf work with the par(mai), to define the margins. To work, I made a very elegant strategy, but it seems to have worked. What I did…
-
8
votes1
answer91
viewsA: How to position text in the intermediate columns?
To achieve this, you just need to know that you can use the object itself barplot for the positions of x in function text. I already gave a similar answer on this one. Since your code is not…
-
1
votes2
answers66
viewsA: How can I find out which packages/libraries belong to the functions of a routine written in R?
Some more alternatives: It may seem obvious, but simply searching for "R function" in Google is almost always sufficient for package functions that are in the CRAN, saving functions with very…
-
3
votes4
answers1242
viewsA: How can I save a list or export a list object in R?
Alternatives using R’s own functions are: Save as binary file, which takes up less space, but can only be opened in R. save(x, file = "arquivo.RData") save.image(file = "todoWorkspace.RData") #…
-
5
votes1
answer142
viewsA: What function in R to reorder a data.frame that contains missing data (NA)?
Reading your data: dat <- read.table(text = "Linha Data Total Med.2 DP.2 Med.3 DP.3 Med.4 DP.4 1 2009 1.4749 NA NA NA NA NA NA 2 2010 2.9945 2.2347 1.0746 NA NA NA NA 3 2011 2.9945 2.9945 0.0000…
-
4
votes2
answers702
viewsA: How to add a second Y axis using the matplot function?
You can use the function axis to add an extra axis to the chart and if necessary mtext to put your label. For example, for a simple chart: par(mar = c(5, 4, 4, 4) + 0.1) plot(1:10, ylab = "Eixo Y…
-
3
votes1
answer50
viewsA: Why does the for-loop convert from Date to integer?
(Adaptation of Soen’s response here) The help page (?`for`) says the following: An Expression evaluating to a vector (including a list and an Expression) or to a pairlist or NULL. As objects of the…
-
1
votes2
answers43
viewsA: Doubts when using the "rle" function
Your error is in using "TRUE", which is a string instead of TRUE, which is a logical value. The correct one, using your code, would be evento = teste$lengths[teste$values==TRUE] But in reality, any…
-
2
votes2
answers2625
viewsA: Bar graph text stacked in R
To do this using barplot() and text(), you need to extract the x and y positions of each value you want to plot. For the values of y, we will use the value returned by the function itself barplot…
-
2
votes2
answers122
viewsA: How can I get maximum and minimum values after applying LOESS
ggplot uses the function loess base R. You can run the setting outside the graphical command and get the values using predict: set.seed(4) dados <- data.frame(date_time = 1:20, duracao =…
-
6
votes4
answers1852
viewsA: Programmatically generate and download links
Here is an answer in R. You will need the packages httr and XML: install.packages("httr") install.packages("XML") I made the code in a simpler way, without creating functions or putting other…
-
3
votes2
answers63
viewsA: Majority vote in matrix in R
You can do this easily using a combination of apply with table: count <- apply(a, 1, function(r) as.numeric(names(which.max(table(r))))) We have to use as.numeric because names returns a string…
-
4
votes2
answers361
viewsA: How to Create a Bubble Plot
The easiest way to do this in R is by using the function symbols. Since you did not provide specific data, I created some random ones using the argument prob to cause an unequal distribution. n…
-
6
votes1
answer138
viewsA: How to extract values from the statistics calculated by the linhom function (L-Function for inhomogeneous Spatial point processes)?
As you may have seen, you can recover the values of the minimum confidence interval using bootlm22$lo, the estimated value with bootlm22$iso, the theoretical value with bootlm22$theo and the value…
-
3
votes3
answers6632
viewsA: How do I know if an object is a javascript array (without jquery)?
Another way: [].constructor === Array
-
1
votes1
answer131
viewsA: Problem with xts Object
Just convert your data to an object xts. For this, you need to select an argument order.by which should be a date. I put the current date, but you should use something that makes sense in your…
-
5
votes1
answer424
viewsA: vector moving mean of a matrix in R
There are packages with this type of function. Adapting to reply posted on Soen: library(raster) r <- raster(m) as.matrix(focal(r, matrix(1,3,3), mean, pad = TRUE, padValue = NA, na.rm = TRUE))…
-
2
votes2
answers140
viewsA: script about the R program
Your code seems to be almost 100%, missed just you assemble the name of each file completely. You can do this using the function paste, or paste0 not to need to define the argument sep = "". Solving…
-
2
votes2
answers5264
viewsA: How to import data (.csv) to R while maintaining the original format
The base functions for reading tables are sufficient to suit most cases. However, they are relatively slow, and there are faster alternatives if they are many files and/or they are very large, which…
-
3
votes2
answers1265
viewsA: Daily Time Series Decomposition
Since you didn’t post your full code, follow what I used with your data and gave a seemingly correct result. dados <- read.csv2("Vazao_UHE.csv") dados.ts <- ts(data=dados$FURNAS, frequency =…