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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With