Category "r"

set.seed in for loop

I'm doing some analysis and I had to impute some values. To do so, I write this chunk of code: A) set.seed(1) for (i in 2:length (Dataset[-c(8,11)])) { D

Go from long to wide using tidyr's pivot_wider

I have a simple long df where every element in the fi column should be a new column in the wide df. So the new df should have 10 columns A:J. The wide df should

I can't install the sf package when I use the renv package in a project

I started a new project in Rstudio and selected the option to use renv. The first thing I did was type the command install.packages('sf') and R returned the mes

Tell `kwic()` to ignore stopwords when situating keywords in context?

I once again have a question about the kwic() function from the quanteda package. I want to extract the five words around a specific keyword (in the example bel

R: converting text with irregular length into dataframe

Simplified example of a text i have after importing with readlines: text <- c("just", "stuff", "nothing", "interesting", "date", "06.05.2022", "number", "1/

WGCNA functions not found

I'm applying WGCNA to RNASeq data following this tutorial. For several functions it give me an error that the package functions are not found. > sft <- pi

dopar% and %foreach% failed to work for gmapsdistance

I am currently trying to apply parallelization with the gmapsdistance package (driving mode = bicycling) to speed up the computation of the for loop that contai

Error when renaming multiple columns and using add_overall() in tbl_summary

I am trying to create summary tables. I want to split these by site and have a "Total" column. However, when I try to rename multiple columns AND use add_overal

Matching and indexing through two dataframes and one matrix

I have a dataframe events with xy-coords of unique points. I have a dataframe all_nodes with xy-coords of network nodes. All points of events are also in all_no

Sampling submatrices in R

Given a matrix like mat > set.seed(1) > mat <- matrix(rbinom(100,1,0.5),10,10) > rownames(mat) <- paste0(sample(LETTERS[1:2],10,replace=T),c(1:nr

Evaluate and save Argument variable value during function definition?

Consider this function plus_x: y <- 1 plus_x <- function(input, x = y){ return(input + x) } here the y default-value for x is evaluated during the fun

dplyr get linear regression coefficients

I'm wondering if there is a better way is to get linear regression coefficients as columns in dplyr. Here is some sample data. mydata <- data.frame( S

Error when trying to download species models in R with ebirdst::ebirdst_download()

I've requested an access key and set it. Sys.getenv("EBIRDST_KEY") returns the correct key. ebirdst_download(species = "Sharp-tailed Grouse") returns an error

Keep libraries in memory even when all browsers are closed

I have a shiny app on RStudio's free shiny server that uses a fair number of libraries which results in a slow calculation time. The time for the UI to load is

Find a probability of Poisson distribution in R

Can somebody help me how can I calculate a probability p from the Poisson equation (programming in R)? I know that there is the ppois function, but I'm not sure

How to calculate correlation(r) between two raster datasets in R?

I have two (NPP_BEGA and ENACT_BEGA) stacked raster dataset. They have the same spatial resolution, extent and dimension. I want to run and map the correlation(

How to change a raster to a specific spatial resolution?

I would like to change the resolution of a raster. For example, let’s take this Landsat 7 images at ~ 30m resolution. library(terra) #> terra 1.5.21 f

Matching colours with column values in gt

I have a simple piece of code that tries to publish a table which colours columns according to the colour listed in the contents using the package gt. df <-

How do obtain max and mean values from different columns with a Date column condition in R?

I am trying to get the daily mean and maximum values for each pollutant. For each of the monitoring stations, the daily mean and maximum values of each pollutan

the function display_html not working in Jupyter Lab

This "R code" works fine in Jupyter but not in lab: library(IRdisplay) display_html( ' <script> code_show=true; function code_toggle() { if (code_s