I think I finally got the answer for my question above! This answer overcome the issue of 'not vectorized' as mentioned by @Roland, thank you sir! And it is in my opinion is much faster even though it took me literally couple of weeks to understand the concept and finding the right questions on the web!
First, I create a new data.table that consist of 2 columns, one with the original name and the second is the desired name for the school.
lookUpStratum <- data.table(STRATUM=c("MYS - stratum 01: MOE National Secondary School\\Other States",
"MYS - stratum 02: MOE Religious School\\Other States",
"MYS - stratum 03: MOE Technical School\\Other States",
"MYS - stratum 04: MOE Fully Residential School",
"MYS - stratum 05: non-MOE MARA Junior Science College\\Other States",
"MYS - stratum 06: non-MOE Other Schools\\Other States",
"MYS - stratum 07: Perlis non-“MOE Fully Residential Schools”",
"MYS - stratum 08: Wilayah Persekutuan Putrajaya non-“MOE Fully Residential Schools”",
"MYS - stratum 09: Wilayah Persekutuan Labuan non-“MOE Fully Residential Schools”"),
SCH.TYPE=c("Public",
"Religious",
"Technical",
"SBP",
"MARA",
"Private",
"Perlis Fully Residential",
"Putrajaya Fully Residential",
"Labuan Fully Residential"))
The answer lies on the setDT
(Coerce lists and data.frames to data.table by reference).
Using this line of code I read here, it looks kinda long but it solved my problem! And to be honest I understand this first before I understand the shortest one code below.
setDT(pisaMalaysia)[,SCH.TYPE := lookUpStratum$SCH.TYPE[match(pisaMalaysia$STRATUM,lookUpStratum$STRATUM)]]
After a few minutes I finally managed to get my head around this code here and produced this code:
setDT(pisaMalaysia)[lookUpStratum,SCH.TYPE1 := i.SCH.TYPE, on = c(STRATUM = "STRATUM")]
I got these answers from the same post here.
To check if everything is the same:
table(pisaMalaysia$SCH.TYPE)
table(pisaMalaysia$SCH.TYPE1)
#' original data
pisaMalaysia[,table(STRATUM)]
results:
> table(pisaMalaysia$SCH.TYPE)
Labuan Fully Residential MARA Perlis Fully Residential
54 122 78
Private Public Putrajaya Fully Residential
385 4929 78
Religious SBP Technical
273 2661 281
> table(pisaMalaysia$SCH.TYPE1)
Labuan Fully Residential MARA Perlis Fully Residential
54 122 78
Private Public Putrajaya Fully Residential
385 4929 78
Religious SBP Technical
273 2661 281
> pisaMalaysia[,table(STRATUM)]
STRATUM
MYS - stratum 01: MOE National Secondary School\\Other States
4929
MYS - stratum 02: MOE Religious School\\Other States
273
MYS - stratum 03: MOE Technical School\\Other States
281
MYS - stratum 04: MOE Fully Residential School
2661
MYS - stratum 05: non-MOE MARA Junior Science College\\Other States
122
MYS - stratum 06: non-MOE Other Schools\\Other States
385
MYS - stratum 07: Perlis non-“MOE Fully Residential Schools”
78
MYS - stratum 08: Wilayah Persekutuan Putrajaya non-“MOE Fully Residential Schools”
78
MYS - stratum 09: Wilayah Persekutuan Labuan non-“MOE Fully Residential Schools”
54
Thanks! Hope this will help others too.