2016-02-26 120 views
1

現有列我與R工作組,並在這裏對數據幀的新列是我的數據框[R創建基於,使用R

Died.At <- c(22,40,72,41) 
Writer.At <- c(16, 18, 36, 36) 
First.Name <- c("John", "John", "Walt", "Walt") 
Second.Name <- c("Doe", "Poe", "Whitman", "Austen") 
Sex <- c("MALE", "MALE", "MALE", "MALE") 

writers_df <- data.frame(Died.At, Writer.At, First.Name, Second.Name, Sex) 

我想補充一個根據名字叫做id新列,所以約翰和沃爾特在這種情況下,我知道我可以很容易地通過

id<-c("1","1","2","2") 

做到這一點,但我有一個大的數據集處理,另外,名稱將不會再次出現之後,所以將不再約翰時,華特,任何人都可以幫我這個請

回答

2

我們可以嘗試

library(data.table) 
setDT(writers_df)[, id:= .GRP, First.Name] 

還是一個base R選項

writers_df$id <- cumsum(!duplicated(writers_df$First.Name)) 

或者使用dplyr

library(dplyr) 
writers_df %>% 
    mutate(id = group_indices_(., .dots="First.Name")) 
+1

啊,那工作!謝謝! – on9jai