Generating random numbers from the Laplace distribution

I have been trying to generate random numbers from the double exponential(Laplace) distribution. I am at a point I can write the code anymore. Any help would be appreciated. The code below is what I have written. rlaplace = function(u,a,b){ u = c(runif(ns)) for(i in 1:ns){ if(u[i] <= 0.5){ X = a+b*log(2*u) } else{ X = a-b*log(2*(1-u))

从拉普拉斯分布生成随机数

我一直试图从双指数(拉普拉斯)分布生成随机数。 我现在可以写代码了。 任何帮助,将不胜感激。 下面的代码是我写的。 rlaplace = function(u,a,b){ u = c(runif(ns)) for(i in 1:ns){ if(u[i] <= 0.5){ X = a+b*log(2*u) } else{ X = a-b*log(2*(1-u)) } } X } z1 = rlaplace(u,a,b) 从概率分布CRAN任务视图中,有几个已经实现了拉普拉斯分布的包,

How to find if the numbers are continuous in R?

I have a range of values c(1,2,3,4,5,8,9,10,13,14,15) And I want to find the ranges where the numbers become discontinuous. All I want is this as output: (1,5) (8,10) (13,15) I need to find break points. I need to do it in R. Something like this? x <- c(1:5, 8:10, 13:15) # example data unname(tapply(x, cumsum(c(1, diff(x)) != 1), range) # [[1]] # [1] 1 5 # # [[2]] # [1] 8 10 # # [

如何查找R中的数字是否连续?

我有一系列的价值观 c(1,2,3,4,5,8,9,10,13,14,15) 我想找到数字变成不连续的范围。 我想要的就是这个输出: (1,5) (8,10) (13,15) 我需要找到断点。 我需要在R做。 像这样? x <- c(1:5, 8:10, 13:15) # example data unname(tapply(x, cumsum(c(1, diff(x)) != 1), range) # [[1]] # [1] 1 5 # # [[2]] # [1] 8 10 # # [[3]] # [1] 13 15 另一个例子: x <- c(1, 5, 10, 11:14, 20:21, 23) unname(tapply(

Random vector a with elements from the uniform distribution in R

I'm new to R and I'm looking through a book called "Discovering Statistics using R". Although the book implies you don't need any statistical background, some of the content isn't covered/explained... I'm wondering how you simulate a random vector a with 200 elements from the uniform distribution on the interval (-50, 50). Maybe I'm unsure because of the te

随机矢量a与R中均匀分布的元素

我是R新手,正在翻阅一本名为“使用R发现统计信息”的书。 虽然这本书意味着你不需要任何统计背景,但其中的一些内容没有被覆盖/解释...... 我想知道你是如何模拟一个随机向量a和200个距离均匀分布(-50,50)的元素的。 也许我不确定,因为使用的术语。 对于这个例子,我创建了一个包含200个元素的矢量,范围介于-50和50之间......我只是检查这是否正确。 还是有一种函数可以像Java中那样生成随机值? 我在Google上没有

Generating random numbers from various distributions in CUDA

I am playing around with doing MCMC on the GPU, and need implementations for various samplers, written for CUDA. Most of the posts I've seen on StackOverflow relate to uniform, binomial, and normal sampling. Are there any libraries that allow me the simplicity and variety of the dpqr functions in R (See this page)? I would like to be able to sample from Gamma, Normal, Binomial, and the i

从CUDA中的各种分布生成随机数

我正在玩GPU上的MCMC,并需要针对CUDA编写的各种采样器的实现。 我在StackOverflow上看到的大多数帖子都涉及统一,二项和正常采样。 是否有任何库允许R中的dpqr函数的简单性和多样性(请参阅此页)? 我希望能够从伽玛,正态,二项式以及贝叶斯问题(逆卡方,逆伽马)中使用的逆分布中抽样,并且宁愿不必使用逆概率变换和接受拒绝采样。 对于Gamma分配,这是我目前使用的。 GSL功能修改为与CuRAND一起使用。 __device__

Random sample from given bivariate discrete distribution

Suppose I have a bivariate discrete distribution, ie a table of probability values P(X=i,Y=j), for i=1,...n and j=1,...m. How do I generate a random sample (X_k,Y_k), k=1,...N from such distribution? Maybe there is a ready R function like: sample(100,prob=biprob) where biprob is 2 dimensional matrix? One intuitive way to sample is the following. Suppose we have a data.frame dt=data.frame(

给定二元离散分布的随机样本

假设我有一个二元离散分布,即对于i = 1,...,n和j = 1,...,m的概率值表P(X = i,Y = j)。 如何从这样的分布生成一个随机样本(X_k,Y_k),k = 1,... N? 也许有一个现成的R功能,如: sample(100,prob=biprob) biprob是二维矩阵? 一个直观的方法是采样如下。 假设我们有一个data.frame dt=data.frame(X=x,Y=y,P=pij) x和y来自哪里 expand.grid(x=1:n,y=1:m) 并且pij是P(X = i,Y = j)。 然后,我们按照

How to convert tidy hierarchical data frame to hierarchical list grid in R?

This is a more complex version of a previous question where I had abstracted the actual problem too much to apply the answers. R convert tidy hierarchical data frame to hierarchical list I've converted a hierarchical data frame with two grouping levels into a hierarchical list-grid using a for loop. Is there a more efficient base R, tidyverse or other approach to achieve this? In the r

如何将整齐的分层数据框转换为R中的分层列表网格?

这是前一个问题的一个更复杂的版本,我已经抽取了实际问题太多以应用答案。 R将整齐的分层数据帧转换为分层列表 我已经使用for循环将具有两个分组级别的分层数据帧转换为分层列表网格。 是否有更高效的R,Tidyverse或其他方法来实现这一目标? 在真实数据集中: 分组变量和描述是多字字符串。 描述前言 - d# - 位于MWE中,便于检查。 有14个相关的变量,各种类型:字符,整数和双精度 规则 第1组和第2组标题

url disappears after merging xlsx files using readxl package

Usually, I ask questions using a reproducible sample. However, due to the nature of the question, I am unable to provide a producible script. I have a folder containing a list of .xlsx files and have merged the files together into one data frame using the readxl package. However, the url s in one column are no longer there. How can I merge .xlsx files while preserving the url s? If it help

使用readxl软件包合并xlsx文件后,url会消失

通常,我会用可重现的样本提问。 但是,由于问题的性质,我无法提供可生成的脚本。 我有一个包含.xlsx文件列表的文件夹,并使用readxl软件包将文件合并到一个数据框中。 但是,一列中的url不再存在。 如何在保留url的同时合并.xlsx文件? 如果有帮助,以下是我用来合并.xlsx文件的脚本: data.files = list.files() df <- readxl::read_excel(data.files[1], sheet=1, skip=1) #reading the first file of list for (

Merging data frames in a list

This question already has an answer here: Simultaneously merge multiple data.frames in a list 7 answers You can try Reduce with merge Reduce(function(...) merge(..., by='custID', all=TRUE), res) # custID top_pct.x top_pct.y #1 1 10 10 #2 2 20 20 #3 6 NA 10 #4 7 NA 20 Or as @Colonel Beauvel suggested, a mor

合并列表中的数据帧

这个问题在这里已经有了答案: 同时合并列表中的多个data.frames 7个答案 您可以尝试merge Reduce Reduce(function(...) merge(..., by='custID', all=TRUE), res) # custID top_pct.x top_pct.y #1 1 10 10 #2 2 20 20 #3 6 NA 10 #4 7 NA 20 或者像@Colonel Beauvel建议的那样,更可读的方法是将Curry的library(functional) li

Merge several data frames on two common columns

I've seen some questions about merging csv files into one data frame. What if the data frames are already in the workspace. I have five wide zoos that I cast as data frames, then melt. Here's the head of one: > head(df.mon.ssf.ret) date variable value 1 2009.000 AA1C NA 2 2009.083 AA1C NA 3 2009.167 AA1C NA 4 2009.250 AA1C NA 5 2009.333 AA1C

在两个公共列上合并几个数据帧

我已经看到一些关于将csv文件合并到一个数据框中的问题。 如果数据帧已经在工作区中,该怎么办? 我有五个宽阔的动物园作为数据框架,然后融化。 这是一个头: > head(df.mon.ssf.ret) date variable value 1 2009.000 AA1C NA 2 2009.083 AA1C NA 3 2009.167 AA1C NA 4 2009.250 AA1C NA 5 2009.333 AA1C NA 6 2009.417 AA1C NA 我可以将这些“日期”和“变量”与一系列嵌

How to join (merge) data frames (inner, outer, left, right)?

Given two data frames: df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3))) df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1))) df1 # CustomerId Product # 1 Toaster # 2 Toaster # 3 Toaster # 4 Radio # 5 Radio # 6 Radio df2 # CustomerId State # 2

如何加入(合并)数据框架(内部,外部,左侧,右侧)?

给定两个数据帧: df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3))) df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1))) df1 # CustomerId Product # 1 Toaster # 2 Toaster # 3 Toaster # 4 Radio # 5 Radio # 6 Radio df2 # CustomerId State # 2 Alabam