1.使用iris数据集 > 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