0

I want to create a new variable, created_idx_var, that is equal to 1 if the variable name contains any of the phrases "idx", "Idx", "indx", "Indx", "index", "Index", "etf", "ETF", or if the variable index equals "Yes".

I just started to learn R. My first step is to eliminate the dataset and keep only equity funds. Then I want to create a flag to see if a fund is an index fund. I searched online but couldn't find anything.

Some of the sample data: enter image description here

This is my code so far.

library(readxl)
mydata <- read_excel("C:/category.xlsx",sheet = 1)
utils::View(mydata)
mydata <- subset(mydata, global_group=="Equity")

1 Answers1

0

Suppose you have a vector of values:

x = c("idx", "a", "b","c", "Index")

Then you can create a binary vector as you mentioned which have 1 if match any string from the list (idx|Idx|indx|Indx|index|Index|etf|ETF) or 0 otherwise:

result = sapply(x, function(x) ifelse(grepl("idx|Idx|indx|Indx|index|Index|etf|ETF", x) == 1, 1, 0))
Aleksandr
  • 1,814
  • 11
  • 19
  • 1
    No need for `ifelse()`. You could simply do `+sapply(x, function(x) grepl("idx|Idx|indx|Indx|index|Index|etf|ETF", x))` or `+stringi::stri_detect_regex(x, "idx|Idx|indx|Indx|index|Index|etf|ETF")` – Steven Beaupré Oct 08 '17 at 17:25