3

Is there a way to create a dataframe from 2 vectors row wise?

list1<- c('a','b','c')
list2<- c('x','y','z')

I'd like to create a DF from these 2 lists such that each of these 2 lists is a row in the DF.

1
  • 2
    data.frame(rbind(list1, list2)) Commented Apr 17, 2021 at 7:40

2 Answers 2

5

One option is data.frame

data.frame(x = list1, y = list2)
#   x y
#1 a x
#2 b y
#3 c z

Or if it should be otherway, use rbind

setNames(rbind.data.frame(list1, list2), c("x", "y"))
Sign up to request clarification or add additional context in comments.

2 Comments

no, out of the 2 suggestions you provided, 2nd one works fine. Thanks for your help.
@user3510503 it is better to provide example that mimics the correct structure of your data.
3

You could also use bind_rows

df <- bind_rows(x = list1, y = list2)

Output:

  x     y    
  <chr> <chr>
1 a     x    
2 b     y    
3 c     z   

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.