﻿ 使用cbind从两个向量创建数据帧 - 代码日志

#### 使用cbind从两个向量创建数据帧

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

``````> x = rbind(c(10, "[]", "[[1,2]]"), c(20, "[]", "[[1,3]]"))
> x
[,1] [,2] [,3]
[1,] "10" "[]" "[[1,2]]"
[2,] "20" "[]" "[[1,3]]"
``````

cbind帮助，也尝试了谷歌，但没有运气找到

(请注意，在您的rbind示例中，促销发生在c调用中：

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

``````> 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
``````

``````> 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
``````

``````> 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
``````
http://stackoverflow.com/questions/12787551/creating-a-data-frame-from-two-vectors-using-cbind