Raphael Li
Raphael Li

Reputation: 53

Create new column to identify the first value >0 across different columns

I have the following data frame

df <- data.frame(col1 = c(0,0,1,1),col2 = c(1,0,0,3),col2 = c(1,0,0,3))

How can I identify the the first value of each value which is greater than 0. The expected output is like this

df <- data.frame(col1 = c(0,0,1,1),col2 = c(1,0,0,3),col3 = c(1,0,0,3),col4 = c(1,0,1,1))

And I have tried the followings

for (i in 1:3){
  df$col4 <- apply(df[,c(0:i)],1,sum)
  if (df$col4>0) 
    break
  }

Upvotes: 0

Views: 42

Answers (3)

Prem
Prem

Reputation: 11965

df$col4 <- apply(df, 1, function(x) x[which(x>0)[1]])
df[is.na(df$col4),'col4'] <- 0

Upvotes: 0

Rich Scriven
Rich Scriven

Reputation: 99351

We can use max.col() for this.

df[cbind(1:nrow(df), max.col(df > 0, "first"))]
# [1] 1 0 1 1

Upvotes: 1

Sotos
Sotos

Reputation: 51592

Here is another idea using mapply,

unlist(mapply(`[`, split(df, 1:nrow(df)), max.col(df>0, ties.method = 'first')))
#1.col2 2.col1 3.col1 4.col1 
#     1      0      1      1 

Depending on what you need by 'count backward', you can either change ties.method to 'last', i.e.

unlist(mapply(`[`, split(df, 1:nrow(df)), max.col(df>0, ties.method = 'last')))
#1.col2.1 2.col2.1   3.col1 4.col2.1 
#       1        0        1        3 

Or reverse the data frame and leave ties.method to 'first', i.e.

unlist(mapply(`[`, split(rev(df), 1:nrow(df)), max.col(df>0, ties.method = 'first')))
#  1.col2 2.col2.1 3.col2.1 4.col2.1 
#       1        0        0        3 

Upvotes: 0

Related Questions