Problems converting NA to numeric in a data frame in R - r

I have a data frame with both numeric values and characters. The NAs are not displayed as NA defined by R, but as characters. How can I convert the characters NA to the numeric value 0? I still want the columns to show 0, because I do not want to remove them from my data frame.
I have tried
df[is.na(df)] <-0
but it only returns "0" as characters, but not values.
df <- as.numeric(as.character(df))
gives me the warning message:
NAs introduced by coercion
Are there other solutions available? Thank you.
Here is a small reproducible example:
structure(list(DNB = c(2.05, 2.05, 2.06, 2.32, 2.32, 2.32), `NORSK HYDRO` =
c(2.59,
2.59, 2.65, 2.81, 2.63, 2.63), ORKLA = c(2.29, 2.29, 2.18, 2.31,
2.25, 2.25), STOREBRAND = c(2.28, 2.28, 2.56, 2.88, 2.94, 2.94
), ATEA = c(2.25, 2.25, 2, 2, 2, 2), `SCHIBSTED A` = c(3.23,
3.23, 3.08, 2.92, 2.92, 2.92), BONHEUR = c(2, 2, 2, 2, 2, 2),
EKORNES = c(2.25, 2.25, 2.25, 2.25, 2.25, 2.25), `KONGSBERG GRUPPEN` =
c(2.8,
2.8, 2.5, 2.5, 2.5, 2.5), `TOMRA SYSTEMS` = c(2.43, 2.43,
2.29, 2.29, 2.29, 2.29), VEIDEKKE = c(2.33, 2.33, 2.5, 2.5,
2.33, 2.33), `ARENDALS FOSSEKOMPANI` = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), `OLAV THON EIEP.` = c(3, 3, 2.8, 2.8, 2.8, 2.8), `PETROLEUM GEO SERVICES` = c(3.13,
3.13, 2.86, 2.63, 2.63, 2.63), `SPAREBANK 1 SR BANK` = c(3,
3, 3, 3, 3, 3), `STOLT-NIELSEN` = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), `ODFJELL 'A'` = c(2.45, 2.45,
2.4, 2.6, 2.4, 2.4), `SPAREBANK 1 NORD-NORGE` = c(3, 3, 3,
3, 3, 3), `SPAREBANK 1 SMN` = c(3, 3, 3, 3, 3, 3), `WILHS.WILHELMSEN HDG.'A'` = c(2.67,
2.67, 2.78, 2.67, 2.67, 2.67), `NORDEA BANK (~NK)` = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `ATLAS COPCO 'A' (~NK)` = c(3.08,
3.08, 3.1, 2.95, 2.95, 2.95), `VOLVO 'B' (~NK)` = c(3.13,
3.13, 3.17, 2.79, 2.59, 2.59), `SANDVIK (~NK)` = c(3, 3,
2.75, 3.04, 3.09, 3.09), `SWEDBANK 'A' (~NK)` = c(2.29, 2.29,
2.21, 2.05, 2.1, 2.1), `ERICSSON 'B' (~NK)` = c(2.33, 2.33,
2.38, 2.52, 2.44, 2.44), `SVENSKA HANDBKN.'A' (~NK)` = c(2.32,
2.32, 2.33, 2.55, 2.55, 2.55), `HENNES & MAURITZ 'B' (~NK)` = c(3.35,
3.35, 3.42, 3.17, 3.06, 3.06), `SEB 'A' (~NK)` = c(2.9, 2.9,
2.9, 3, 3.09, 3.09), `INVESTOR 'B' (~NK)` = c(2.47, 2.47,
2.38, 2.69, 2.62, 2.62), `SWEDISH MATCH (~NK)` = c(2.08,
2.08, 1.83, 1.69, 1.69, 1.69), `ELECTROLUX 'B' (~NK)` = c(3.38,
3.38, 3.23, 3.13, 3.13, 3.13), `SKANSKA 'B' (~NK)` = c(2.5,
2.5, 2.43, 2.85, 2.86, 2.86), `SCA 'B' (~NK)` = c(2.96, 2.96,
2.87, 2.64, 2.55, 2.55), `SECURITAS 'B' (~NK)` = c(3.64,
3.64, 3.78, 4, 4, 4), `HOLMEN 'B' (~NK)` = c(3.16, 3.16,
3.26, 3.05, 3.24, 3.24), `SSAB 'A' (~NK)` = c(2.33, 2.33,
2.29, 2.41, 2.41, 2.41), `ERICSSON 'A' (~NK)` = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_), `INVESTOR 'A' (~NK)` = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), `VOLVO 'A' (~NK)` = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), `NOVO NORDISK 'B' (~NK)` = c(2.52,
2.52, 2.55, 2.64, 2.55, 2.55), `DANSKE BANK (~NK)` = c(2.12,
2.12, 2.38, 2.53, 2.58, 2.58), `COLOPLAST 'B' (~NK)` = c(3.8,
3.8, 4.13, 4.13, 4.13, 4.13), `CARLSBERG 'B' (~NK)` = c(3.11,
3.11, 3.06, 3.24, 3.24, 3.24), `A P MOLLER - MAERSK 'B' (~NK)` = c(2.89,
2.89, 2.75, 2.63, 2.75, 2.75), `TDC (~NK)` = c(2.93, 2.93,
2.96, 2.96, 3.04, 3.04), `TOPDANMARK (~NK)` = c(2.78, 2.78,
2.56, 2.8, 2.8, 2.8), `WILLIAM DEMANT HLDG. (~NK)` = c(4,
4, 3.78, 4, 3.78, 3.78), `JYSKE BANK (~NK)` = c(1.5, 1.5,
1.5, 1.5, 1.5, 1.5), `KOBENHAVNS LUFTHAVNE (~NK)` = c(2.56,
2.56, 2.47, 2.75, 2.56, 2.56), `NKT (~NK)` = c(2.25, 2.25,
2.25, 2.25, 2.25, 2.25), `ROCKWOOL 'B' (~NK)` = c(3.25, 3.25,
3, 3, 3, 3), `SYDBANK (~NK)` = c(3.6, 3.6, 3.2, 4, 4, 4),
`FLSMIDTH & CO.'B' (~NK)` = c(2.6, 2.6, 2.4, 2.4, 2.4, 2.4
), `GN STORE NORD (~NK)` = c(3, 3, 2.78, 2.89, 3.11, 3.11
), `ALK-ABELLO (~NK)` = c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), `BANG & OLUFSEN 'B' (~NK)` = c(4, 4,
3.67, 3.22, 3.22, 3.22), `SANTA FE GROUP (~NK)` = c(3.5,
3.5, 3.4, 3.22, 3.44, 3.44), `CARLSBERG 'A' (~NK)` = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `ROCKWOOL 'A' (~NK)` = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `NOKIA (~NK)` = c(1.89,
1.89, 2.04, 1.86, 1.81, 1.81), `SAMPO 'A' (~NK)` = c(2.08,
2.08, 2, 2.36, 2.36, 2.36), `KONE 'B' (~NK)` = c(3.71, 3.71,
3.77, 3.67, 3.64, 3.64), `UPM-KYMMENE (~NK)` = c(2.43, 2.43,
2.45, 2.09, 2.04, 2.04), `WARTSILA (~NK)` = c(2.13, 2.13,
2.07, 2.07, 2.07, 2.07), `METSO (~NK)` = c(2.41, 2.41, 2.41,
2.47, 2.47, 2.47), `STORA ENSO 'R' (~NK)` = c(2.76, 2.76,
2.95, 2.74, 2.57, 2.57), `HUHTAMAKI (~NK)` = c(2.33, 2.33,
2.13, 2.25, 2.25, 2.25), `FINNAIR (~NK)` = c(3, 3, 3, 2.92,
2.92, 2.92), `KEMIRA (~NK)` = c(2.4, 2.4, 2.4, 2.67, 2.8,
2.8), `UPONOR (~NK)` = c(2, 2, 2, 1.8, 1.8, 1.8), `KESKO 'B' (~NK)` = c(2.45,
2.45, 3.09, 2.58, 2.67, 2.67), `ORION 'B' (~NK)` = c(2.57,
2.57, 2.57, 2.63, 2.63, 2.63), `OUTOKUMPU 'A' (~NK)` = c(3.31,
3.31, 3.31, 3, 2.63, 2.63), `RAISIO (~NK)` = c(2.91, 2.91,
3.09, 3.08, 3, 3), `TIETO OYJ (~NK)` = c(2, 2, 2.11, 2.4,
2.4, 2.4), `METSA BOARD 'B' (~NK)` = c(3.26, 3.26, 3.32,
3.14, 2.84, 2.84), `ORION 'A' (~NK)` = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), `STOCKMANN 'A' (~NK)` = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), `STORA ENSO 'A' (~NK)` = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
)), .Names = c("DNB", "NORSK HYDRO", "ORKLA", "STOREBRAND",
"ATEA", "SCHIBSTED A", "BONHEUR", "EKORNES", "KONGSBERG GRUPPEN",
"TOMRA SYSTEMS", "VEIDEKKE", "ARENDALS FOSSEKOMPANI", "OLAV THON EIEP.",
"PETROLEUM GEO SERVICES", "SPAREBANK 1 SR BANK", "STOLT-NIELSEN",
"ODFJELL 'A'", "SPAREBANK 1 NORD-NORGE", "SPAREBANK 1 SMN",
"WILHS.WILHELMSEN HDG.'A'",
"NORDEA BANK (~NK)", "ATLAS COPCO 'A' (~NK)", "VOLVO 'B' (~NK)",
"SANDVIK (~NK)", "SWEDBANK 'A' (~NK)", "ERICSSON 'B' (~NK)",
"SVENSKA HANDBKN.'A' (~NK)", "HENNES & MAURITZ 'B' (~NK)", "SEB 'A' (~NK)",
"INVESTOR 'B' (~NK)", "SWEDISH MATCH (~NK)", "ELECTROLUX 'B' (~NK)",
"SKANSKA 'B' (~NK)", "SCA 'B' (~NK)", "SECURITAS 'B' (~NK)",
"HOLMEN 'B' (~NK)", "SSAB 'A' (~NK)", "ERICSSON 'A' (~NK)", "INVESTOR 'A'
(~NK)",
"VOLVO 'A' (~NK)", "NOVO NORDISK 'B' (~NK)", "DANSKE BANK (~NK)",
"COLOPLAST 'B' (~NK)", "CARLSBERG 'B' (~NK)", "A P MOLLER - MAERSK 'B'
(~NK)",
"TDC (~NK)", "TOPDANMARK (~NK)", "WILLIAM DEMANT HLDG. (~NK)",
"JYSKE BANK (~NK)", "KOBENHAVNS LUFTHAVNE (~NK)", "NKT (~NK)",
"ROCKWOOL 'B' (~NK)", "SYDBANK (~NK)", "FLSMIDTH & CO.'B' (~NK)",
"GN STORE NORD (~NK)", "ALK-ABELLO (~NK)", "BANG & OLUFSEN 'B' (~NK)",
"SANTA FE GROUP (~NK)", "CARLSBERG 'A' (~NK)", "ROCKWOOL 'A' (~NK)",
"NOKIA (~NK)", "SAMPO 'A' (~NK)", "KONE 'B' (~NK)", "UPM-KYMMENE (~NK)",
"WARTSILA (~NK)", "METSO (~NK)", "STORA ENSO 'R' (~NK)", "HUHTAMAKI (~NK)",
"FINNAIR (~NK)", "KEMIRA (~NK)", "UPONOR (~NK)", "KESKO 'B' (~NK)",
"ORION 'B' (~NK)", "OUTOKUMPU 'A' (~NK)", "RAISIO (~NK)", "TIETO OYJ (~NK)",
"METSA BOARD 'B' (~NK)", "ORION 'A' (~NK)", "STOCKMANN 'A' (~NK)",
"STORA ENSO 'A' (~NK)"), row.names = c(NA, 6L), class = "data.frame")

We can loop over the columns of dataset, replace the NAs with 0 and convert it to numeric (as there are some character columns)
df[] <- lapply(df, function(x) as.numeric(replace(x, is.na(x), 0)))
The OP's method of replacing the NAs with 0 first should also work, but the character columns remain as character unless we change it
df[is.na(df)] <-0
df[] <- lapply(df, as.numeric)
Here, we don't have any factor columns, so as.character is not needed. Note that as.character/as.numeric are applied on vector/columns and not on the entire dataset

Related

Match data from two different data frames in R like Excel VLOOKUP

I am trying to pull data from one data frame to another based on the equivalent of a VLOOKUP table in Excel. I have had a look at the most popular VLOOKUP question in R, but I cannot see how it applies to my specific problem. The key thing is that I don't want to pull all of the columns from the second data frame into my first one - I only want to pull in one column. I'm pretty sure this will be some kind of derivation of a merge function.
Referring to the below data, I am trying to create a new column called df1$Trait1Percentile. This needs to draw from LookupTable$Trait1Percentiles based on a match between df1$Trait1Scores and
LookupTable$Scores.
#Import data.
df1 <- structure(list(JobNumber = c(634L, 21L, 300L, 797L, 1112L, 147L,
1L, 4L, 260L, 194L, 981L, 1110L, 634L, 554L, 213L, 722L, 1036L,
855L, 624L, 1113L, 681L, 547L, 195L, 624L, 546L, 201L, 918L,
1069L, 300L, 294L, 587L, 933L, 918L, 620L, 918L, 298L, 749L,
295L, 635L, 515L, 624L, 147L, 200L, 527L, 800L, 827L, 4L, 568L,
252L, 655L, 559L, 629L, 639L, 933L, 214L, 750L, 1066L, 495L,
1113L, 1L, 1113L, 12L, 561L, 741L, 495L, 981L, 147L, 199L, 629L,
163L, 615L, 294L, 49L, 624L, 260L, 1L, 299L, 193L, 108L, 113L,
426L, 299L, 708L, 749L, 749L, 483L, 935L, 1036L, 295L, 12L, 1113L,
1038L, 4L, 973L, 448L, 295L, 197L, 76L, 1L, 1L), Trait1Score = c(3.89,
4.39, 4.22, 4.21, 3.94, 3.9, 4.58, 4.5, 4.29, 4.47, 4.41, 4.4,
4.14, 4.78, 4.09, 4.58, 4.27, 4.24, 3.96, 3.94, 4.3, 4.07, 4.28,
4.19, 4.57, 4.74, 3.29, 4.23, 3.51, 3.77, 4.46, 5.04, 4.25, 3.92,
3.78, 4.43, 4.12, 4.18, 4.63, 3.25, 3.87, 4.4, 3.83, 4.03, 3.42,
4.9, 4.09, 4.58, 4.29, 4.7, 4.38, 4.61, 4.41, 4.5, 4.6, 4.22,
3.72, 4.34, 4.34, 4.38, 4.15, 4.22, 3.93, 5, 3.81, 4.3, 4.6,
4.96, 4.29, 4.8, 5.05, 3.76, 4.81, 4.77, 4.25, 4.17, 4.75, 4.15,
4.35, 4.23, 5.31, 4.18, 3.67, 3.84, 4.06, 3.66, 3.58, 4.37, 4.43,
4.63, 4.74, 4.79, 5.04, 3.55, 3.64, 4.9, 4.38, 4.01, 4.47, 4.53
), Trait2Score = c(4, 2.94, 3.17, 3.83, 4.22, 3.83, 5.11, 3,
2.83, 2.78, 2.22, 2.22, 4.11, 2.39, 2.22, 2.06, 2.89, 3.61, 3.89,
4.89, 3.78, 4.22, 4.5, 4.39, 1.89, 4.78, 4.56, 3.78, 2.28, 4.61,
2.72, 1.89, 4.44, 4.06, 3.72, 2.44, 3.61, 2.06, 2.17, 6.44, 3.22,
2.78, 4.61, 2.72, 2.83, 2.44, 6.5, 2.28, 2.89, 2.11, 4.44, 2.83,
3, 6.33, 3.11, 3.17, 3.67, 4.5, 2.5, 4.33, 5, 2.89, 3.89, 1.72,
3.33, 4.28, 2.17, 3.17, 2.61, 2.89, 1.22, 3.39, 1.28, 2.61, 2.5,
4.56, 2.89, 4.89, 3.11, 3.5, 1.44, 2.39, 5.33, 3.78, 1.5, 3.44,
5.83, 3.17, 3.78, 2.67, 1.61, 1.83, 4.56, 4.67, 4.61, 2.5, 4.94,
3.94, 4.33, 2.72)), row.names = c(NA, -100L), class = "data.frame")
LookupTable <- structure(list(Scores = c(0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06,
0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17,
0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28,
0.29, 0.3, 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39,
0.4, 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5,
0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6, 0.61,
0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7, 0.71, 0.72,
0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8, 0.81, 0.82, 0.83,
0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9, 0.91, 0.92, 0.93, 0.94,
0.95, 0.96, 0.97, 0.98, 0.99, 1, 1.01, 1.02, 1.03, 1.04, 1.05,
1.06, 1.07, 1.08, 1.09, 1.1, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16,
1.17, 1.18, 1.19, 1.2, 1.21, 1.22, 1.23, 1.24, 1.25, 1.26, 1.27,
1.28, 1.29, 1.3, 1.31, 1.32, 1.33, 1.34, 1.35, 1.36, 1.37, 1.38,
1.39, 1.4, 1.41, 1.42, 1.43, 1.44, 1.45, 1.46, 1.47, 1.48, 1.49,
1.5, 1.51, 1.52, 1.53, 1.54, 1.55, 1.56, 1.57, 1.58, 1.59, 1.6,
1.61, 1.62, 1.63, 1.64, 1.65, 1.66, 1.67, 1.68, 1.69, 1.7, 1.71,
1.72, 1.73, 1.74, 1.75, 1.76, 1.77, 1.78, 1.79, 1.8, 1.81, 1.82,
1.83, 1.84, 1.85, 1.86, 1.87, 1.88, 1.89, 1.9, 1.91, 1.92, 1.93,
1.94, 1.95, 1.96, 1.97, 1.98, 1.99, 2, 2.01, 2.02, 2.03, 2.04,
2.05, 2.06, 2.07, 2.08, 2.09, 2.1, 2.11, 2.12, 2.13, 2.14, 2.15,
2.16, 2.17, 2.18, 2.19, 2.2, 2.21, 2.22, 2.23, 2.24, 2.25, 2.26,
2.27, 2.28, 2.29, 2.3, 2.31, 2.32, 2.33, 2.34, 2.35, 2.36, 2.37,
2.38, 2.39, 2.4, 2.41, 2.42, 2.43, 2.44, 2.45, 2.46, 2.47, 2.48,
2.49, 2.5, 2.51, 2.52, 2.53, 2.54, 2.55, 2.56, 2.57, 2.58, 2.59,
2.6, 2.61, 2.62, 2.63, 2.64, 2.65, 2.66, 2.67, 2.68, 2.69, 2.7,
2.71, 2.72, 2.73, 2.74, 2.75, 2.76, 2.77, 2.78, 2.79, 2.8, 2.81,
2.82, 2.83, 2.84, 2.85, 2.86, 2.87, 2.88, 2.89, 2.9, 2.91, 2.92,
2.93, 2.94, 2.95, 2.96, 2.97, 2.98, 2.99, 3, 3.01, 3.02, 3.03,
3.04, 3.05, 3.06, 3.07, 3.08, 3.09, 3.1, 3.11, 3.12, 3.13, 3.14,
3.15, 3.16, 3.17, 3.18, 3.19, 3.2, 3.21, 3.22, 3.23, 3.24, 3.25,
3.26, 3.27, 3.28, 3.29, 3.3, 3.31, 3.32, 3.33, 3.34, 3.35, 3.36,
3.37, 3.38, 3.39, 3.4, 3.41, 3.42, 3.43, 3.44, 3.45, 3.46, 3.47,
3.48, 3.49, 3.5, 3.51, 3.52, 3.53, 3.54, 3.55, 3.56, 3.57, 3.58,
3.59, 3.6, 3.61, 3.62, 3.63, 3.64, 3.65, 3.66, 3.67, 3.68, 3.69,
3.7, 3.71, 3.72, 3.73, 3.74, 3.75, 3.76, 3.77, 3.78, 3.79, 3.8,
3.81, 3.82, 3.83, 3.84, 3.85, 3.86, 3.87, 3.88, 3.89, 3.9, 3.91,
3.92, 3.93, 3.94, 3.95, 3.96, 3.97, 3.98, 3.99, 4, 4.01, 4.02,
4.03, 4.04, 4.05, 4.06, 4.07, 4.08, 4.09, 4.1, 4.11, 4.12, 4.13,
4.14, 4.15, 4.16, 4.17, 4.18, 4.19, 4.2, 4.21, 4.22, 4.23, 4.24,
4.25, 4.26, 4.27, 4.28, 4.29, 4.3, 4.31, 4.32, 4.33, 4.34, 4.35,
4.36, 4.37, 4.38, 4.39, 4.4, 4.41, 4.42, 4.43, 4.44, 4.45, 4.46,
4.47, 4.48, 4.49, 4.5, 4.51, 4.52, 4.53, 4.54, 4.55, 4.56, 4.57,
4.58, 4.59, 4.6, 4.61, 4.62, 4.63, 4.64, 4.65, 4.66, 4.67, 4.68,
4.69, 4.7, 4.71, 4.72, 4.73, 4.74, 4.75, 4.76, 4.77, 4.78, 4.79,
4.8, 4.81, 4.82, 4.83, 4.84, 4.85, 4.86, 4.87, 4.88, 4.89, 4.9,
4.91, 4.92, 4.93, 4.94, 4.95, 4.96, 4.97, 4.98, 4.99, 5, 5.01,
5.02, 5.03, 5.04, 5.05, 5.06, 5.07, 5.08, 5.09, 5.1, 5.11, 5.12,
5.13, 5.14, 5.15, 5.16, 5.17, 5.18, 5.19, 5.2, 5.21, 5.22, 5.23,
5.24, 5.25, 5.26, 5.27, 5.28, 5.29, 5.3, 5.31, 5.32, 5.33, 5.34,
5.35, 5.36, 5.37, 5.38, 5.39, 5.4, 5.41, 5.42, 5.43, 5.44, 5.45,
5.46, 5.47, 5.48, 5.49, 5.5, 5.51, 5.52, 5.53, 5.54, 5.55, 5.56,
5.57, 5.58, 5.59, 5.6, 5.61, 5.62, 5.63, 5.64, 5.65, 5.66, 5.67,
5.68, 5.69, 5.7, 5.71, 5.72, 5.73, 5.74, 5.75, 5.76, 5.77, 5.78,
5.79, 5.8, 5.81, 5.82, 5.83, 5.84, 5.85, 5.86, 5.87, 5.88, 5.89,
5.9, 5.91, 5.92, 5.93, 5.94, 5.95, 5.96, 5.97, 5.98, 5.99, 6,
6.01, 6.02, 6.03, 6.04, 6.05, 6.06, 6.07, 6.08, 6.09, 6.1, 6.11,
6.12, 6.13, 6.14, 6.15, 6.16, 6.17, 6.18, 6.19, 6.2, 6.21, 6.22,
6.23, 6.24, 6.25, 6.26, 6.27, 6.28, 6.29, 6.3, 6.31, 6.32, 6.33,
6.34, 6.35, 6.36, 6.37, 6.38, 6.39, 6.4, 6.41, 6.42, 6.43, 6.44,
6.45, 6.46, 6.47, 6.48, 6.49, 6.5, 6.51, 6.52, 6.53, 6.54, 6.55,
6.56, 6.57, 6.58, 6.59, 6.6, 6.61, 6.62, 6.63, 6.64, 6.65, 6.66,
6.67, 6.68, 6.69, 6.7, 6.71, 6.72, 6.73, 6.74, 6.75, 6.76, 6.77,
6.78, 6.79, 6.8, 6.81, 6.82, 6.83, 6.84, 6.85, 6.86, 6.87, 6.88,
6.89, 6.9, 6.91, 6.92, 6.93, 6.94, 6.95, 6.96, 6.97, 6.98, 6.99,
7), Trait1Percentiles = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03,
0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.13, 0.13, 0.13,
0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.16, 0.19, 0.19, 0.19, 0.19,
0.25, 0.28, 0.35, 0.38, 0.41, 0.5, 0.5, 0.5, 0.5, 0.54, 0.57,
0.57, 0.6, 0.6, 0.66, 0.66, 0.69, 0.69, 0.85, 0.85, 0.91, 1.01,
1.04, 1.07, 1.1, 1.13, 1.2, 1.23, 1.32, 1.42, 1.48, 1.48, 1.67,
1.73, 1.89, 1.98, 2.14, 2.14, 2.14, 2.33, 2.33, 2.52, 2.52, 2.77,
2.77, 3.12, 3.34, 3.46, 3.75, 3.97, 4.16, 4.57, 4.82, 5.1, 5.26,
5.45, 5.61, 5.73, 6.14, 6.36, 6.65, 7.09, 7.43, 7.43, 8.31, 8.31,
9.01, 9.01, 9.51, 9.51, 10.65, 11.15, 11.69, 12.03, 12.6, 13.39,
14.08, 14.61, 14.96, 15.59, 16.5, 17.23, 18.02, 18.8, 19.78,
20.79, 21.57, 22.33, 22.93, 22.93, 25.01, 25.92, 26.9, 26.9,
28.79, 29.83, 31.28, 32.35, 33.45, 34.43, 35.43, 36.91, 37.95,
39.31, 40.88, 42.05, 43.15, 44.22, 45.61, 46.87, 48.22, 49.23,
50.77, 52.03, 52.03, 54.46, 55.81, 56.94, 56.94, 59.37, 60.66,
61.95, 61.95, 64.28, 65.48, 66.96, 68, 68.79, 69.73, 70.55, 71.43,
72.44, 73.48, 74.3, 74.99, 75.81, 76.76, 77.73, 78.46, 78.46,
80.16, 80.79, 81.64, 81.64, 83.28, 83.97, 84.63, 84.63, 85.76,
86.27, 86.99, 87.46, 87.91, 88.35, 88.69, 88.91, 89.32, 89.73,
90.11, 90.43, 90.8, 91.24, 91.5, 91.69, 91.69, 92.44, 92.72,
93.2, 93.2, 93.76, 93.92, 94.17, 94.17, 94.52, 94.83, 95.21,
95.46, 95.72, 95.87, 95.94, 96.19, 96.35, 96.6, 96.85, 96.98,
97.1, 97.1, 97.2, 97.32, 97.32, 97.54, 97.64, 97.76, 97.76, 97.92,
97.98, 98.05, 98.05, 98.24, 98.3, 98.36, 98.39, 98.55, 98.58,
98.68, 98.77, 98.83, 98.87, 98.87, 98.99, 99.06, 99.06, 99.06,
99.06, 99.06, 99.06, 99.15, 99.15, 99.15, 99.28, 99.28, 99.31,
99.31, 99.31, 99.5, 99.53, 99.59, 99.62, 99.62, 99.65, 99.65,
99.65, 99.65, 99.69, 99.69, 99.72, 99.78, 99.78, 99.81, 99.81,
99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81,
99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.87,
99.87, 99.91, 99.91, 99.94, 99.94, 99.97, 99.97, 99.97, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100), Trait2Percentiles = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.03, 0.03, 0.03,
0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.06, 0.06, 0.06, 0.09, 0.09,
0.09, 0.09, 0.13, 0.13, 0.13, 0.13, 0.19, 0.19, 0.19, 0.22, 0.25,
0.28, 0.31, 0.31, 0.31, 0.41, 0.41, 0.41, 0.41, 0.41, 0.41, 0.57,
0.6, 0.63, 0.69, 0.76, 0.82, 0.82, 0.91, 0.98, 0.98, 1.13, 1.23,
1.23, 1.35, 1.51, 1.54, 1.57, 1.67, 1.67, 1.89, 1.89, 2.08, 2.08,
2.55, 2.55, 2.93, 3.02, 3.15, 3.34, 3.53, 3.94, 4.19, 4.6, 4.85,
5.04, 5.35, 5.57, 6.02, 6.27, 6.65, 7.02, 7.56, 8.06, 8.06, 8.85,
8.85, 9.7, 9.7, 10.55, 10.55, 11.62, 12.38, 12.98, 13.64, 14.3,
14.93, 15.62, 16.35, 17.35, 18.11, 19.15, 20.19, 21.39, 22.52,
23.5, 24.85, 25.61, 26.68, 26.68, 29.13, 29.13, 31.78, 31.78,
34.2, 34.2, 36.91, 38.33, 39.53, 41.35, 42.93, 44.72, 46.52,
48, 49.57, 50.96, 52.41, 54.27, 55.69, 57.64, 58.96, 60.85, 62.2,
64, 65.48, 65.48, 68.31, 69.86, 71.21, 71.21, 73.73, 75.02, 76.54,
77.45, 78.46, 79.78, 80.63, 81.8, 82.8, 83.81, 84.91, 85.86,
86.68, 87.4, 88.16, 88.54, 89.04, 89.86, 90.52, 90.93, 90.93,
91.81, 92.16, 92.47, 92.47, 93.48, 93.8, 94.27, 94.27, 94.68,
95.02, 95.37, 95.59, 95.94, 96.13, 96.41, 96.66, 96.76, 96.79,
97.01, 97.13, 97.32, 97.45, 97.51, 97.57, 97.57, 97.57, 97.95,
98.05, 98.05, 98.24, 98.36, 98.39, 98.39, 98.61, 98.61, 98.74,
98.77, 98.87, 98.93, 98.99, 99.02, 99.12, 99.21, 99.31, 99.4,
99.43, 99.53, 99.59, 99.59, 99.59, 99.59, 99.62, 99.65, 99.65,
99.65, 99.75, 99.75, 99.75, 99.78, 99.81, 99.84, 99.87, 99.87,
99.87, 99.87, 99.87, 99.87, 99.91, 99.91, 99.91, 99.94, 99.94,
99.94, 99.94, 99.94, 99.94, 99.94, 99.97, 99.97, 99.97, 99.97,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100)), class = "data.frame", row.names = c(NA, -701L))
You can use match here :
df1$Trait1Percentile <- LookupTable$Trait1Percentiles[match(df1$Trait1Score, LookupTable$Scores)]
head(df1)
# JobNumber Trait1Score Trait2Score Trait1Percentile
#1 634 3.89 4.00 14.08
#2 21 4.39 2.94 68.00
#3 300 4.22 3.17 46.87
#4 797 4.21 3.83 45.61
#5 1112 3.94 4.22 17.23
#6 147 3.90 3.83 14.61
With merge you need to select relevant columns
merge(df1, LookupTable, by.x = 'Trait1Score', by.y = 'Scores')[1:4]
Similarly in dplyr :
library(dplyr)
inner_join(df1, LookupTable, by = c('Trait1Score' = 'Scores')) %>% select(1:4)

Divide each column of a dataframe by one row of the dataframe

I would like to divide each column of my dataframe by the values of one row.
I tried to transform my dataframe into a matrix and to extract one row of the dataframe as a vector then divide the matrix by the vector but it did not work. Indeed, only the first row of the matrix got divided by the vector.
Here is my original dataframe.
And this is the code I tried to run :
data <- read_excel("Documents/TFB/xlsx_geochimie/solfatara_maj.xlsx")
View(data)
data.mat <- as.matrix(data[,2:20])
vector <- data[12,2:20]
data.mat/vector
We replicate the vector to make the length same and then do the division
data.mat/unlist(vector)[col(data.mat)]
# FeO Total S SO4 Total N SiO2 Al2O3 Fe2O3 MnO MgO CaO Na2O K2O
#[1,] 0.10 16.5555556 NA NA 0.8908607 0.8987269 0.1835206 0.08333333 0.03680982 0.04175365 0.04823151 0.5738562
#[2,] 0.40 125.8333333 NA NA 0.5510204 0.4456019 0.2359551 0.08333333 0.04294479 0.01878914 0.04501608 0.2588235
#[3,] 0.85 0.6111111 NA NA 1.0021295 1.0162037 0.7715356 1.08333333 0.53987730 0.69728601 1.03858521 1.0457516
#[4,] 0.15 48.0555556 NA NA 1.1027507 0.2569444 NA 0.08333333 0.01840491 0.01878914 0.04180064 0.1647059
#[5,] 0.85 NA NA NA 1.0889086 1.0271991 0.6591760 0.75000000 0.59509202 0.53862213 1.02250804 1.1228758
#[6,] NA NA NA NA 1.3426797 0.6319444 0.0411985 0.08333333 0.03067485 0.11899791 0.65594855 0.7764706
# TiO2 P2O5 LOI LOI2 Total Total 2 Fe2O3(T)
#[1,] 0.7924528 0.3928571 7.0841837 6.6963855 0.9922233 0.9894632 0.14489796
#[2,] 0.5094340 0.3214286 14.5561224 13.7710843 0.9958126 0.9936382 0.31020408
#[3,] 0.8679245 0.6428571 1.5637755 1.5228916 0.9990030 0.9970179 0.80612245
#[4,] 1.4905660 0.2857143 7.4056122 7.0024096 0.9795613 0.9769384 0.05510204
#[5,] 1.0377358 0.2500000 0.3520408 0.3783133 0.9969093 0.9960239 0.74489796
#[6,] 0.3018868 0.2500000 1.2551020 1.1879518 1.0019940 1.0000000 0.04489796
Or use sweep
sweep(data.mat, MARGIN = 2, unlist(vector), FUN = `/`)
Or using mapply with asplit
mapply(`/`, asplit(data.mat, 2), vector)
data
data_mat <- structure(c(0.2, 0.8, 1.7, 0.3, 1.7, NA, 5.96, 45.3, 0.22, 17.3,
NA, NA, NA, 6.72, NA, 4.08, 0.06, 0.16, NA, NA, NA, NA, NA, NA,
50.2, 31.05, 56.47, 62.14, 61.36, 75.66, 15.53, 7.7, 17.56, 4.44,
17.75, 10.92, 0.49, 0.63, 2.06, NA, 1.76, 0.11, 0.01, 0.01, 0.13,
0.01, 0.09, 0.01, 0.06, 0.07, 0.88, 0.03, 0.97, 0.05, 0.2, 0.09,
3.34, 0.09, 2.58, 0.57, 0.15, 0.14, 3.23, 0.13, 3.18, 2.04, 4.39,
1.98, 8, 1.26, 8.59, 5.94, 0.42, 0.27, 0.46, 0.79, 0.55, 0.16,
0.11, 0.09, 0.18, 0.08, 0.07, 0.07, 27.77, 57.06, 6.13, 29.03,
1.38, 4.92, 27.79, 57.15, 6.32, 29.06, 1.57, 4.93, 99.52, 99.88,
100.2, 98.25, 99.99, 100.5, 99.54, 99.96, 100.3, 98.28, 100.2,
100.6, 0.71, 1.52, 3.95, 0.27, 3.65, 0.22), .Dim = c(6L, 19L), .Dimnames = list(
NULL, c("FeO", "Total S", "SO4", "Total N", "SiO2", "Al2O3",
"Fe2O3", "MnO", "MgO", "CaO", "Na2O", "K2O", "TiO2", "P2O5",
"LOI", "LOI2", "Total", "Total 2", "Fe2O3(T)")))
vector <- structure(list(FeO = 2, `Total S` = 0.36, SO4 = NA_real_, `Total N` = NA_real_,
SiO2 = 56.35, Al2O3 = 17.28, Fe2O3 = 2.67, MnO = 0.12, MgO = 1.63,
CaO = 4.79, Na2O = 3.11, K2O = 7.65, TiO2 = 0.53, P2O5 = 0.28,
LOI = 3.92, LOI2 = 4.15, Total = 100.3, `Total 2` = 100.6,
`Fe2O3(T)` = 4.9), row.names = c(NA, -1L), class = c("tbl_df",
"tbl", "data.frame"))
To divide data frame, df, by the third row:
df/df[rep(3, nrow(df)), ]

