cbind不创建数据集,而是R中的字符向量

时间:2019-07-05 14:10:21

标签: r dataframe cbind

我的目标是使用cbind创建一个具有3列的新数据框。 但是,每次尝试时,它会变成chr,而不是data.frame。

L_bw_visit <- c(24.68130,   175.1266,   1.47421,    2.714970,   2.080063,   42.06174,   17.29546,
                51.98034,   0.8140756,  0.7157895)
S_bw_visit <- c(29.26038,   120.9192,   5.04845,    3.742081,   3.654214,   37.53483,   21.65989,
                44.27059,   4.5231854,  3.0566597)
Species_bw <- c("Central American Spiny rat", "Common opossum","Agouti", "White-nosed coati",
                "Northern tamandua","Nine-banded armadillo", "Lowland paca", "Ocelot",
                "Red brocket deer", "Collared peccary")
paired_bw_visit<- cbind(Species_bw, L_bw_visit, S_bw_visit)

有人知道我应该怎样做才能创建一个数据框?

2 个答案:

答案 0 :(得分:2)

我们需要// Ionicons link(rel='stylesheet', href="/bower_components/Ionicons/css/ionicons.min.css") // jvectormap link(rel='stylesheet', href="/bower_components/jvectormap/jquery-jvectormap.css") // Theme style link(rel='stylesheet', href="/dist/css/AdminLTE.min.css") ... ... ... 而不是data.frame,因为cbind创建了cbind,矩阵只能有一个类,即如果元素之一是字符,则所有元素更改为角色类

matrix

答案 1 :(得分:2)

cbind期望所有向量都具有相同的类型,并在需要时将所有内容都转换为字符。

改为使用data.frame

> data.frame(Species_bw, L_bw_visit, S_bw_visit)
                   Species_bw  L_bw_visit S_bw_visit
1  Central American Spiny rat  24.6813000  29.260380
2              Common opossum 175.1266000 120.919200
3                      Agouti   1.4742100   5.048450
4           White-nosed coati   2.7149700   3.742081
5           Northern tamandua   2.0800630   3.654214
6       Nine-banded armadillo  42.0617400  37.534830
7                Lowland paca  17.2954600  21.659890
8                      Ocelot  51.9803400  44.270590
9            Red brocket deer   0.8140756   4.523185
10           Collared peccary   0.7157895   3.056660