I was working with the inbuilt data set (ggplot2
), named diamonds
. After having assigned to dia1
, I performed the following script, so as to group the values according to their carats. I got a message regarding NAs
being introduced by coercion. I fail to understand how that would have happened, as is also apparent by the sum of the is.na()
vector equating to zero.
#data
library(ggplot2)
dia1 <- diamonds
#logic
x<-1
dia1$carat<-as.character(dia1$carat)
for (i in 1:(length(dia1$carat))){
if (0<(as.numeric(dia1$carat[x]))&(as.numeric(dia1$carat[x]))<=1){
dia1$carat[x]<-"0-1"
}
if (1 < (as.numeric(dia1$carat[x]))&(as.numeric(dia1$carat[x])) <= 2){
dia1$carat[x]<-"1-2"
}
if (2<(as.numeric(dia1$carat[x]))&(as.numeric(dia1$carat[x]))<=3){
dia1$carat[x]<-"2-3"
}
if (3<(as.numeric(dia1$carat[x]))&(as.numeric(dia1$carat[x]))<=4){
dia1$carat[x]<-"3-4"
}
if (4<(as.numeric(dia1$carat[x]))&(as.numeric(dia1$carat[x]))<=5){
dia1$carat[x]<-"4-5"
}
x<-x+1
}
Error in if (0 < (as.numeric(dia1$carat[x])) & (as.numeric(dia1$carat[x])) < : missing value where TRUE/FALSE needed In addition: Warning messages: 1: NAs introduced by coercion 2: NAs introduced by coercion
# check if there are any NAs in the data
sum(is.na(dia1$carat))
[1] 0
Alternatively, why were there no NAs
introduced when the dia1$carat
vector was explicitly coerced to a character, but there were NAs
introduced in the back transformation?
cut(dia$carat, breaks=0:6, labels=c("0-1", "1-2", "2-3", "3-4", "4-5", "5-6"), include.lowest=TRUE)
will probably be a great deal more efficient. And, you should probably do arange(dia$carat)
to validate your assumptions. – hrbrmstrrange(dia$carat)
to validate your assumptions. Andfor
+if
== python/C/Java, not R. – hrbrmstr