简体   繁体   中英

How to add certain rows from a column in one data.table to another data.table under a heading?

I am trying to paste certain values of a column of one data set (I need to be able to select 10-10k values dynamically from the first column) to another data set's column, but without overwriting the 2nd dataset's column value 1 (which is a header name). Sometimes it will be just part of a column and not the entire column. Some of the entries will be characters and some will be numerial.

data.table1: (no header row)

Row1: 21  32  77 ... 1293
Row2: 55  23  18 ... 2341 
Row3: 87  29  41 ... 8283
Rows++: ... (10k+rows long)

data.table2: (has header row)

Row1:    header1  header2  header3 ... header50
Row2:    123      char1    531     ... char5
Row3:    520      char2    488     ... 876 
Rows++: ... (10k+rows long)

Example of desired output, with column one containing values from data.table1 without overwriting header in row1 from data.table2:

data.table3:

Row1:    header1  header2  header3 ... header50
Row2:    21      char1    531     ... char5
Row3:    55      char2    488     ... 876    
Rows++: ... (10k+rows long)

I've found similar threads discussing using different methods to paste entire columns (meaning that the first row containing the header would be overwritten).

I will need to make many of these types of manipulations, and will also need to be able to do so for arbitrary data.table row locations. So instead of just working with row2-row50 of column1, I will need to be able to also adjust row50-750 in column10 for example. Is there a way to do this with data.table? Thank you

A good friend was able to provide some advice about moving one column to another including the header:

data.table2$V57[2:nrow(data.table2)] <- data.table1$header3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM