Rbind vector to be the first row in data.table


I need to assing a character vector to a data table at first row. I can get it to be in the last row but cannot find way to be on the first.

Character_vector <- colnames(DT)

DT_NEW <- rbind(DT ,as.list(Character_vector, ))


Best Wishes, Anitra

Can you please share a small part of the data set in a copy-paste friendly format?

In case you don't know how to do it, there are many options, which include:

  1. If you have stored the data set in some R object, dput function is very handy.

  2. In case the data set is in a spreadsheet, check out the datapasta package. Take a look at this link.

TEST_DT <- data.table(
  AMD_ID = c(1, 2, 3, 4),
  PM_BMI = c(30, 21, 24, 20),
  LAB_GGT = c(90, 80, 70, 80),
  SDC_GENDER = c(1, 2, 1, 2)

LABEL <- c("q123", "q124", "q125", "q126")
TEST_DT_NEW <- rbind(TEST_DT, as.list(LABEL, ))
TEST_DT_NEW[1, 1:4] <- TEST_DT_NEW[5, 1:4]

Hello! Is this ok? I have not been using R Studio Community for a while. But with these commands I now have the label row in both 1st and last row. In the real data I made column names a key row but I need to have the original labels below as 1st row.

Best Wishes, Anitra

What you are trying to do does not really make any sense. You are adding characters to numeric columns, but if you really need to add the label vector before the other rows:

TEST_DT_NEW <- rbind(as.list(LABEL), TEST_DT)

# or using data.table's function:
TEST_DT_NEW <- rbindlist(list(as.list(LABEL), TEST_DT))

Thank you so much! This saved me from losing my mind! My brains are tired and I was confused with the whole thing.


Got the solution.

Best Wishes, Anitra

In the real data I have the correct classes for the other variables.

Thank's again, Anitra

But of course now I see the problem now in the test data. The LABEL row should be character and the rest numeric and factors. Is that even possible?
I have 10 tables with different labels for variables and I made keys to harmonise, but I would like to keep the original labels below as well.


No, that was my point.

I think you need to understand dataframes (data.tables are just extended dataframes) before proceeding further.

Yes, back to basics and theory.

Thank's, Anitra

The whole idea turned out to be wrong. Character LABEL row below key column names does not work, because rest of the column values are numeric or factor values. I just thought it would be good to have original labels below key labels.

Best Wishes, Anitra

You can probably just rethink how you wanted to collect your data by flipping the rows and columns. How data is stored is separate from how you may wish to present it for output if that is why you came up with your original set-up.

This has already been quite a task. Sample collections are from 1992 to 2017 and made with different programs, regex and so on. I have to do some rethinking. Anyway, all the information, keys and original labels will be in codebook. I just want to be absolutely sure that everything matches correct.

Thank's again, Anitra

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.