How to compute/plot efficient frontiers per time period in one graph in R?

Currently we compute and sort data of stocks (X1 to X10). Historical data is stored in Excel and R for the time period 1950-1980, 1980-1999 and for 1950-1999.
The dataset:
date X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1 1950-01-01 5.92 6.35 4.61 4.08 5.47 3.90 2.35 1.49 2.27 0.82
2 1950-02-01 2.43 2.16 2.10 1.58 -0.05 1.14 1.51 1.52 2.02 1.12
3 1950-03-01 -0.81 0.21 -1.67 -0.02 -0.79 0.18 -0.22 1.03 0.12 1.75
4 1950-04-01 5.68 6.45 5.41 5.94 6.10 5.87 3.82 3.34 3.44 3.97
5 1950-05-01 3.84 1.60 1.64 3.33 2.54 2.12 4.46 2.83 3.82 4.75
6 1950-06-01 -9.88 -10.56 -8.02 -7.86 -7.27 -7.44 -7.13 -7.76 -6.32 -5.04
7 1950-07-01 9.09 8.76 7.31 5.88 3.84 4.61 3.09 3.07 1.41 0.42
598 1999-10-01 -0.95 -1.88 -1.25 -0.52 1.65 0.72 5.41 4.38 5.58 6.59
599 1999-11-01 11.57 9.15 8.17 7.14 6.15 4.95 5.78 4.21 1.55 2.15
600 1999-12-01 12.32 14.97 9.29 11.77 11.09 5.89 11.88 11.26 6.23 5.64
The main question is, we would like to compute/plot efficient frontiers for these 4 time periods to see how the efficient frontier has evolved in 1 graph. Are there ways to do this in R?
The efficient frontier is the set of optimal portfolios that offers the highest expected return for a defined level of risk or the lowest risk for a given level of expected return.
In modern portfolio theory, the efficient frontier (or portfolio frontier) is an investment portfolio which occupies the 'efficient' parts of the risk-return spectrum. Formally, it is the set of portfolios which satisfy the condition that no other portfolio exists with a higher expected return but with the same standard deviation of return.
So, how would one go about computing this in R?
dput sample data (first 50 rows)
> dput(head(data,50))
structure(list(X__1 = structure(c(-631152000, -628473600, -626054400,
-623376000, -620784000, -618105600, -615513600, -612835200, -610156800,
-607564800, -604886400, -602294400, -599616000, -596937600, -594518400,
-591840000, -589248000, -586569600, -583977600, -581299200, -578620800,
-576028800, -573350400, -570758400, -568080000, -565401600, -562896000,
-560217600, -557625600, -554947200, -552355200, -549676800, -546998400,
-544406400, -541728000, -539136000, -536457600, -533779200, -531360000,
-528681600, -526089600, -523411200, -520819200, -518140800, -515462400,
-512870400, -510192000, -507600000, -504921600, -502243200), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), X__2 = c(5.92, 2.43, -0.81, 5.68,
3.84, -9.88, 9.09, 4.93, 3.99, -0.5, 3.09, 15.77, 8.22, 0.36,
-7.36, 3.84, -2.81, -7.12, 3.57, 6.59, 1.04, -1.41, -1.42, -0.53,
1.86, -3.25, 0.68, -4.4, 0.57, 2.5, -0.36, -0.74, -1.11, -0.58,
3.22, 0.33, 5.01, 2.75, -1.25, -2.13, 1.3, -4.42, 0.25, -5.56,
-4.09, 2.71, 2.01, -3.15, 8.48, -0.16), X__3 = c(6.35, 2.16,
0.21, 6.45, 1.6, -10.56, 8.76, 4.63, 3.52, -1.2, 3.36, 10.98,
8.41, 0.81, -4.01, 3.56, -4.27, -6.11, 4.7, 5.3, 2.73, -3.07,
-0.13, 0.6, 1.1, -2.77, 2.37, -4.5, 1.87, 3.18, 1.51, 0.43, -1.91,
-1.52, 4.91, 1.43, 3.4, 3.03, -2.25, -2, 0.34, -4.75, 2.24, -6.53,
-1.87, 1.97, 1.78, -2.96, 7.38, 0.43), X__4 = c(4.61, 2.1, -1.67,
5.41, 1.64, -8.02, 7.31, 4.56, 5.18, -0.46, 3.52, 10.78, 8.46,
0.28, -4.88, 4.26, -3.25, -6.76, 6.78, 4.99, 3.86, -2.57, 0.59,
0.16, 1.75, -2.04, 2.49, -5.29, 1.76, 2.88, 0.76, 0.67, -1.67,
-1.45, 5.69, 2.95, 3.66, 1.15, -1.58, -2.34, 0.51, -3.82, 0.72,
-6.25, -2.33, 3.1, 2.19, -2.63, 7.3, 1.82), X__5 = c(4.08, 1.58,
-0.02, 5.94, 3.33, -7.86, 5.88, 4.68, 5.99, 0.75, 2.68, 9.29,
8, 1.08, -3.13, 4.21, -3.35, -5.01, 5.77, 4.85, 2.73, -3.44,
0.27, 1.56, 1.62, -2.35, 2.93, -4.62, 2.36, 2.56, 0.86, 0.16,
-1.8, -2.04, 5.12, 2.72, 3.21, 1.21, -2.17, -1.84, 0.32, -3.63,
1.47, -5.16, -0.65, 3.33, 1.34, -1.36, 6.24, 1.19), X__6 = c(5.47,
-0.05, -0.79, 6.1, 2.54, -7.27, 3.84, 6.29, 4.46, -0.24, 2.42,
6.12, 8.63, 0.88, -3.31, 4.56, -2.14, -5.62, 5.73, 5.36, 2.44,
-1.88, 0.83, 0.65, 1.47, -1.81, 2.31, -4.48, 2.56, 2.69, 0.9,
0.34, -0.62, -1.58, 6.59, 0.86, 3.58, 1.92, -1.85, -2.79, 0.7,
-3.4, 1.26, -5.26, -1.18, 4.26, 1.35, -0.97, 6.66, 1.77), X__7 = c(3.9,
1.14, 0.18, 5.87, 2.12, -7.44, 4.61, 4.57, 6.14, -0.84, 4.22,
8.37, 7.44, 0.69, -4.26, 4.13, -2.24, -6.75, 5.81, 4.35, 1.98,
-2.87, 0.93, 0.61, 1.27, -2.18, 2.97, -4.09, 2.27, 2.96, 1.16,
-0.38, -2.37, -0.71, 5.53, 2.45, 1.3, 0.31, -0.47, -2.03, 0.14,
-3.26, 1.79, -5.5, -1.47, 4.18, 1.96, -1.35, 7.06, 1.69), X__8 = c(2.35,
1.51, -0.22, 3.82, 4.46, -7.13, 3.09, 5.01, 5.84, -1.05, 3.81,
7.54, 6.46, 0.71, -3.56, 4.42, -1.87, -4.52, 7.3, 3.66, 2.11,
-2.92, 2.25, 2.17, 1.32, -1.71, 3.17, -4.63, 2.59, 3.89, 0.49,
0.21, -1.71, -1.18, 4.95, 3.21, 1.41, 0.89, -1.02, -2.89, 0.59,
-2.67, 1.47, -4.62, -0.69, 4.07, 2.83, -1.44, 6.11, 1.58), X__9 = c(1.49,
1.52, 1.03, 3.34, 2.83, -7.76, 3.07, 3.72, 6.21, -1.66, 3.46,
6.14, 7.17, 2.13, -3.19, 4.59, -2.65, -3.5, 7.43, 3.5, 2.41,
-2.73, 1.35, 1.97, 1.72, -1.8, 4.06, -5.35, 2.57, 3.14, 1.89,
-0.86, -1.73, -0.95, 6.07, 1.73, 1.09, 0.37, -1.34, -2.48, 0.31,
-3.2, 1.34, -4.99, -0.18, 4.35, 3.03, 0.09, 5.65, 2.39), X__10 = c(2.27,
2.02, 0.12, 3.44, 3.82, -6.32, 1.41, 4.54, 5.55, -0.97, 3.8,
5.69, 5.65, 1.78, -2.6, 4.21, -1.29, -2.63, 7.15, 3.52, 1.85,
-2.32, 0.96, 2.74, 1.9, -2.6, 3.83, -4.31, 3.15, 2.76, 0.93,
-0.39, -1.86, -1.57, 7.05, 2.36, -0.33, -0.23, -0.54, -2.6, 0.61,
-2.37, 2.12, -3.76, 0.47, 3.98, 3.03, 0.2, 5.63, 1.26), X__11 = c(0.82,
1.12, 1.75, 3.97, 4.75, -5.04, 0.42, 4.96, 4.32, 0.25, 2.26,
4.71, 5.05, 1.63, -1.53, 5.12, -2.59, -1.92, 6.89, 4.48, -0.09,
-2.49, 0.26, 4.03, 1.37, -2.82, 4.95, -5.1, 3.4, 4.29, 0.89,
-1.06, -2.18, -0.31, 5.76, 3.32, -1.04, -0.63, -1.78, -2.97,
0.55, -1.3, 2.75, -4.47, 0.48, 4.83, 2.85, 0.27, 4.4, 1.93)), .Names = c("date",
"X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8",
"X9", "X10"), row.names = c(NA, 50L), class = c("tbl_df",
"tbl", "data.frame"))
After a few correpondence via the comments with #Jonathan, I widened the example data from 3 columns to 12 columns with some sampling. And the code at the "With short-selling" section at the blog scales well for 10K observations:
# using code at:
# https://www.r-bloggers.com/a-gentle-introduction-to-finance-using-r-efficient-frontier-and-capm-part-1/
# https://datashenanigan.wordpress.com/2016/05/24/a-gentle-introduction-to-finance-using-r-efficient-frontier-and-capm-part-1/
library(data.table)
calcEFParams <- function(rets)
{
retbar <- colMeans(rets, na.rm = T)
covs <- var(rets, na.rm = T) # calculates the covariance of the returns
invS <- solve(covs)
i <- matrix(1, nrow = length(retbar))
alpha <- t(i) %*% invS %*% i
beta <- t(i) %*% invS %*% retbar
gamma <- t(retbar) %*% invS %*% retbar
delta <- alpha * gamma - beta * beta
retlist <- list(alpha = as.numeric(alpha),
beta = as.numeric(beta),
gamma = as.numeric(gamma),
delta = as.numeric(delta))
return(retlist)
}
# load data
link <- "https://raw.githubusercontent.com/DavZim/Efficient_Frontier/master/data/mult_assets.csv"
df <- data.table(read.csv(link))
df2 <- df[,lapply(.SD, sample),]
df3 <- cbind(df, df2)
df4 <- df3[,lapply(.SD, sample),]
df5 <- cbind(df3, df4)
Now loading the microbenchmark package, the performance is as such:
> library(microbenchmark)
> microbenchmark(calcEFParams(df5), times = 10)
Unit: milliseconds
expr min lq mean median uq max neval
calcEFParams(df5) 2.692514 2.764053 2.795127 2.777547 2.805447 3.024349 10
It seems that David Zimmermann's code is scalable and efficient enough!

R: finding intersection between two vectors [duplicate]

This question already has answers here:
Why are these numbers not equal?
(6 answers)
Closed 5 years ago.
v1 = c(2, 2.01, 2.02, 2.03, 2.04, 2.05, 2.06, 2.07, 2.08, 2.09, 2.1,
2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18, 2.19, 2.2, 2.21,
2.22, 2.23, 2.24, 2.25, 2.26, 2.27, 2.28, 2.29, 2.3, 2.31, 2.32,
2.33, 2.34, 2.35, 2.36, 2.37, 2.38, 2.39, 2.4, 2.41, 2.42, 2.43,
2.44, 2.45, 2.46, 2.47, 2.48, 2.49, 2.5, 2.51, 2.52, 2.53, 2.54,
2.55, 2.56, 2.57, 2.58, 2.59, 2.6, 2.61, 2.62, 2.63, 2.64, 2.65,
2.66, 2.67, 2.68, 2.69, 2.7, 2.71, 2.72, 2.73, 2.74, 2.75, 2.76,
2.77, 2.78, 2.79, 2.8, 2.81, 2.82, 2.83, 2.84, 2.85, 2.86, 2.87,
2.88, 2.89, 2.9, 2.91, 2.92, 2.93, 2.94, 2.95, 2.96, 2.97, 2.98,
2.99)
> intersect(v1, seq(2, 2.99, 0.01))
[1] 2.00 2.01 2.02 2.04 2.05 2.06 2.08 2.09 2.10 2.12 2.13 2.14 2.16 2.17 2.19 2.20 2.21 2.23 2.24 2.25 2.26 2.27
[23] 2.28 2.29 2.30 2.31 2.33 2.34 2.35 2.37 2.38 2.39 2.41 2.42 2.44 2.45 2.46 2.48 2.49 2.50 2.51 2.52 2.53 2.54
[45] 2.55 2.56 2.57 2.58 2.59 2.60 2.62 2.63 2.64 2.66 2.67 2.69 2.70 2.71 2.72 2.73 2.74 2.75 2.76 2.77 2.78 2.79
[67] 2.80 2.81 2.82 2.83 2.84 2.85 2.87 2.88 2.89 2.91 2.92 2.94 2.95 2.96 2.97 2.98 2.99
I have a vector of length 100 called v1. I want to see the intersection of v1 and a seq(2, 2.99, 0.01) vector (should be just v1 itself). But I get a vector that is only 83 elements long? And clearly 2.03, 2.15 ... are not in the intersection. How is that possible?
This a floating point error in r. See the Floating Point Guide for more information.
This can be seen as the error because this returns what you're looking for:
v1 = c(2, 2.01, 2.02, 2.03, 2.04, 2.05, 2.06, 2.07, 2.08, 2.09, 2.1,
2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18, 2.19, 2.2, 2.21,
2.22, 2.23, 2.24, 2.25, 2.26, 2.27, 2.28, 2.29, 2.3, 2.31, 2.32,
2.33, 2.34, 2.35, 2.36, 2.37, 2.38, 2.39, 2.4, 2.41, 2.42, 2.43,
2.44, 2.45, 2.46, 2.47, 2.48, 2.49, 2.5, 2.51, 2.52, 2.53, 2.54,
2.55, 2.56, 2.57, 2.58, 2.59, 2.6, 2.61, 2.62, 2.63, 2.64, 2.65,
2.66, 2.67, 2.68, 2.69, 2.7, 2.71, 2.72, 2.73, 2.74, 2.75, 2.76,
2.77, 2.78, 2.79, 2.8, 2.81, 2.82, 2.83, 2.84, 2.85, 2.86, 2.87,
2.88, 2.89, 2.9, 2.91, 2.92, 2.93, 2.94, 2.95, 2.96, 2.97, 2.98,
2.99)
v2 <- seq(2, 2.99, 0.01)
v1 <- round(v1,2) #rounds to 2 decimal places
v2 <- round(v2,2)
intersect(v1,v2) #returns v1

