Search code examples
rvariablesnames

Change the name of fields in R


I am new in this group (and also a quite-new R user) and I have a question. I have a data.table like this

Date             V2                       Deal Type
-----------------

1: 2009-1       Public sector bank        Corporate Bond-Investment-Grade                 
2: 2009-1       Private sector bank       Corporate Bond-Investment-Grade                 
3: 2009-7       Private sector industrial Corporate Bond-Investment-Grade                   
4: 2009-1       Private sector bank       Corporate Bond-Investment-Grade                  
5: 2009-1       Private sector bank       Covered Bond                         
6: 2009-1       Public sector bank        Corporate Bond-Investment-Grade                 
7: 2009-1       Private sector bank       Corporate Bond-Investment-Grade  

The question is how do I change the names of variables (and variables) in column V2. For example i want that "public sector bank" and "private sector bank" would appear in a new column as "financial" and "private sector industrial" and "public sector industrial" as "non-financial". Hope I have been sufficiently clear. Thank you very much for your help.


Solution

  • Assuming your dataframe is called df, you could do something like:

    df <- read.csv("data.csv", stringsAsFactors=FALSE)
    
    df$newColumn[df$V2 == "Public sector bank" | df$V2 == "Private sector bank"] <- "financial"
    df$newColumn[df$V2 == "Public sector industrial" | df$V2 == "Private sector industrial"] <- "non-financial"
    

    or if you're sure that your V2 fields have the words "bank" and "industrial" in them, and thats how you're determining what to call the values in the new column, you could do this:

    df$newColumn[grepl("bank", df$V2)] <- "financial"
    df$newColumn[grepl("industrial", df$V2)] <- "non-financial"
    

    This works the same way with data tables as well