Home > Mobile >  Why R is accepting numeric value stored inside vector as character
Why R is accepting numeric value stored inside vector as character

Time:09-01

I'm new to R and while executing this code, getting weird result. Can someone explain what is happening?

workshop <- c(1,2,1,2,1,2,1,2)
gender <- c("f","f","f",NA,"m","m","m","m")
q1 <- c(1,2,2,3,4,5,5,4)
q2 <- c(1,1,2,1,5,4,3,5)
q3 <- c(5,4,4,NA,2,5,4,5)
q4 <- c(1,1,3,3,4,5,4,5)
gender[ workshop==2 ] # shows the gender of people who took workshop 2
gender[ workshop=="2" ] #even this works but 2 is not a character

CodePudding user response:

Type conversion issue. workshop=="2" is actually as.character(workshop)=="2".

More surprise?

1 < "3"

1 < "03"
  • Related