2

My dataframe looks like this:

colnames:

state   city     name_of_company    11/22    11/23    11/24
ga    atlanta     name              NA       NA         1
ny    newyork     name              NA       .75        1

I am trying to create a new column that contains the dates that correspond to the first non NA value in each row. I have no clue how to do this, so any help is greatly appreciated.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Christian
  • 139
  • 9
  • There will be more non-NA values after the first non-NA. The non-NA values represent business transactions, so the date of the first value is the date the company opened--which is what i need – Christian Nov 23 '16 at 21:17

2 Answers2

3

No max.col? I am disappointed. ;-)

names(dat[4:6])[max.col(!is.na(dat[4:6]), "first")]
#[1] "11/24" "11/23"
thelatemail
  • 91,185
  • 12
  • 128
  • 188
1

This is one possible solution

df$first_NA <- apply(df,1,function(x) names(which(which(!is.na(x))>3))[1])
code_is_entropy
  • 611
  • 3
  • 11