Creating a data frame from two vectors using cbind

前端 未结 2 1519
情书的邮戳
情书的邮戳 2020-12-08 00:43

Consider the following R code.

> x = cbind(c(10, 20), c(\"[]\", \"[]\"), c(\"[[1,2]]\",\"[[1,3]]\"))
> x
     [,1] [,2] [,3]     
[1,] \"10\" \"[]\" \"         


        
相关标签:
2条回答
  • 2020-12-08 01:37

    Vectors and matrices can only be of a single type and cbind and rbind on vectors will give matrices. In these cases, the numeric values will be promoted to character values since that type will hold all the values.

    (Note that in your rbind example, the promotion happens within the c call:

    > c(10, "[]", "[[1,2]]")
    [1] "10"      "[]"      "[[1,2]]"
    

    If you want a rectangular structure where the columns can be different types, you want a data.frame. Any of the following should get you what you want:

    > x = data.frame(v1=c(10, 20), v2=c("[]", "[]"), v3=c("[[1,2]]","[[1,3]]"))
    > x
      v1 v2      v3
    1 10 [] [[1,2]]
    2 20 [] [[1,3]]
    > str(x)
    'data.frame':   2 obs. of  3 variables:
     $ v1: num  10 20
     $ v2: Factor w/ 1 level "[]": 1 1
     $ v3: Factor w/ 2 levels "[[1,2]]","[[1,3]]": 1 2
    

    or (using specifically the data.frame version of cbind)

    > x = cbind.data.frame(c(10, 20), c("[]", "[]"), c("[[1,2]]","[[1,3]]"))
    > x
      c(10, 20) c("[]", "[]") c("[[1,2]]", "[[1,3]]")
    1        10            []                 [[1,2]]
    2        20            []                 [[1,3]]
    > str(x)
    'data.frame':   2 obs. of  3 variables:
     $ c(10, 20)              : num  10 20
     $ c("[]", "[]")          : Factor w/ 1 level "[]": 1 1
     $ c("[[1,2]]", "[[1,3]]"): Factor w/ 2 levels "[[1,2]]","[[1,3]]": 1 2
    

    or (using cbind, but making the first a data.frame so that it combines as data.frames do):

    > x = cbind(data.frame(c(10, 20)), c("[]", "[]"), c("[[1,2]]","[[1,3]]"))
    > x
      c.10..20. c("[]", "[]") c("[[1,2]]", "[[1,3]]")
    1        10            []                 [[1,2]]
    2        20            []                 [[1,3]]
    > str(x)
    'data.frame':   2 obs. of  3 variables:
     $ c.10..20.              : num  10 20
     $ c("[]", "[]")          : Factor w/ 1 level "[]": 1 1
     $ c("[[1,2]]", "[[1,3]]"): Factor w/ 2 levels "[[1,2]]","[[1,3]]": 1 2
    
    0 讨论(0)
  • 2020-12-08 01:43

    Using data.frame instead of cbind should be helpful

    x <- data.frame(col1=c(10, 20), col2=c("[]", "[]"), col3=c("[[1,2]]","[[1,3]]"))
    x
      col1 col2    col3
    1   10   [] [[1,2]]
    2   20   [] [[1,3]]
    
    sapply(x, class) # looking into x to see the class of each element
         col1      col2      col3 
    "numeric"  "factor"  "factor" 
    

    As you can see elements from col1 are numeric as you wish.

    data.frame can have variables of different class: numeric, factor and character but matrix doesn't, once you put a character element into a matrix all the other will become into this class no matter what clase they were before.

    0 讨论(0)
提交回复
热议问题