3

I have a data.table with 47 variables looking at 5007 PhD student outcomes that looks something like this

sample<-data.table(PHD_STUDENT_ID=c(101:120),STUDY_LOCATION=c("Sydney","Canberra","Sydney","Sydney", 
    "Malaysia", "Malaysia", "CLF", "DRR", "GHS", "HMS", "DRJD", "KLS", "Malaysia",
    "Singapore", "Melbourne", "RD3S", "South Africa", "RME", "Sydney", "Canberra"),
    GRADE=c(51:70))

So the data.table looks something like this

PHD_STUDENT_ID      STUDY_LOCATION  GRADE
1   101             Sydney          51 
2   102             Canberra        52
3   103             Sydney          53
4   104             Sydney          54
5   105             Malaysia        55
6   106             Malaysia        56
7   107             CLF             57
8   108             DRR             58
.........

I need to retain all the rows except for the rows where the Study location is "Malaysia", "South Africa" or "Singapore". So basically all the values that are not at the Campuses in those countries. I have hundreds of unique values where the study location is just a code for a lab eg "CLF" and "DRR" which I want to retain so I can't just subset by Australia cities.

Any advice on how to subset this data table by reference to the values in STUDY_LOCATION are not "Malaysia", "South Africa" or "Singapore" would be greatly appreciated.

2 Answers 2

3

I assume you're learning data.table. Thus a data.table way is

setkey(sample, STUDY_LOCATION)
sample[!c('Malaysia', 'South Africa', 'Singapore')]
Sign up to request clarification or add additional context in comments.

1 Comment

Since 1.9.4, data.table uses secondary keys for automatically indexing columns to fast subset using binary search. This is a new feature. and currently implemented for DT[x == ...] and DT[X %in% ...].
3

You could try

   sample[!STUDY_LOCATION %in% c('Malaysia', 'South Africa', 'Singapore')]

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.