0

Q1:

Is it possible for me to search on two different columns in a data table. I have a 2 million odd row data and I want to have the option to search on either of the two columns. One has names and other has integers.

Example:

x <- data.table(foo=letters,bar=1:length(letters))
x

want to do
x['c'] : searching on foo column
as well as 
x[2]   : searching on bar column

Q2: Is it possible to change the default data types in a data table. I am reading in a matrix with both character and integer columns however everything is being read in as a character.

Thanks! -Abhi

2 Answers 2

3

To answer your Q2 first, a data.table is a data.frame, both of which are internally a list. Each column of the data.table (or data.frame) can therefore be of a different class. But you can't do that with a matrix. You can use := to change the class (by reference - no unnecessary copy being made), for example, of "bar" here:

x[, bar := as.integer(as.character(bar))]

For Q1, if you want to use fast subset (using binary search) feature of data.table, then you've to set key, using the function setkey.

setkey(x, foo)

allows you to fast-subset on 'x' alone like: x['a'] (or x[J('a')]). Similarly setting a key on 'bar' allows you to fast-subset on that column.

If you set the key on both 'foo' and 'bar' then you can provide values for both like so:

setkey(x) # or alternatively setkey(x, foo, bar)
x[J('c', 3)]

However, this'll subset those where x == 'c' and y == 3. Currently, I don't think there is a way to do a | operation with fast-subset directly. You'll have to resort to a vector-scan approach in that case.

Hope this is what your question was about. Not sure.

Sign up to request clarification or add additional context in comments.

Comments

1

Your matrix is already a character. Matrices hold only one data type. You can try X['c'] and X[J(2)]. You can change data types as X[,col := as.character(col)]

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.