Year = c(2013, 2013, 2014, 2014, 2015, 2014, 2013, 2015, 2015);districts = c("A", "B", "C", "B","A", "A", "C" ,"C","B"); sex = c("M"," F", "M", "F","M"," F","M", "F", "F"); age = c("age1",'age2', 'age3','age4', "age5","age6", "age7","age8","age8");df = data.frame(Year, districts, sex, age); df
我想用以下格式制作:
Year = c(2013, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 2015); districts = c("A", "B", "C", "A","B", "C", "A" ,"B","C"); sex = c("M"," F", "M", "F","F"," M","M", "F", "F"); age = c("age1",'age2', 'age3','age4', "age5","age6", "age7","age8","age8");df = data.frame(Year, districts, sex, age) ;df
答案 0 :(得分:3)
我们可以arrange
列,然后检查'age'中的下一个值是否与当前值相同
library(dplyr)
res <- df %>%
arrange(Year, districts) %>%
mutate(age = paste0('age', cumsum(c(TRUE, age[-1] != age[-n()]))))
all.equal(res, out)
#[1] TRUE
注意:删除原始数据集('df')中'sex'列中的空格和预期输出('out')后。此外,转换为character
类以便于比较