我想在data.frame中创建一个列,将年份第一次出现在每个id中。
也就是说,我有这些数据:
example <- structure(list(id = structure(c(1, 2, 3, 4, 5), class = "numeric"),
`2007` = c(0, 0, 0, 0, 0), `2008` = c(0, 0, 0, 0, 1), `2009` = c(1,
0, 0, 0, 0), `2010` = c(1, 0, 1, 0, 1), `2011` = c(0, 0,
0, 0, 0), `2012` = c(1, 0, 1, 1, 1), `2013` = c(1, 0, 1,
0, 1), `2014` = c(1, 1, 1, 1, 0), `2015` = c(1, 1, 0, 0,
0), `2016` = c(1, 1, 1, 0, 1)), row.names = c(NA, 5L), class = "data.frame")
我想得到以下内容:
example2 <- structure(list(id = structure(c(1, 2, 3, 4, 5), class = "numeric"),
`2007` = c(0, 0, 0, 0, 0), `2008` = c(0, 0, 0, 0, 1), `2009` = c(1,
0, 0, 0, 0), `2010` = c(1, 0, 1, 0, 1), `2011` = c(0, 0,
0, 0, 0), `2012` = c(1, 0, 1, 1, 1), `2013` = c(1, 0, 1,
0, 1), `2014` = c(1, 1, 1, 1, 0), `2015` = c(1, 1, 0, 0,
0), `2016` = c(1, 1, 1, 0, 1), situation = c(2009, 2014,
2010, 2012, 2008)), row.names = c(NA, 5L), class = "data.frame")
这可能吗?欢迎大家的帮助。谢谢。
试试这个:
#Code
example$situation <- apply(example[,-1],1,function(x) names(x)[min(which(x==1))])
输出:
example
id 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 situation
1 1 0 0 1 1 0 1 1 1 1 1 2009
2 2 0 0 0 0 0 0 0 1 1 1 2014
3 3 0 0 0 1 0 1 1 1 0 1 2010
4 4 0 0 0 0 0 1 0 1 0 0 2012
5 5 0 1 0 1 0 1 1 0 0 1 2008
或与dplyr
和tidyr
重塑合并:
library(dplyr)
library(tidyr)
#Code
example <- example %>%
left_join(
example %>% pivot_longer(-1) %>%
group_by(id) %>%
summarise(situation=name[min(which(value==1))])
)
相同的输出。