我正在尝试在R中创建一个新变量,它根据另一个变量中的重复值为每个观察提供唯一(有序)数值。我已经把数据放在下面,我希望它看起来像什么。有人可以帮忙吗?
name <- c("Alex", "Alex", "Alex", "Bill", "Bill", "Cathy")
purchase <- c("hat", "bag", "book", "bag", "book", "book")
individual_purchase_No <- c(1, 2, 3, 1, 2, 1)
purchase.data <- data.frame(name, purchase)
purchase_order.data <- data.frame(name, purchase, individual_purchase_No)
答案 0 :(得分:1)
您可以使用dplyr
:
library(dplyr)
purchase.data %>% group_by(name) %>%
mutate(individual_purchase_No = 1:n())
## Source: local data frame [6 x 3]
## Groups: name [3]
##
## name purchase individual_purchase_No
## (fctr) (fctr) (int)
## 1 Alex hat 1
## 2 Alex bag 2
## 3 Alex book 3
## 4 Bill bag 1
## 5 Bill book 2
## 6 Cathy book 1
基础R解决方案例如:
purchase.data$individual_purchase_No <- sequence(table(purchase.data$name))
表计算每个名称的出现次数,然后序列为每个数字n
创建序列1:n
。