[英]How to apply specific columns to sapply function in R?
For example:例如:
sapply(cars[2:3], FUN = IQR)
But how about if I wanted index 2 and 5?但是如果我想要索引 2 和 5 呢? Also instead of indexes is there any way to use the column names instead?
另外有没有办法代替索引来使用列名?
We can use anonymous function我们可以使用匿名函数
sapply(names(cars)[1:2], function(x) IQR(cars[[x]]))
If we wanted columns 2 and 5, use c
instead of seq operator (using a different dataset as cars
have only two columns)如果我们想要第 2 和第 5 列,请使用
c
而不是 seq 运算符(使用不同的数据集,因为cars
只有两列)
sapply(mtcars[c(2, 5)], IQR)
# cyl drat
# 4.00 0.84
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.