当我运行下面的代码时,这是我得到的错误:
中的错误:if (mm[[j]] == mm[[j + 1]]) {
需要 TRUE/FALSE 时缺少值
关于如何解决这个问题的任何想法
m <- matrix(c(1:5,NA,7,7,NA),
nrow=3,ncol=3,byrow=T)
print(m)
for ( i in 1:dim(m)[[1]] ) {
mm <- sort(m[i,],na.last=c(NA,NA,T)[[i]])
for ( j in 1:(length(mm)-1) ) {
if ( mm[[j]]==mm[[j+1]] ) {
cat(i,j,mm[[j]],fill=T)
}
}
}
尝试将
isTRUE()
包裹在您的 if
周围 - 状况:
for ( i in 1:dim(m)[[1]] ) {
mm <- sort(m[i,],na.last=c(NA,NA,T)[[i]])
for ( j in 1:(length(mm)-1) ) {
if ( isTRUE(mm[[j]]==mm[[j+1]]) ) {
cat(i,j,mm[[j]],fill=T)
}
}
}
输出:
3 1 7
print(message)
#load the mtcars dataset
data(mtcars)
#create a histogram of the mpg variables
hist(mtcars$mpg, breaks=10, main="histogram of MPG",xlab="MPG")