[R] Error when using the cat function

Mike Meredith mmeredith at wcs.org
Sat Jul 28 14:20:05 CEST 2007


Your problem is with ifelse, not with cat.

First clue is that 

ifelse(TRUE,{print("yes")},{print("no")})  # results in "yes" being printed
TWICE. Try this:

tmp <- ifelse(TRUE,{print("yes")},{print("no")}) # one "yes"

tmp  # another "yes"

Try:

print(print("yes")) # prints "yes" and returns "yes" invisibly. This
returned value is passed on to/by ifelse.

Now try:

print(cat("yes\n")) # "yes" appears, but cat("yes") returns NULL, which
ifelse can't handle:

ifelse(TRUE, NULL, "whatever") # Gives the error you saw.

What you need is if { } else { } :

if(!inherits(dat[[n]], "factor")) {cat("yes\n")} else {cat("no\n")}

HTH, Mike.


Stan Hopkins wrote:
> 
> Is the following developed in my console output a recognized bug or am I
> using the cat function incorrectly?
> 
> Thanks,
> 
> Stan
> 
> 
> 
>> ifelse(class(data[[n]])!="factor",{print("yes")},{print("no")})
> [1] "yes"
> [1] "yes"
>> ifelse(class(data[[n]])!="factor",{cat("yes")},{cat("no")})
> yesError in ans[test & !nas] <- rep(yes, length.out = length(ans))[test &
> : 
> incompatible types (from NULL to logical) in subassignment type fix
> 
> 
>>cat("yes")
> yes 
>> class(data[[n]])!="factor"
> [1] TRUE
>> class(data[[n]])
> [1] "numeric"
>> n
> [1] 28
>> length(data[[n]])
> [1] 955
>> class(data)
> [1] "data.frame"
>> dim(data)
> [1] 955 182
>> data[[n]]
> [1] 2.5 4.9 2.6 3.0 4.7 5.0 3.9 1.5 4.8 3.2 3.6 5.2 6.3
> [14] 6.3 5.0 4.6 6.0 4.5 3.9 3.6 5.7 8.5 4.0 5.0 11.8 4.7
> [27] 7.9 2.8 4.8 5.1 4.1 4.2 3.7 2.0 2.1 1.1 14.6 7.0 3.4
> [40] 3.4 10.1 4.7 4.9 5.2 4.3 2.9 2.8 2.3 1.2 2.0 2.0 3.0
> [53] 2.0 1.1 2.0 1.0 2.0 2.0 2.7 1.0 2.0 2.0 2.0 2.0 1.1
> [66] 2.0 2.0 1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1.8 2.4 1.8 1.9 6.1 5.1 4.0 3.8 2.8 3.4 3.1 2.3
> [781] 7.5 3.0 3.0 3.1 2.4 6.0 2.3 5.0 2.8 2.7 2.2 5.0 5.0
> [794] 3.0 9.0 7.0 7.0 7.0 9.0 8.0 9.0 2.0 4.0 4.0 3.0 3.0
> [807] 2.0 2.0 3.0 4.0 3.0 3.0 7.9 11.0 16.0 4.0 7.7 5.0 6.6
> [820] 16.0 9.0 19.0 4.0 4.0 7.5 6.6 22.0 20.0 7.2 7.1 7.6 6.6
> [833] 6.2 7.8 6.9 10.9 11.2 6.0 5.0 8.0 7.0 4.0 3.0 6.0 4.0
> [846] 2.6 6.0 7.0 4.0 1.4 2.0 6.0 6.0 6.0 6.0 24.0 27.6 15.8
> [859] 8.0 7.8 7.3 8.2 5.3 3.4 18.1 31.6 5.8 5.5 4.0 4.1 4.7
> [872] 4.9 3.9 0.5 3.9 6.2 3.0 3.0 4.0 2.0 3.0 3.0 2.0 2.0
> [885] 2.0 0.0 3.0 2.0 2.0 4.0 1.6 1.7 2.0 2.0 2.0 2.0 26.0
> [898] 2.0 2.0 2.0 3.0 2.0 3.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0
> [911] 2.0 0.0 2.0 2.0 2.0 2.0 5.0 2.0 11.7 10.4 8.0 4.0 11.1
> [924] 13.2 14.6 11.7 13.4 14.3 15.8 25.6 10.0 6.0 9.1 9.7 4.0 10.7
> [937] 6.0 5.0 10.9 10.0 10.6 12.9 12.3 11.6 11.8 13.3 15.1 10.7 11.0
> [950] 13.5 32.9 12.9 8.4 8.1 12.5
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://www.nabble.com/Error-when-using-the-cat-function-tf4161670.html#a11842347
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list