Hi:

It's unclear what you want for your vector of probabilities, but here's one
approach:

u$p <- runif(1000)        # generate a random vector of success
probabilities
samp <- function(x) {
          p <- as.numeric(x[3])     # x will be character as constructed,
pick out the numeric value of p
          x[sample(c(1, 2), 1, prob = c(p, 1 - p))]     # randomly sample
first or second element
      }
# Apply function samp to each row of the data frame
u$V3 <- apply(u, 1, samp)
u$p <- NULL         # get rid of the probability vector

head(u)

You weren't clear about how you wanted to select the success probabilities
of each element in each row, so I just generated them randomly. Adapt to
your situation.

HTH,
Dennis

On Mon, Mar 21, 2011 at 10:43 AM, Lisa <lisajca@gmail.com> wrote:

> Hi, everybody,
>
> I have a problem and need your help.
> There are two columns that look like this:
>
>  [1,] "t"  "f"
>  [2,] "f"  "t"
>  [3,] "t"  "f"
>  [4,] "t"  "t"
>  [5,] "f"  "f"
>
> I just want to generate the third column based on these two columns. First,
> I randomly choose one of the two columns, say, the first column. So, the
> first character of the third column is “t” that looks like this:
>
>  [1,] "t"  "f"  "t"
>  [2,] "f"  "t"
>  [3,] "t"  "f"
>  [4,] "t"  "t"
>  [5,] "f"  "f"
>
> Second, I determine the second character of the third column with an
> additional Bernoulli trial with a probability, for example, rbinom(1, 1,
> 0.3). If the random generation in R is 0, we keep "f", the second character
> in the first column because the first column has been chosen in the first
> step, while if the random generation in R is 1, we choose "t" instead, the
> second character in the second column.
>
> Third, I determine the third character of the third column with a Bernoulli
> trial but a different probability, for example, rbinom(1, 1, 0.7).
>
> Repeat these processes…
>
> How can I do this efficiently if there are more than thousand records
> (rows)?  Can anybody please help how to get this done? Thanks a lot in
> advance
>
> Lisa
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Randomly-generating-data-tp3394250p3394250.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>

	[[alternative HTML version deleted]]