Calculating stock returns in R

absolute noob here guys, so please bear with me. i have the following code to load data from a csv file and do some calculations. one of the calculations is just getting monthly returns for each stock. but i am clearly doing something wrong as I am getting the following msg:
Error in log(mprices) : non-numeric argument to mathematical function
I am putting up my code that I have written so far. Very basic stuff.
stockdata <- read.csv("NZXALL.csv", stringsAsFactors = F)
# Converting to XTS
dates <- stockdata$Date
stock.xts <- as.xts(stockdata, order.by=as.Date(dates, "%d/%m/%Y"))
data <- stock.xts
data$Date <- NULL #Remove Date field
mprices <- aggregate(data, as.yearmon, tail, 1) #Convert to monthly prices
monthly.returns <- diff(log(mprices)) # convert prices to log returns
Any idea where I am going wrong? The dataset is in the following manner
Date StockX StockY StockZ...
dd/mm/yyyy price1 price1 price1
There are currently 142 stocks and data ranges between 2005 & 2014
Thanks!
> dput(head(stockdata))
structure(list(Date = c("5/01/2005", "6/01/2005", "7/01/2005",
"10/01/2005", "11/01/2005", "12/01/2005"), ABA = c(1.11, 1.11,
1.12, 1.13, 1.12, 1.12), ACY = c(3.61, 3.61, 3.7, 3.7, 3.75,
3.75), AFI = c(3.89, 3.89, 3.9, 3.92, 3.92, 3.98), AIA = c(8.05,
8.2, 8.1, 8.11, 8.1, 8.08), AIR = c(1.65, 1.63, 1.63, 1.63, 1.62,
1.62), ALF = c(2.6, 2.6, 2.6, 2.6, 2.65, 2.5), AMP = c(7.75,
7.65, 7.75, 8, 8.03, 8), ANZ = c(22.3, 22.3, 22.2, 22.5, 22.63,
22.6), AOR = c(0.03, 0.03, 0.03, 0.03, 0.03, 0.03), APN = c(5.58,
5.58, 5.58, 5.58, 5.58, 5.58), ARG = c(1.18, 1.18, 1.18, 1.18,
1.18, 1.18), ASBPA = c(1.07, 1.07, 1.07, 1.05, 1.07, 1.06), ASBPB = c(1.01,
1, 1.01, 1.01, 1.01, 1.01), ATM = c(0.08, 0.08, 0.08, 0.08, 0.08,
0.08), ATR = c(2.05, 2.05, 2.05, 2.05, 2.05, 2.05), AWF = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), BGR = c(1.13,
1.14, 1.14, 1.14, 1.15, 1.15), BIT = c(7.47, 7.47, 7.45, 7.45,
7.4, 7.58), BLT = c(0.18, 0.19, 0.25, 0.2, 0.2, 0.2), CAV = c(4.55,
4.59, 4.6, 4.55, 4.55, 4.5), CDI = c(0.32, 0.32, 0.32, 0.33,
0.35, 0.36), CEN = c(6.37, 6.53, 6.52, 6.5, 6.44, 6.4), CMO = c(2.96,
2.96, 2.99, 2.99, 3, 3), CVT = c(2.1, 2.1, 2.1, 2.1, 2.1, 2.1
), DPC = c(2.79, 2.8, 2.76, 2.76, 2.76, 2.69), EBO = c(4.54,
4.52, 4.53, 4.55, 4.55, 4.55), EUT = c(12.35, 12.35, 12.35, 12.35,
12.05, 12.05), FBU = c(6.6, 6.57, 6.5, 6.4, 6.44, 6.46), FCS = c(6.85,
6.85, 6.85, 6.85, 6.85, 6.85), FCT = c(5.17, 5.15, 5.14, 5.12,
5.15, 5.15), FIN = c(1, 1, 1, 1, 1, 1), FNZ = c(1.46, 1.46, 1.47,
1.47, 1.48, 1.48), FPH = c(3.28, 3.23, 3.18, 3.24, 3.23, 3.17
), FRE = c(3.1, 3.1, 3.1, 3.1, 3.11, 3.1), GFF = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), GLL = c(0.85,
0.84, 0.84, 0.85, 0.85, 0.85), GMT = c(1.14, 1.14, 1.15, 1.15,
1.14, 1.15), GPG = c(2.21, 2.23, 2.22, 2.19, 2.21, 2.25), GXH = c(0.1,
0.1, 0.1, 0.1, 0.12, 0.13), HBY = c(6.4, 6.45, 6.46, 6.5, 6.6,
6.5), HED = c(4.2, 4.25, 4.25, 4.22, 4.22, 4.22), HLG = c(3.8,
3.75, 3.77, 3.75, 3.73, 3.61), IFT = c(3.36, 3.45, 3.45, 3.45,
3.42, 3.4), JFJ = c(5.3, 5.3, 5.3, 5.3, 5.3, 5.35), JMO = c(11.35,
11.35, 11.35, 11.38, 11.4, 11.65), KFL = c(1.04, 1.04, 1.07,
1.08, 1.08, 1.09), KIP = c(1.13, 1.12, 1.15, 1.16, 1.15, 1.12
), KRK = c(2.28, 2.28, 2.26, 2.3, 2.3, 2.3), LPC = c(1.75, 1.73,
1.75, 1.77, 1.88, 1.86), MCK = c(0.6, 0.6, 0.6, 0.59, 0.6, 0.59
), MDZ = c(2.43, 2.47, 2.48, 2.48, 2.48, 2.45), MET = c(3.46,
3.45, 3.45, 3.45, 3.45, 3.45), MFT = c(2.3, 2.35, 2.38, 2.34,
2.32, 2.32), MGL = c(1.12, 1.15, 1.15, 1.15, 1.2, 1.2), MHI = c(8.22,
8.2, 8.14, 8, 8.05, 8), MOW = c(1.62, 1.62, 1.62, 1.62, 1.62,
1.62), MVN = c(1.65, 1.65, 1.65, 1.64, 1.64, 1.6), MZY = c(4.44,
4.45, 4.48, 4.48, 4.5, 4.5), NPT = c(0.99, 0.98, 0.98, 0.99,
1, 1), NPX = c(5.94, 5.95, 5.95, 5.9, 5.84, 5.9), NTH = c(2.95,
2.95, 2.96, 2.96, 2.96, 2.99), NTL = c(0.11, 0.11, 0.11, 0.11,
0.1, 0.1), NWF = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), NZF = c(0.4, 0.4, 0.41, 0.42, 0.42, 0.41), NZO = c(1.08,
1.11, 1.13, 1.12, 1.11, 1.1), NZR = c(31, 30.1, 30.95, 30.55,
30.9, 30.8), NZX = c(9.2, 9.3, 9.38, 9.35, 9.35, 9.29), OZY = c(2.3,
2.28, 2.28, 2.28, 2.3, 2.3), PBG = c(3.45, 3.39, 3.33, 3.44,
3.4, 3.4), PCT = c(0.91, 0.91, 0.92, 0.92, 0.91, 0.91), PEB = c(0.28,
0.28, 0.28, 0.28, 0.28, 0.28), PFI = c(1.04, 1.05, 1.05, 1.04,
1.04, 1.03), PGC = c(4.01, 4.1, 4.15, 4.15, 4.15, 4.02), PGW = c(2.15,
2.18, 2.2, 2.21, 2.2, 2.18), PIL = c(0.02, 0.02, 0.02, 0.02,
0.02, 0.02), POT = c(5.59, 5.58, 5.6, 5.6, 5.6, 5.45), PPG = c(0.84,
0.84, 0.83, 0.84, 0.86, 0.85), PPL = c(2.76, 2.72, 2.69, 2.65,
2.61, 2.7), PPP = c(0.14, 0.14, 0.14, 0.14, 0.15, 0.14), RBC = c(1.06,
1.06, 1.05, 1.06, 1.06, 1.05), RBD = c(1.28, 1.28, 1.28, 1.28,
1.28, 1.28), RNS = c(0.94, 0.99, 1.03, 1.04, 1, 1.01), RYM = c(3.65,
3.7, 3.7, 3.7, 3.68, 3.64), SAN = c(4.55, 4.5, 4.6, 4.6, 4.55,
4.51), SCT = c(3.25, 3.19, 3.1, 3.2, 3.21, 3.25), SCY = c(0.83,
0.84, 0.84, 0.83, 0.83, 0.83), SEA = c(0.07, 0.07, 0.07, 0.08,
0.07, 0.07), SEK = c(4.9, 4.9, 4.8, 4.92, 4.92, 4.92), SKC = c(5.43,
5.48, 5.45, 5.41, 5.5, 5.22), SKL = c(1.25, 1.25, 1.25, 1.25,
1.25, 1.25), SKT = c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), SLG = c(0.39, 0.4, 0.39, 0.38, 0.39, 0.39
), SPN = c(1.29, 1.29, 1.26, 1.29, 1.29, 1.28), SPY = c(0.08,
0.08, 0.08, 0.08, 0.08, 0.08), STU = c(4.74, 4.76, 4.85, 4.8,
4.74, 4.72), TCL = c(5.85, 5.99, 5.99, 5.99, 6.02, 6), TEL = c(6.25,
6.23, 6.23, 6.2, 6.23, 6.2), TEM = c(4.5, 4.5, 4.5, 4.55, 4.55,
4.55), TEN = c(2.18, 2.18, 2.19, 2.18, 2.19, 2.18), THL = c(2.09,
2.1, 2.09, 2.09, 2.15, 2.15), TLS = c(5.36, 5.36, 5.35, 5.4,
5.42, 5.42), TNZ = c(1.19, 1.19, 1.2, 1.19, 1.2, 1.19), TPW = c(5.7,
5.6, 5.61, 5.6, 5.6, 5.6), TRS = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), TTK = c(2.6, 2.6, 2.63, 2.64,
2.55, 2.55), TUA = c(4.22, 4.15, 4.16, 4.2, 4.15, 4.15), TUR = c(3,
3, 3, 3.1, 3.1, 3.1), TWR = c(2.14, 2.14, 2.11, 2.08, 2.08, 2.06
), VCT = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), VHP = c(0.99, 0.99, 1, 0.99, 0.99, 0.99), VIL = c(0.85,
0.85, 0.9, 0.86, 0.85, 0.84), WBC = c(18.32, 18.32, 18.32, 18.32,
18.32, 18.32), WDT = c(0.58, 0.53, 0.58, 0.54, 0.5, 0.5), WHS = c(3.71,
3.77, 3.72, 3.69, 3.66, 3.63), AUG = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), BRM = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), BRM.1 = c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), DGL = c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), HFL = c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), RAK = c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), TPI = c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), AORWA = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), DIL = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), MLN = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), OGC = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), OIC = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), XRO = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), CUE = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), KIPGC = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), KMD = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), DNZ = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), TIL = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), CNU = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), HNZ = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), MAD = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), SUM = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), TME = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), MOA = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), NTLOA = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), FSF = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), JMOOA = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), .Names = c("Date",
"ABA", "ACY", "AFI", "AIA", "AIR", "ALF", "AMP", "ANZ", "AOR",
"APN", "ARG", "ASBPA", "ASBPB", "ATM", "ATR", "AWF", "BGR", "BIT",
"BLT", "CAV", "CDI", "CEN", "CMO", "CVT", "DPC", "EBO", "EUT",
"FBU", "FCS", "FCT", "FIN", "FNZ", "FPH", "FRE", "GFF", "GLL",
"GMT", "GPG", "GXH", "HBY", "HED", "HLG", "IFT", "JFJ", "JMO",
"KFL", "KIP", "KRK", "LPC", "MCK", "MDZ", "MET", "MFT", "MGL",
"MHI", "MOW", "MVN", "MZY", "NPT", "NPX", "NTH", "NTL", "NWF",
"NZF", "NZO", "NZR", "NZX", "OZY", "PBG", "PCT", "PEB", "PFI",
"PGC", "PGW", "PIL", "POT", "PPG", "PPL", "PPP", "RBC", "RBD",
"RNS", "RYM", "SAN", "SCT", "SCY", "SEA", "SEK", "SKC", "SKL",
"SKT", "SLG", "SPN", "SPY", "STU", "TCL", "TEL", "TEM", "TEN",
"THL", "TLS", "TNZ", "TPW", "TRS", "TTK", "TUA", "TUR", "TWR",
"VCT", "VHP", "VIL", "WBC", "WDT", "WHS", "AUG", "BRM", "BRM.1",
"DGL", "HFL", "RAK", "TPI", "AORWA", "DIL", "MLN", "OGC", "OIC",
"XRO", "CUE", "KIPGC", "KMD", "DNZ", "TIL", "CNU", "HNZ", "MAD",
"SUM", "TME", "MOA", "NTLOA", "FSF", "JMOOA"), row.names = c(NA,
6L), class = "data.frame")

Resources