Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a data frame from two vectors using cbind

Tags:

dataframe

r

Consider the following R code.

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

Similarly

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

Now, I don't want the integers 10 and 20 to be converted to strings. How can I perform this operation without any such conversion? I would of course also like to know why this conversion happens. I looked at the cbind help and also tried Googling, but had no luck finding a solution. I also believe that in some cases. R converts strings to factors, and I don't want that to happen either, though it doesn't seem to be happening here.

like image 569
Faheem Mitha Avatar asked Oct 08 '12 18:10

Faheem Mitha


People also ask

Does Cbind create Dataframe?

The cbind function – short for column bind – is a merge function that can be used to combine two data frames with the same number of multiple rows into a single data frame.

How do you combine vectors into data frames?

Method 1: Using cbind() cbind() function stands for column-bind. This function can be used to combine several vectors, matrices, or DataFrames by columns. In this approach, a single vector is considered as one column and then these columns are combined to form a dataframe.

What is the use of Rbind () and Cbind () in R explain with example?

cbind() and rbind() both create matrices by combining several vectors of the same length. cbind() combines vectors as columns, while rbind() combines them as rows. Let's use these functions to create a matrix with the numbers 1 through 30.

What is the function of Cbind () in R programming?

cbind() function in R Language is used to combine specified Vector, Matrix or Data Frame by columns.


1 Answers

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 
like image 189
Brian Diggs Avatar answered Oct 06 '22 01:10

Brian Diggs