Hi everybody,
I would like to ask for help. I have data like that.
df <- data.frame(stringsAsFactors = FALSE,
ticker = as.factor(c("AAA", "AAA", "AAA", "AAM", "AAM", "AAM", "AAM",
"AAM", "AAM", "AAM")),
code = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
year = c(2016L, 2017L, 2018L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L,
2015L),
Rsquared = c(0.352056874, 0.153949414, 0.294185982, 0.471601759,
0.492063894, 0.072787034,
0.088809017, 0.027639196, 0.062938271,
0.01485792),
SPI = as.factor(c("0.849306191", "1.614827022", "1.023443021",
"0.54360083",
"0.495553509", "2.273584969",
"2.099646998", "3.116617298",
"2.400446634", "3.655988108")),
State = as.factor(c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0")),
Foreign = as.factor(c("0.1749", "0.1193", "0.0988", "0.003550307",
"0.014386416", "0.015",
"0.0265", "0.0255", "0.0097",
"0.0049")),
Domestic = as.factor(c("0.8251", "0.8807", "0.9012", "0.9964", "0.9856",
"0.985", "0.9735",
"0.9745", "0.9903", "0.9951"))
)
(1) I would like to generate a new variable "code" for each stock ticker, for example, ticker AAA will get code of 001, AAM gets code of 002, ABC gets 003,.....
(2) I would like to generate a new dummy variable "State10", if the value in "State" is greater than 10%, it will return 1, the others is 0.
I would be grateful if anyone can help.