Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine two vectors into a data frame

Tags:

dataframe

r

I have two vectors like this

 x <-c(1,2,3)  y <-c(100,200,300)  x_name <- "cond"  y_name <- "rating" 

I'd like to output the dataframe like this:

> print(df)       cond rating       1  x 1        2  x 2       3  x 3       4  y 100       5  y 200       6  y 300 

What's the way to do it?

like image 632
neversaint Avatar asked Jan 31 '13 07:01

neversaint


People also ask

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.

How do you combine two vectors?

The concatenation of vectors can be done by using combination function c. For example, if we have three vectors x, y, z then the concatenation of these vectors can be done as c(x,y,z). Also, we can concatenate different types of vectors at the same time using the same same function.


1 Answers

While this does not answer the question asked, it answers a related question that many people have had:

x <-c(1,2,3) y <-c(100,200,300) x_name <- "cond" y_name <- "rating"  df <- data.frame(x,y) names(df) <- c(x_name,y_name) print(df)    cond rating 1    1    100 2    2    200 3    3    300 
like image 165
Gx1sptDTDa Avatar answered Sep 19 '22 19:09

Gx1sptDTDa