Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
655 views
in Technique[技术] by (71.8m points)

matrix - rbind/data.frame conversion type hierarchy R

Could anyone systematically explain to me the hierarchy of type conversion between character/numeric/factor while using rbind and data.frame?

In my understanding, rbind puts together in a matrix, which can only have one type. So if there's a type conflict, what's the type that will get converted to? Do other types of matrix-creation function (e.g. cbind, matrix) work the same way? Example:

> sapply(rbind("a", "b"), class)
      a           b 
"character" "character" 
> sapply(rbind(1, "b"), class)
          1           b 
"character" "character" 

On the other hand, a data frame can hold multiple types, so data.frame preserves the original type, EXCEPT that it always tries to convert character into factors. (Is this correct? This is very counter-intuitive to me.)

With the same logic, is it correct that a factor type will always remain factor, no matter whether it is factor(c(1,2)) or factor(c("a", "b"))?

> sapply(data.frame("a", "b"), class)
    X.a.     X.b. 
"factor" "factor" 
> sapply(data.frame(1, "b"), class)
       X1      X.b. 
"numeric"  "factor"
> sapply(data.frame(1, factor("a")), class)
     X1 factor..a.. 
  "numeric"    "factor"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have a look at the Value section of ?cbind (or ?rbind):

"The type of a matrix result determined from the highest type of any of the inputs in the hierarchy raw < logical < integer < double < complex < character < list"

Some examples of coercion depending on the hierarchy:

# logical
a <- c(FALSE, TRUE)

# integer
b <- 0:1

# double
c <- c(0, 1.0)

# character
d <- c("0", "1")


m1 <- cbind(a, b)
m1
str(m1)
# logical converted to integer

m2 <- cbind(b, c)
m2
str(m2)
# integer converted to double

m3 <- cbind(c, d)
m3
str(m3)
# double converted to character

See also "Data frame methods" in ?cbind.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...