0

Hi I am trying to set all non-NA values to 1 in a column of my dataset. Here is some sample data:

structure(list(ID = c(1, 1, 1, 1, 2, 3, 4, 4), match = structure(c(NA, 
10227, 10227, 10957, NA, 11323, NA, 11323), class = "Date"), 
actual.date = structure(c(10135, 10258, 11808, 11808, 10773, 
13027, 13269, 12086), class = "Date")), .Names = c("ID", 
"match", "actual.date"), row.names = c(NA, -8L), class = "data.frame")

  ID      match actual.date
1  1       <NA>  1997-10-01
2  1 1998-01-01  1998-02-01
3  1 1998-01-01  2002-05-01
4  1 2000-01-01  2002-05-01
5  2       <NA>  1999-07-01
6  3 2001-01-01  2005-09-01
7  4       <NA>  2006-05-01
8  4 2001-01-01  2003-02-03

For the 'match' column I want to set all non-NA's equal to 1.

michael
  • 412
  • 1
  • 3
  • 13

1 Answers1

1

Normally you would use something like dat$match[!is.na(dat$match)] <- 1, but this causes an error in a Date column. You could instead use ifelse:

dat$match <- ifelse(is.na(dat$match), NA, 1)
dat
#   ID match actual.date
# 1  1    NA  1997-10-01
# 2  1     1  1998-02-01
# 3  1     1  2002-05-01
# 4  1     1  2002-05-01
# 5  2    NA  1999-07-01
# 6  3     1  2005-09-01
# 7  4    NA  2006-05-01
# 8  4     1  2003-02-03

If you want to make a binary column (as you seem to suggest in the comments under your original question), you could simply do:

dat$match <- !is.na(dat$match)
dat
#   ID match actual.date
# 1  1 FALSE  1997-10-01
# 2  1  TRUE  1998-02-01
# 3  1  TRUE  2002-05-01
# 4  1  TRUE  2002-05-01
# 5  2 FALSE  1999-07-01
# 6  3  TRUE  2005-09-01
# 7  4 FALSE  2006-05-01
# 8  4  TRUE  2003-02-03
josliber
  • 43,891
  • 12
  • 98
  • 133
  • because it's a date format. Something like `df$match = as.integer(df$match); df$match[!is.na(df$match)]=1` works. – Colonel Beauvel Jul 12 '15 at 09:37
  • I would go with `dat$match <- (!is.na(dat$match))^NA` just because I absolutely hate `ifelse` (but that's just me probably) – David Arenburg Jul 12 '15 at 09:38
  • @DavidArenburg any particular reason why? I find `ifelse` to be quite readable and useful, and I use it quite regularly. – josliber Jul 12 '15 at 09:42
  • Or possibly `dat$match <- as.numeric(substr(dat$match, 10, 10))` (though not sure if true for all cases in the data). – David Arenburg Jul 12 '15 at 09:42
  • [this](http://stackoverflow.com/questions/16275149/does-ifelse-really-calculate-both-of-its-vectors-every-time-is-it-slow) mainly – David Arenburg Jul 12 '15 at 09:43