0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Q.行ごとに対応した条件で変数を作る

Last updated at Posted at 2021-12-04

さあそろそろネタがなくなってきましたw

これも誰得みたいな話なんですが,今回は次のようなケースです。

theta <- 0.6
lambda <- 3.5
N <- 10
sample <- data.frame(FLG= rbinom(N,size=1, prob = theta) )

上のコードを実行すると,次のようなデータフレームができます。

> sample
   FLG
1    1
2    1
3    1
4    0
5    0
6    1
7    1
8    0
9    1
10   1

ここで,FLGが0のところは0のままで,1になっているところはlambda=3.5のポアソン分布に従う乱数が入る,というようなデータを作りたい。

最初次のようなコードを書いていたんですが,エラーが出てうまくいきませんでした。
どうしましょう。

sample %>% 
  mutate(value = if_else(FLG==0, 0, rpois(1,lambda)))
 エラー: Problem with `mutate()` column `value`.
ℹ `value = if_else(FLG == 0, 0, rpois(1, lambda))`.
x `false` must be a double vector, not an integer vector.

解答編はこちら。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?