我是这个组的新成员(也是一个新的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中变量(和变量)的名称。例如,我希望“公共部门银行”和“私营部门银行”在新的专栏中以“金融”和“私营部门工业”和“公共部门工业”作为“非金融”。希望我说得够清楚了。非常感谢你的帮助。
发布于 2016-04-07 10:32:28
假设您的dataframe名为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字段中包含"bank“和"industrial”两个词,那么您就可以这样决定如何在新列中调用这些值:
df$newColumn[grepl("bank", df$V2)] <- "financial"
df$newColumn[grepl("industrial", df$V2)] <- "non-financial"这与数据表的工作方式是一样的。
发布于 2016-04-07 12:18:43
在这个场景中,替换()非常方便。假设数据为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")发布于 2016-04-07 10:50:38
如果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
https://stackoverflow.com/questions/36473430
复制相似问题