【问题标题】:Change the name of fields in R更改 R 中的字段名称
【发布时间】:2016-04-07 10:27:24
【问题描述】:

我是这个组的新手(也是一个全新的 R 用户),我有一个问题。我有一个这样的data.table

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  

问题是如何更改 V2 列中变量(和变量)的名称。例如,我希望“公共部门银行”和“私营部门银行”在新列中显示为“金融”和“私营部门工业”,“公共部门工业”作为“非金融”出现。希望我已经足够清楚了。非常感谢您的帮助。

【问题讨论】:

标签: r variables names


【解决方案1】:

replace() 在这种情况下会很方便。假设您的数据框为 DF,而您的新列为 V2new

# Creating new column V2new and replacing "Public/Private sector bank" to "financial" 
DF$V2new <- replace(DF$V2 ,DF$V2 =="Public sector bank"|DF$V2=="Private sector bank","financial") 
# Replacing "Public/Private sector industrial"  from V2new to "non-financial"
DF$V2new <-  replace(DF$V2new ,DF$V2new =="Public sector industrial"|DF$V2new =="Private sector industrial","non-financial")

【讨论】:

    【解决方案2】:

    假设您的数据框称为 df,您可以执行以下操作:

    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"
    

    或者,如果您确定您的 V2 字段中包含“银行”和“工业”这两个词,并且这就是您确定在新列中调用什么值的方式,您可以这样做:

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

    这同样适用于数据表

    【讨论】:

      【解决方案3】:

      如果 DT 是你的 data.table

      `DT[,':='(V3 = ifelse(V2 %in% c("Public sector bank","Private sector bank"),"Non financial","Financial")`]
      

      标准化文本字段通常是一种很好的做法,因此您可以考虑:

      DT[,':='(V3 = ifelse(tolower(gsub(" ","",V2)) %in% c("publicsectorbank","privatesectorbank"),"Non financial","Financial")]
      

      希望对你有帮助,我也推荐https://s3.amazonaws.com/assets.datacamp.com/img/blog/data+table+cheat+sheet.pdf

      【讨论】:

        猜你喜欢
        • 2015-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多