2016-04-07 138 views
1

我是這個組中的新成員(也是一個相當新的R用戶),我有一個問題。我有這樣的data.table更改R中字段的名稱

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中變量(和變量)的名稱。例如,我希望「公共部門銀行」和「私營部門銀行」在「財務」和「私營部門工業」和「公共部門工業」這個新專欄中出現爲「非金融」。希望我已經足夠清楚。非常感謝您的幫助。

+0

的可能的複製http://stackoverflow.com/questions/7531868/how-to-rename-a-single-column-in -a-data-frame-in-r,http://stackoverflow.com/questions/5824173/replace-a-value-in-a-data-frame-based-on-a-conditional-if-statement-in -r等 –

+0

使用包data.table時,我建議他們的offcial Cheat Sheet https://s3.amazonaws.com/assets.datacamp.com/img/blog/data+table+cheat+sheet.pdf – Berecht

回答

0

假設你的數據框被稱爲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" 

這適用於數據表以同樣的方式,以及

0

如果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

1

更換()就可以派上用場了這種情況。假設你的數據幀爲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")