0

Suppose I have the following dataframe:

df <- data.frame(stock_code = c("600000", "300000", "000000"), 
                 stock_price = c(10.00, 11.00, 12.00))

I would like to add some characters to the stock_code, the rules are:

  1. if the first number of stock_code is "6",then add".XSHG" to the stock_code, like 600000.XSHG
  2. if the first number of stock _code is "0" or "3", then add".XSHE" to the stock_code, like 300000.XSHE and 000000.XSHE

I know str_sub may be helpful, but can anybody tell me how to code in tidyverse with %>%?

PRZ
  • 551
  • 2
  • 11
Bob
  • 37
  • 6

1 Answers1

0

You can use grepl to look for a pattern and case_when to add conditions.

library(dplyr)

df %>%
  mutate(new_stock_code = case_when(
            grepl('^6', stock_code) ~ paste0(stock_code, '.XSHG'), 
            grepl('^[30]', stock_code) ~ paste0(stock_code, '.XSHE'), 
            TRUE ~ stock_code))


#  stock_code stock_price new_stock_code
#1     600000          10    600000.XSHG
#2     300000          11    300000.XSHE
#3     000000          12    000000.XSHE

In base R, you can use nested ifelse :

transform(df, new_stock_code = ifelse(grepl('^6', stock_code), 
     paste0(stock_code, '.XSHG'), ifelse(grepl('^[30]', stock_code), 
     paste0(stock_code, '.XSHE'),stock_code)))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • 1
    Both are nice. Code golf, bring `paste0` *outside* of the `case_when` and `ifelse` statements, have them return one of `'.XSHG'`, `'.XSHE'`, or `""`, and then `paste0(stock_code, ...)` to the entire `case_when`/`ifelse`. – r2evans Jul 04 '20 at 04:33