这样做。
ok <- function(data)
{
lattice:::bwplot(size ~ mxPH, data=data)
}
> ok(algae)
## no error
我想按列号指定 size 和 mxPH 。但是,以下方法无效。
第一次尝试
aa <- function(data, n1, n2)
{
names <- names(data)
lattice:::bwplot(names[n1] ~ names[n2], data=data)
}
> aa(algae,2,4)
Warning message:
In function (x, y, box.ratio = 1, box.width = box.ratio/(1 + box.ratio), :
NAs introduced by coercion
第二次尝试(对第一次尝试的小修改)
bb <- function(data, n1, n2)
{
names <- factor(names(data))
lattice:::bwplot(names[n1] ~ names[n2], data=data)
}
> bb(algae,2,4)
## no error
第三次尝试(对第二次尝试的小修改)
cc <- function(data, n1, n2)
{
names <- factor(names(data))
lattice:::bwplot(deparse(substitute(names[n1])) ~ deparse(substitute(names[n2])), data=data)
}
> cc(algae,2,4)
Warning message:
In function (x, y, box.ratio = 1, box.width = box.ratio/(1 + box.ratio), :
NAs introduced by coercion
数据
algae <- utils:::read.table('http://www.liaad.up.pt/~ltorgo/DataMiningWithR/DataSets/Analysis.txt',
header=F,
dec='.',
col.names=c('season','size','speed','mxPH','mnO2','Cl','NO3','NH4','oPO4','PO4','Chla','a1','a2','a3','a4','a5','a6','a7'),
na.strings=c('XXXXXXX'))
答案 0 :(得分:2)
library(lattice)
bwplot.column <- function(data, n1, n2, xlab = NULL, ylab = NULL, ...)
{
if (is.null(ylab)) {
ylab <- names(data)[n1]
}
if (is.null(xlab)) {
xlab <- names(data)[n2]
}
bwplot(data[,n1] ~ data[,n2], xlab = xlab, ylab = ylab, data = data, ...)
}
答案 1 :(得分:2)
您可以参考迈克尔演示的特定列,或者您也可以手动粘贴公式:
fun <- function(dat,n1,n2){
nms <- colnames(dat)
f <- as.formula(paste(nms[n1],"~",nms[n2]))
bwplot(f,data = dat)
}
fun(algae,2,4)