1

My goal is to create a new dataframe with 3 columns using cbind. However, each time I try it turns into a chr, not a 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)

Somebody knows what I should do differently to create a dataframe?

Andrea
  • 41
  • 8

2 Answers2

2

We need data.frame instead of cbind as cbind creates a matrix and matrix can have only a single class i.e. if one of the elements is a character, all the elements are changed to character class

df1 <- data.frame(Species_bw, L_bw_visit, S_bw_visit, stringsAsFactors = FALSE)
akrun
  • 874,273
  • 37
  • 540
  • 662
2

cbind expects that all vectors are of the same type and converts everything to character if it has to.

Use data.frame instead:

> 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
Cettt
  • 11,460
  • 7
  • 35
  • 58