Functions cbind and rbind. How to remove quotes for the resulting data frame? For example:

my.vector<-c(1,2,3,4,5) my.vector2<-c("window","door","floor","door","floor") my.vector3<-c(6:10) cbind(my.vector,my.vector2,my.vector3) my.vector my.vector2 my.vector3 [1,] "1" "window" "6" [2,] "2" "door" "7" [3,] "3" "floor" "8" [4,] "4" "door" "9" [5,] "5" "floor" "10" 

Is there a way to get rid of quotes?

  • one
    If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

cbind returns an object of class matrix , and in a matrix there can be only values ​​of one type. In this case, one of the source vectors contains strings, so everything is cast to the character type. You can change the type of the column in the usual way:

 df <- as.data.frame(cbind(my.vector,my.vector2,my.vector3)) df[[1]] <- as.numeric(df[[1]]) str(df) 'data.frame': 5 obs. of 3 variables: $ my.vector : num 1 2 3 4 5 $ my.vector2: Factor w/ 3 levels "door","floor",..: 3 1 2 1 2 $ my.vector3: Factor w/ 5 levels "10","6","7","8",..: 2 3 4 5 1 

Or you need to immediately create a table using data.frame() .

For example :

 data.frame(my.vector,my.vector2,my.vector3) my.vector my.vector2 my.vector3 1 1 window 6 2 2 door 7 3 3 floor 8 4 4 door 9 5 5 floor 10 
  • thank! but why do we change the type of the 1st column, if we need to change the 2nd one? - Liz
  • In the sense - to change the second? There is a factor that can only be made an ordinary symbolic vector ( as.character(df[[2]]) ). A purely visual quotes will not be, if you directly as.data.frame(cbind(my.vector,my.vector2,my.vector3)) table as.data.frame(cbind(my.vector,my.vector2,my.vector3)) to the console. - Ogurtsov