[R] Reconstruct a dataset

Gray Calhoun gray.calhoun at gmail.com
Wed Dec 16 03:37:36 CET 2009


Hi Lisa,
  Look up indexing and random number generation.  c(0,0,1,1) is not a
random number.  You can use sample to generate a random index, and
index the dataset.  I think that if you read the help pages (ie
?sample), you'll be able to use the code I sent you to get your
results (I'll reproduce it below since I accidentally didn't cc the
list on my first email)
--Gray

### start of code
d <- data.frame(var1 = rnorm(10), var2 = rnorm(10))
dWow <- cbind(d[sample(10),], d[sample(10),])
names(dWow) <- c("var1.1", "var2.1", "var1.2", "var2.2")
dWow
### end of code


On Tue, Dec 15, 2009 at 5:10 PM, Lisa <lisajca at gmail.com> wrote:
>
> Supplement to my problem
>
> I know how to generate the random numbers, but I am not sure how to rewrite
> the dataset. In my example, for the first set of random number, rn1 <- c(0,
> 0, 1, 1),  only the last two numbers are “1”s, not zeros, so I select the
> individual 3 and individual 4 from y original dataset, i.e.,
>
> 2  1
> 2  2
>
> And then write these two rows as
>
> id  var1.1  var1.2  var2.1  var2.2
> 1        2       2       1       2
>
> For the second set of random number, rn2 <- c(0, 0, 2, 0), only the third
> number is “2”,  so I select the individual 3 two times from my original
> dataset, i.e.,
>
> 2  1
> 2  1
>
> And then write these two rows as
>
> id  var1.1  var1.2  var2.1  var2.2
> 2        2       2       1       1
>
> And so on.
>
> Finally, combine all rows together
>
> id  var1.1  var1.2  var2.1  var2.2
> 1        2       2       1       2
> 2        2       2       1       1
> ….
>
> Lisa
>
> Lisa wrote:
>>
>> Dear all,
>>
>> I have a dataset that looks like this:
>>
>> inividual   var1  var2   …
>> 1              1     1   …
>> 2              1     2   …
>> 3              2     1   …
>> 4              2     2   …
>> …              …     …   …
>>
>> I will random sample 2 individuals from this dataset based on a set of
>> random numbers, e.g.,
>>
>> rn1 <- c(0, 0, 1, 1),  rn2 <- c(0, 0, 2, 0), rn3 <- c(1, 0, 0, 1), rn4 <-
>> c(0, 1, 1, 0),…
>>
>> After reconstructed the samples, I have a dataset that looks like this:
>>
>> id  var1.1  var1.2  var2.1  var2.2  …
>> 1        2       2       1       2  …
>> 2        2       2       1       1  …
>> 3        1       2       1       2  …
>> 4        1       2       2       1  …
>> …        …       …       …       …  …
>>
>> Please ignore the bold and italic fond styles, I just want to make my
>> problem clear.
>>
>> Can anyone please help how to get this done? Your help would be greatly
>> appreciated.
>>
>> Lisa
>>
>>
>>
>
> --
> View this message in context: http://n4.nabble.com/Reconstruct-a-dataset-tp964699p964761.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Gray Calhoun

Assistant Professor of Economics
Iowa State University




More information about the R-help mailing list