R言語|カスタム関数データセット(data.frame)の列に対する条件判定計算
1.irisデータセットの使用
2.念のため、z列を0に設定しても不要かもしれません
3.カスタム関数を用いてdata.frameのx行を判断し、y列を演算し、z列に値を与える
4…Mapの使用に注意
> iris_10 <- head(iris, n = 10)
## : x >= 5.0, z = y *10
> get_With_function <- function(x, y, z){
+ if(x >= 5.0){
+ z <- y * 10
+ }
+ c(zlie = z )
+ }
2.念のため、z列を0に設定しても不要かもしれません
> iris_10$z <- 0
3.カスタム関数を用いてdata.frameのx行を判断し、y列を演算し、z列に値を与える
4…Mapの使用に注意
> iris_10$z <- with(
+ iris_10,
+ Map(
+ get_With_function,
+ iris_10$Sepal.Length,
+ iris_10$Sepal.Width,
+ z
+ )
+ )
> iris_10
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 1.3 0.2
4 4.6 3.1 1.5 0.2
5 5.0 3.6 1.4 0.2
6 5.4 3.9 1.7 0.4
7 4.6 3.4 1.4 0.3
8 5.0 3.4 1.5 0.2
9 4.4 2.9 1.4 0.2
10 4.9 3.1 1.5 0.1
Species z
1 setosa 35
2 setosa 0
3 setosa 0
4 setosa 0
5 setosa 36
6 setosa 39
7 setosa 0
8 setosa 34
9 setosa 0
10 setosa 0