0
        string <- c("AUSTRALIAN DOLLAR","BRAZILIAN REAL","CANADIAN DOLLAR","CHINESE YUAN","COLOMBIAN 
        PESO","DANISH KRONER",
        "EURO, HONG KONG DOLLAR", "HUNGARIAN FORINT", "INDIAN RUPEE", "INDONESIAN RUPIAH",
        "JAPANESE YEN", "KOREA (SOUTH) WON", "MALAYSIAN RINGGIT", "MEXICAN PESO", 
        "NEW ZEALAND DOLLAR", "NORWEIGIAN KRONER", "PERUCVIA SOL", 
        "POLISH ZLOTY", "Russian Rouble (new)",
        "SINGAPORE DOLLAR", "STH AFRICAN RAND", 
        "SWEIDSH KRONER", "SWISS FRANC", "TAIWANESE DOLAR",
        "THAILAND BAHTS", "TURKISH LIRA", "UK STERLING", "US DOLLAR")

          cashstring <- c("AUD","BRL","CAD","CHF","CNY","COP","DKK","EUR","GBP","HKD",
            "HUF","IDR","INR","JPY","KRW","MXN", "MYR","NOK","NZD",
            "PEN","PLN","RUB","SEK","SGD","THB","TRY","TWD",
            "USD","ZAR") 

         lg$type <- ifelse(lg$cashtype == cashstring & lg$instrument_name == string, "Cash", "Not Cash")

and i keep getting this error back:

                Warning messages:
                1: In lg$cashtype == cashstring :
                 longer object length is not a multiple of shorter object length
                2: In lg$instrument_name == string :
                 longer object length is not a multiple of shorter object length

How could i fix this simple problem. the columns corresponds to the relevant currency so it shouldn't be a problem.

Thanks

jogo
  • 12,469
  • 11
  • 37
  • 42
Tykid15
  • 61
  • 5
  • What is `lg`? Please make your example reproducible! – jogo Jun 01 '20 at 10:27
  • it is a large dateset. the string and cashstring variable are originally in the column i'm looking to search within, but this is my version of compressing it. when name column "UK STERLING" is observed, then cashtype will follow "GBP" etc. – Tykid15 Jun 01 '20 at 11:36
  • https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example You can show a reduced dataset as example. – jogo Jun 01 '20 at 14:03

1 Answers1

1

There are a couple of things going on here, but more information on what you're looking to do would be helpful.

The warning messages are telling you that the length of lg$cashtype is not a multiple of the length of cashstring, and similarly for instrument_name and string. It's doing this because it's comparing each entry of lg$cashtype to each of cashstring, in order, and then restarting once it reaches the end of the shorter object (and same for instrument_name and string. One weird thing is that the length of cashstring and string as you've posted them are not the same (I believe you're missing the cashstring entry for "ZAR") -- it's likely that this is part of your problem.

But even if you make that change, what your code will be doing is checking to see if the first row of lg (assuming it's a data frame) corresponds to "AUSTRALIAN DOLLAR" and "AUD", the second to "BRAZILIAN REAL" and "BRL", and so on. Unless your data frame is ordered in this very way (in which case you probably wouldn't be running this code), this probably isn't what you want. I imagine you want to do something like:

lg$type <- ifelse(lg$cashtype %in% cashstring & lg$instrument_name %in% string, 
                  "Cash", "Not Cash")

This will, for every row, tells you whether the corresponding cashtype is a value existing in cashstring, and the same for instrument_name and string.

If you provide more details about what you're trying to do, I'd be happy to refine my response.

Dharman
  • 30,962
  • 25
  • 85
  • 135