32

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?

8 Answers 8

72

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
2
  • 3
    your output is different from the expectation of the question. I don't get it
    – TSR
    Commented Oct 14, 2016 at 9:00
  • I think this answers a question that other people have. I just updated the answer to make note of that.
    – fny
    Commented Sep 26, 2017 at 19:39
17
x <-c(1,2,3)
y <-c(100,200,300)
x_name <- "cond"
y_name <- "rating"

require(reshape2)
df <- melt(data.frame(x,y))
colnames(df) <- c(x_name, y_name)
print(df)

UPDATE (2017-02-07): As an answer to @cdaringe comment - there are multiple solutions possible, one of them is below.

library(dplyr)
library(magrittr)

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

# Helper function to create data.frame for the chunk of the data
prepare <- function(name, value, xname = x_name, yname = y_name) {
  data_frame(rep(name, length(value)), value) %>%
    set_colnames(c(xname, yname))
}

bind_rows(
  prepare("x", x),
  prepare("y", y),
  prepare("z", z)
)
1
  • do you know how this would change if there was also a component such as z <-c(1,2,3,4,5)? the dimensionality is different, hence the dataframe in this solution doesn't take on creation.
    – cdaringe
    Commented Feb 6, 2017 at 6:20
12

This should do the trick, to produce the data frame you asked for, using only base R:

df <- data.frame(cond=c(rep("x", times=length(x)), 
                        rep("y", times=length(y))), 
                 rating=c(x, y))

df
  cond rating
1    x      1
2    x      2
3    x      3
4    y    100
5    y    200
6    y    300

However, from your initial description, I'd say that this is perhaps a more likely usecase:

df2 <- data.frame(x, y)
colnames(df2) <- c(x_name, y_name)

df2
  cond rating
1    1    100
2    2    200
3    3    300

[edit: moved parentheses in example 1]

2
  • One bracketing error -- corrected code: df <- data.frame(cond=c(rep("x", times=length(x)), rep("y", times=length(y))), rating=c(x, y))
    – maia
    Commented Sep 28, 2014 at 18:33
  • Exactly what I needed! Been scratching my head over combining multiple vectors into a data.frame that worked. Thanks!!!
    – mightypile
    Commented Oct 9, 2014 at 14:20
10

You can use expand.grid( ) function.

x <-c(1,2,3)
y <-c(100,200,300)
expand.grid(cond=x,rating=y)
2
  • 1
    This is the simplest, and therefore the best, method. It solves the problem with a one-line call to a base R function.
    – Argent
    Commented Feb 7, 2017 at 21:43
  • this is not a correct answer to the question. here data.frame(x, y) should be used to create a table of x and y. while expand.grid is creating a data frame from all combinations of the supplied vectors or factors, which is not the case here.
    – Ali Safari
    Commented Nov 12, 2020 at 22:18
3

Here's a simple function. It generates a data frame and automatically uses the names of the vectors as values for the first column.

myfunc <- function(a, b, names = NULL) {
  setNames(data.frame(c(rep(deparse(substitute(a)), length(a)), 
                        rep(deparse(substitute(b)), length(b))), c(a, b)), names)
}

An example:

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

myfunc(x, y, c(x_name, y_name))

  cond rating
1    x      1
2    x      2
3    x      3
4    y    100
5    y    200
6    y    300
2

With expand_grid from tidyverse:

library(tidyr)

tidyr::expand_grid(cond = c(1,2,3), ratings = c(100,200,300))
3
  • 1
    Not exactly – Vanilla R: expand.grid; tidyr: expand_grid Commented Feb 1 at 10:52
  • 1
    Apology, I entirely overlooked
    – benson23
    Commented Feb 1 at 10:58
  • 1
    No problem, I made the answer a bit clearer Commented Feb 1 at 13:15
1

df = data.frame(cond=c(rep("x",3),rep("y",3)),rating=c(x,y))

1

Alt simplification of https://stackoverflow.com/users/1969435/gx1sptdtda above:

cond <-c(1,2,3)
rating <-c(100,200,300)
df <- data.frame(cond, rating)
df
  cond rating
1    1    100
2    2    200
3    3    300

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.