-2

I have a variable which is used after it has been formed using paste. However after pasting the variable comes insider quotes which makes it unusable. This variable is inside as.date so as.name is not working. noquotes is not working as well. Is there any other way this variable can be used in as.date quotes free?

An example:

names<-c("dateA","dateB")
dateA<-c("5/30/2014","6/01/2014")
dateB<-c("5/30/2014")
dateZ<-as.data.frame(c("5/30/2014","6/01/2014","6/02/2014"))
names(dateZ)<-"date"
dateZ$date <- as.Date(dateZ$date,"%m/%d/%Y")
for (item in names){
  dateZ[paste(item)] <- ifelse(dateZ$date %in% as.Date(paste(item), format = "%m/%d/%Y"), 1, 0)
}

The output is

> dateZ
        date dateA dateB
1 2014-05-30     0     0
2 2014-06-01     0     0
3 2014-06-02     0     0

However the output I want is

       date  dateA  dateB
1 2014-05-30     1     1
2 2014-06-01     1     0
3 2014-06-02     0     0

I suspect it is due to the quotes which are staying back because of paste(items). Simply using item yields the same result.

Thanks in advance and sorry for not providing an example before

Rajarshi Bhadra
  • 1,826
  • 6
  • 25
  • 41
  • 2
    Please carefully [read this post](http://adv-r.had.co.nz/Computing-on-the-language.html) and use its advice to provide a reproducible example. – Thomas Aug 13 '14 at 11:20
  • You may also want to read this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – nico Aug 13 '14 at 11:52

1 Answers1

0

I am sure there are other ways, but one way of solving it is by using get(item):

for (item in names){
  dateZ[item] <- ifelse(dateZ$date %in% as.Date(get(item), format = "%m/%d/%Y"), 1, 0)
}

In this example you don't achieve anything by your use of paste, but the method above will also work if the real case is actually something like item = paste0(item1, item2).

Jon
  • 56
  • 3