sylvia
sylvia

Reputation: 217

Combine for-loops

I have a dataframe with 2 variables (brith and death time) like this:

df<-data_frame(birth=c(1945,1977,1919,1966),death=c(2011,2015,1980,2019))

I would like to count the alive numbers year 1995-2000, I wrote a for loop counter for it

For year 1995

Man_counts <- 0
for(row in 1:nrow(df)){
  start<-df[row,"birth"]
  end<-df_m[row,"death"]
  if (start<=1995|end>=1995){
    Man_counts= Man_counts+1}}

For Year 1996

Man_counts_2 <- 0
for(row in 1:nrow(df)){
  start<-df[row,"birth"]
  end<-df_m[row,"death"]
  if (start<=1996|end>=1996){
    Man_counts_2= Man_counts_2+1}}

And so on, for other years(1997-2000). I failed to use a double loop to deal with it. Furthermore, a double loop sometimes run endless on my laptop. I would like to know how to combine them in an elegant way so that I don't have to run 1995-2000 every year separately .

Th ideal output could be

output<-data_frame(year=1995:2000, alive_counts=c(2,2,2,2,3,3,))

Many Thanks!

Upvotes: 1

Views: 238

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388862

You can use sapply :

df<-data_frame(birth=c(1945,1977,1919,1966),death=c(2011,2015,1980,2019))
years <- 1995:2000

result <- data.frame(years, alive_counts = sapply(years, function(x) 
                                   sum(df$birth <= x & df$death >= x)))

Upvotes: 2

Related Questions