replicate

How to repeat a block of code to sample 2 values in r?

百般思念 提交于 2021-02-05 11:28:07
问题 (I'm new to this so now editing my question as a reproducible example). I've reviewed bootstrapping,loop and replicate functions and can't figure out how to repeat a series of steps (not just a single function) in R and store the result in dataframe. I need to randomly select 2 values from a pool of 22 values, 9 times. Then conduct a spearman rank correlation test on that dataset (2 columns, 9 rows) 10,000 times and store the value of each of those iterations. So I need to repeat these steps

conditionally duplicating rows in a data frame

…衆ロ難τιáo~ 提交于 2021-01-28 03:12:23
问题 This is a sample of my data set: day city count 1 1 A 50 2 2 A 100 3 2 B 110 4 2 C 90 Here is the code for reproducing it: df <- data.frame( day = c(1,2,2,2), city = c("A","A","B","C"), count = c(50,100,110,90) ) As you could see, the count data is missing for city B and C on the day 1. What I want to do is to use city A's count as an estimate for the other two cities. So the desired output would be: day city count 1 1 A 50 2 1 B 50 3 1 C 50 4 2 A 100 5 2 B 110 6 2 C 90 I could come up with a

Simulating data in R with multiple probability distributions

一世执手 提交于 2020-06-26 05:33:15
问题 I am trying to simulate data via bootstrapping to create confidence bands for my real data with a funnel plot. I am building on the strategy of the accepted answer to a previous question. Instead of using a single probability distribution for simulating my data I want to modify it to use different probability distributions depending on the part of the data being simulated. I greatly appreciate anyone who can help answer the question or help me phrase the question more clearly. My problem is

I am trying to form 7 groups of random number of observations from a total of 100 observations. All observations should be used

瘦欲@ 提交于 2020-01-06 06:46:27
问题 I am trying to create a list of 7 groups with 100 observations. Each group can have different number of observations. All observations should be placed in one of the 7 groups. In other words, all observations should be used. The code I am using does not use all the observations. Is there a way that I can solve this? times_to_sample = 7L NN = nrow(df) sample<-replicate(times_to_sample, df[sample(NN, sample(5:15, 1L)), ], simplify = FALSE) my expected result just has to place each observation

Replicate certain values in vector determined by other vector

安稳与你 提交于 2020-01-04 04:01:21
问题 I have a vector of values (say 1:10 ), and want to repeat certain values in it 2 or more times, determined by another vector (say c(3,4,6,8) ). In this example, the result would be c(1,2,3,3,4,4,5,6,6,7,8,8,9,10) when repeating 2 times. This should work for an arbitrary length range vector (like 200:600 ), with a second vector which is contained by the first. Is there a handy way to achieve this? 回答1: Akrun's is a more compact method, but this also will work # get rep vector reps <- rep(1L,

How to run a function multiple times and write the results to a list?

不想你离开。 提交于 2019-12-23 10:22:46
问题 I have a function that creates a matrix and I want to call this function a thousand times such that in the end I have a list of 1000 matrices. Here is an example: set.seed(1) gen_mat <- function(x) matrix(c(1, 1, 1, x + rnorm(1)), nrow = 2) Now, I tried replicate(10, gen_mat(1)) , but this returns an array and not a list. How to do it? 回答1: Combination of above answer, comment, and my own answer. Naturally, I like mine better. Also, I think there is a mistake in the above answer for base R. n

How to duplicate a MySQL database on the same server

大憨熊 提交于 2019-12-20 08:46:42
问题 I have a large MySQL database, lets call it live_db , which I want to replicate on the same machine to provide a test system to play around with ( test_db ), including table structure and data. In regular intervals I want to update the test_db with the content of the live_db ; if possible incremental. Is there some built-in mechanism in MySQL to do that? I think that master-slave replication is not the thing I want since it should be possible to alter data in the test_db . These changes do

Repeating a repeated sequence

被刻印的时光 ゝ 提交于 2019-12-18 11:55:07
问题 We want to get an array that looks like this: 1,1,1,2,2,2,3,3,3,4,4,4,1,1,1,2,2,2,3,3,3,4,4,4,1,1,1,2,2,2,3,3,3,4,4,4 What is the easiest way to do it? 回答1: You can do it with a single rep call. The each and times parameters are evaluated sequentially with the each being done first. rep(1:4, times=3, each=3) #[1] 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 回答2: Or, simpler (assuming you mean a vector, not an array) rep(rep(1:4,each=3),3) 回答3: 42-'s answer will work

Using “…” and “replicate”

ぐ巨炮叔叔 提交于 2019-12-18 07:28:14
问题 In the documentation of sapply and replicate there is a warning regarding using ... Now, I can accept it as such, but would like to understand what is behind it. So I've created this little contrived example: innerfunction<-function(x, extrapar1=0, extrapar2=extrapar1) { cat("x:", x, ", xp1:", extrapar1, ", xp2:", extrapar2, "\n") } middlefunction<-function(x,...) { innerfunction(x,...) } outerfunction<-function(x, ...) { cat("Run middle function:\n") replicate(2, middlefunction(x,...)) cat(

Repeat each row of data.frame the number of times specified in a column

醉酒当歌 提交于 2019-12-16 18:16:29
问题 df <- data.frame(var1 = c('a', 'b', 'c'), var2 = c('d', 'e', 'f'), freq = 1:3) What is the simplest way to expand each row the first two columns of the data.frame above, so that each row is repeated the number of times specified in the column 'freq'? In other words, go from this: df var1 var2 freq 1 a d 1 2 b e 2 3 c f 3 To this: df.expanded var1 var2 1 a d 2 b e 3 b e 4 c f 5 c f 6 c f 回答1: Here's one solution: df.expanded <- df[rep(row.names(df), df$freq), 1:2] Result: var1 var2 1 a d 2 b e