Using data.table in R, can I select a vector of variable columns from a table?

Sinnombre

Suppose I have a table like:

> A = data.table(c(1,2,3),c(4,5,6),c(7,8,9))
> A
    V1 V2 V3
1:  1  4  7
2:  2  5  8
3:  3  6  9

I want to select the first column of the first row, second of the second row and third of the third row, getting the result:

> B
[1] 1 5 9

I've tried something like:

B = A[,c(1,2,3)]

But that does not work. Is it possible to do this? (Obviously in my actual use case the column indexes for each row are going to be variables, but I can guarantee a vector of length equal to the number of rows in the table)

akrun

A vectorized way of subsetting the elements is by providing the row index and column index as a matrix

as.data.frame(A)[cbind(seq_len(nrow(A)), 1:3)]
#[1] 1 5 9

Or convert the .SD to matrix or data.frame and use the row/column index

A[, as.matrix(.SD)[cbind(1:3, 1:3)]]

Or in data.table, pass the i, j index in a loop and extract the elements

A[, unlist(Map(function(i, j) .SD[i, j, with = FALSE], 1:3, 1:3), 
          use.names = FALSE)]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Data from one table to select data columns from another table, using r

Select columns in a data.table using vector of characters

Select columns in data table in R

R - How can I add columns to a data frame / data.table based on values of a vector that contains a sequence of dates

How can I select some columns of table?

Select subset of columns in data.table R

How to select duplicate columns data from table

How to select columns in data.table using a character vector of certain column names?

How can I select specific values from a table using Python?

Insert INTO using SELECT * from table with additional columns

How can I easily INSERT data in a table from multiple columns from another table?

Select columns in data.table based on logical vector

When selecting "top n" columns from data.table or head(), R returns levels from underlying vector

Select from table where columns names equal data in a different table

How can I use a variable from one table as a SELECT parameter for another table in php

Select rows from R table based on two columns in another table

How can I insert data into a child table from a parent table columns?

How to select columns and sum of columns using group by keyword from data table in c#

Selecting a subset of columns in R data.table using a vector with a constant column

R - How can i create graphs using an Aggregate data table?

Using the data from a for loop as a table in the From in a Select

How would I create a new table in excel using specific data from columns of another table

R: Using IDs from another table, replace these IDs in 2 columns in another table with the relevant data

R: gather gives warning when using vector variable to select columns

how can i insert into table via select data from another table for specific field

How can I select data from a mysql table, and then throw it into a html table

How can I scrape table from PHP website using R?

Sum columns of R data table, the names of which are stored in a vector

How to sort a data.table using vector of multiple columns