简体   繁体   中英

Create column in a data.frame with a specific condition

I would like to create a column in a data.frame, placing the first time that the year appears in each id.

That is, I have this data:

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")

And I would like to get the following:

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")

Is it possible to do that? Every help is welcome. Thanks.

Try this:

#Code
example$situation <- apply(example[,-1],1,function(x) names(x)[min(which(x==1))])

Output:

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

Or with dplyr and tidyr reshaping and merging:

library(dplyr)
library(tidyr)
#Code
example <- example %>%
  left_join(
    example %>% pivot_longer(-1) %>%
      group_by(id) %>%
      summarise(situation=name[min(which(value==1))])  
  )

Same output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM