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
  • 382
  • 1
  • 2
  • 11

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,000
  • 12
  • 95
  • 132