简体   繁体   中英

R: data.table : searching on multiple columns AND setting data type

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

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.

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)]

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