“R 代码中需要 TRUE/FALSE 时缺少值”语法错误

问题描述 投票:0回答:2

当我运行下面的代码时,这是我得到的错误:

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)
}
}
}
r syntax-error
2个回答
2
投票

尝试将

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

-2
投票
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")
© www.soinside.com 2019 - 2024. All rights reserved.