Create a tibble by column names another tibble

I have a ''' tb <- tibble() ''' and another tibble by 100 column names. I want to only assign colnames in tibble by 100 column names to tb, and get value 1:100

I don't understand your requirements. Before we build up to the 100 case, can you give a full example of what you want to start with and where you want to get to , if instead of 100 it was 3?

Suppose we have a empty tibble and another tibbe is '''tb <-tibble(x1 =1, x2 =2,..., xn = n)'''. Now i want to get all colnames tb and assign this colnames to empty tibble.

There is not really a concept of empty tibbles. A tibble variable must have a type. When assigning column names with no rows associated, what variable types are you wanting these columns to be?

You could simply apply dplyr::filter(mydf, FALSE) to throw away all the rows from your df. You could assignt that to a an object called tb if you wanted to.

My column variable type can get any type.
I can't use any way to add colnames to empty tb?

You can only make a column with a definite type.
What real problem are you trying to solve with this ?

But i want to get all column by all type.
I have a tibble that save in time 1 and another tibble that save in time 2 and..., my tibbles are different dimentions (my column is same and row is different). I want to merge this tibbles by unique rows. My idea was create a empty tibble and use ''' Reduce(function (x, y) merge(x, y, all=T), list(empty tb, tibble time1, tibble time2,...) '''.

I think you are describing that you want to take two dataframes with same columns but different rows and stack them vertically ? If so this is easy also.. functions like bind_rows() and union() etc exist to do this. If something else a simple example would help.

Thsnks, its useful. But the first that give data my data is one tibble and i have'nt the second tibble, so i wanted to create an empty tibble that when get first tibble, then use it for merge.

I'm sorry but you don't make sense to me.
If you only have one tibble. What can it merge with ?

I use empty tibble for first arg and when time 1 code run, then save tibble and use this tibble for merge by empty tibble and save result to empty tibble and then time 2 we use of result for first arg and tibble time 2 for second arg and so on.

Sorry, I will let someone else assist you.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.